jshint-rb 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ require 'jshint'
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'jslint.js' do
4
+ before :all do
5
+ File.open(JSHint::DEFAULT_CONFIG_FILE, "w") { |f| f.write "color: red\nsize: 5\nshape: circle\n" }
6
+ File.open("custom_config.yml", "w") { |f| f.write "color: blue\nsize: 7\nborder: 2\n" }
7
+ File.open("other_config.yml", "w") { |f| f.write "color: green\nborder: 0\nshape: square" }
8
+ JSHint.config_path = "custom_config.yml"
9
+ end
10
+
11
+ describe "options in comments" do
12
+ before do
13
+ FakeFS.deactivate!
14
+ create_file 'spec-config.yml', 'evil: false'
15
+ create_file 'first.js', "
16
+ /*jslint evil: true*/
17
+ eval('alert(\"muahahahaha\")');
18
+ "
19
+ create_file 'second.js', "
20
+ // I shouldn't be able to do that
21
+ eval('alert(\"muahahahaha\")');
22
+ "
23
+ end
24
+
25
+ it "should not apply local options from one file to all subsequent files" do
26
+ lint = JSHint::Lint.new(:paths => ["first.js", "second.js"], :config => 'spec-config.yml')
27
+
28
+ # silence stdout from jslint
29
+ lint.instance_eval do
30
+ def system(command)
31
+ `#{command}`
32
+ $? == 0
33
+ end
34
+ end
35
+
36
+ expect { lint.run }.to raise_error(JSHint::LintCheckFailure)
37
+ end
38
+
39
+ after do
40
+ File.delete('first.js')
41
+ File.delete('second.js')
42
+ File.delete('spec-config.yml')
43
+ FakeFS.activate!
44
+ end
45
+ end
46
+ end
data/spec/lint_spec.rb ADDED
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSHint::Lint do
4
+
5
+ JSHint::Lint.class_eval do
6
+ attr_reader :config, :file_list
7
+ end
8
+
9
+ before :all do
10
+ create_config 'color' => 'red', 'size' => 5, 'shape' => 'circle'
11
+ create_file 'custom_config.yml', 'color' => 'blue', 'size' => 7, 'border' => 2
12
+ create_file 'other_config.yml', 'color' => 'green', 'border' => 0, 'shape' => 'square'
13
+ JSHint.config_path = "custom_config.yml"
14
+ end
15
+
16
+ it "should merge default config with custom config from JSHint.config_path" do
17
+ lint = JSHint::Lint.new
18
+ lint.config.should == { 'color' => 'blue', 'size' => 7, 'border' => 2, 'shape' => 'circle' }
19
+ end
20
+
21
+ it "should merge default config with custom config given in argument, if available" do
22
+ lint = JSHint::Lint.new :config_path => 'other_config.yml'
23
+ lint.config.should == { 'color' => 'green', 'border' => 0, 'shape' => 'square', 'size' => 5 }
24
+ end
25
+
26
+ it "should convert predef to string if it's an array" do
27
+ create_file 'predef.yml', 'predef' => ['a', 'b', 'c']
28
+
29
+ lint = JSHint::Lint.new :config_path => 'predef.yml'
30
+ lint.config['predef'].should == ['a', 'b', 'c']
31
+ end
32
+
33
+ it "should accept predef as string" do
34
+ create_file 'predef.yml', 'predef' => 'd,e,f'
35
+
36
+ lint = JSHint::Lint.new :config_path => 'predef.yml'
37
+ lint.config['predef'].should == ['d', 'e', 'f']
38
+ end
39
+
40
+ it "should not pass paths and exclude_paths options to real JSHint" do
41
+ create_file 'test.yml', 'paths' => ['a', 'b'], 'exclude_paths' => ['c'], 'debug' => 'true'
42
+
43
+ lint = JSHint::Lint.new :config_path => 'test.yml'
44
+ lint.config['debug'].should == 'true'
45
+ lint.config['paths'].should be_nil
46
+ lint.config['exclude_paths'].should be_nil
47
+ end
48
+
49
+ it "should fail if JSHint check fails" do
50
+ lint = JSHint::Lint.new
51
+ lint.instance_variable_set("@file_list", ['app.js'])
52
+ lint.should_receive(:process_file).and_return([{'line'=>0,'character'=>0,'reason'=>'failure'}])
53
+ lambda { lint.run }.should raise_error(JSHint::LintCheckFailure)
54
+ end
55
+
56
+ it "should not fail if JSHint check passes" do
57
+ lint = JSHint::Lint.new
58
+ lint.instance_variable_set("@file_list", ['app.js'])
59
+ lint.should_receive(:process_file).and_return([])
60
+ lambda { lint.run }.should_not raise_error
61
+ end
62
+
63
+ it "should run JSHint once for each file" do
64
+ lint = JSHint::Lint.new
65
+ lint.instance_variable_set("@file_list", ['app.js', 'jquery.js'])
66
+ lint.should_receive(:process_file).twice.and_return([])
67
+ lint.run
68
+ end
69
+
70
+ describe "file lists" do
71
+ before :each do
72
+ JSHint::Utils.stub!(:exclude_files).and_return { |inc, exc| inc - exc }
73
+ JSHint::Utils.stub!(:unique_files).and_return { |files| files.uniq }
74
+ end
75
+
76
+ before :all do
77
+ @files = ['test/app.js', 'test/lib.js', 'test/utils.js', 'test/vendor/jquery.js', 'test/vendor/proto.js']
78
+ @files.each { |fn| create_file(fn, "alert()") }
79
+ @files = @files.map { |fn| File.expand_path(fn) }
80
+ end
81
+
82
+ it "should calculate a list of files to test" do
83
+ lint = JSHint::Lint.new :paths => ['test/**/*.js']
84
+ lint.file_list.should == @files
85
+
86
+ lint = JSHint::Lint.new :paths => ['test/a*.js', 'test/**/*r*.js']
87
+ lint.file_list.should == [@files[0], @files[3], @files[4]]
88
+
89
+ lint = JSHint::Lint.new :paths => ['test/a*.js', 'test/**/*r*.js'], :exclude_paths => ['**/*q*.js']
90
+ lint.file_list.should == [@files[0], @files[4]]
91
+
92
+ lint = JSHint::Lint.new :paths => ['test/**/*.js'], :exclude_paths => ['**/*.js']
93
+ lint.file_list.should == []
94
+
95
+ lint = JSHint::Lint.new :paths => ['test/**/*.js', 'test/**/a*.js', 'test/**/p*.js']
96
+ lint.file_list.should == @files
97
+
98
+ create_file 'new.yml', 'paths' => ['test/vendor/*.js']
99
+
100
+ lint = JSHint::Lint.new :config_path => 'new.yml', :exclude_paths => ['**/proto.js']
101
+ lint.file_list.should == [@files[3]]
102
+
103
+ lint = JSHint::Lint.new :config_path => 'new.yml', :paths => ['test/l*.js']
104
+ lint.file_list.should == [@files[1]]
105
+ end
106
+
107
+ it "should accept :paths and :exclude_paths as string instead of one-element array" do
108
+ lambda do
109
+ lint = JSHint::Lint.new :paths => 'test/*.js', :exclude_paths => 'test/lib.js'
110
+ lint.file_list.should == [@files[0], @files[2]]
111
+ end.should_not raise_error
112
+ end
113
+
114
+ it "should ignore empty files" do
115
+ create_file 'test/empty.js', ''
116
+ create_file 'test/full.js', 'qqq'
117
+
118
+ lint = JSHint::Lint.new :paths => ['test/*.js']
119
+ lint.file_list.should_not include(File.expand_path("test/empty.js"))
120
+ lint.file_list.should include(File.expand_path("test/full.js"))
121
+ end
122
+ end
123
+
124
+ end
@@ -0,0 +1,35 @@
1
+ require 'jshint'
2
+ require 'pp' # fix for fakefs/pp incompatibility in Ruby 1.9.3
3
+ require 'fakefs'
4
+
5
+ module Rails
6
+ end
7
+
8
+ module FakeFS
9
+ class File
10
+ def self.identical?(file1, file2)
11
+ File.expand_path(file1) == File.expand_path(file2)
12
+ end
13
+ end
14
+ end
15
+
16
+ module SpecHelper
17
+ def create_file(filename, contents)
18
+ contents = YAML.dump(contents) + "\n" unless contents.is_a?(String)
19
+ File.open(filename, "w") { |f| f.print(contents) }
20
+ end
21
+
22
+ def create_config(data)
23
+ create_file(JSHint::DEFAULT_CONFIG_FILE, data)
24
+ end
25
+ end
26
+
27
+ RSpec.configure do |config|
28
+ config.include SpecHelper
29
+
30
+ config.before do
31
+ # disable logging to stdout
32
+ JSHint::Utils.stub(:display)
33
+ JSHint::Utils.stub(:log)
34
+ end
35
+ end
@@ -0,0 +1,191 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSHint::Utils do
4
+
5
+ JSU = JSHint::Utils
6
+
7
+ before :all do
8
+ create_config "default config file"
9
+ end
10
+
11
+ describe ".config_path" do
12
+ context "if config path is set explicitly" do
13
+ let(:path) { 'some/path' }
14
+
15
+ before { JSHint.config_path = path }
16
+
17
+ it "should return the path that was set" do
18
+ JSHint.config_path.should == path
19
+ end
20
+ end
21
+
22
+ context "if config path is not set" do
23
+ before { JSHint.config_path = nil }
24
+
25
+ context "if JSHint is run within Rails" do
26
+ before do
27
+ JSHint::Utils.stub(:in_rails? => true)
28
+ Rails.stub(:root => '/dir')
29
+ end
30
+
31
+ it "should return config/jslint.yml in Rails project directory" do
32
+ JSHint.config_path.should == '/dir/config/jslint.yml'
33
+ end
34
+ end
35
+
36
+ context "if JSHint is not run within Rails" do
37
+ before do
38
+ JSHint::Utils.stub(:in_rails? => false)
39
+ end
40
+
41
+ it "should return config/jslint.yml in current directory" do
42
+ JSHint.config_path.should == 'config/jslint.yml'
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ describe "path_from_command_line" do
49
+ it "should extract a path from the command line argument" do
50
+ ENV['test_arg'] = 'file.js'
51
+ JSU.path_from_command_line('test_arg').should == 'file.js'
52
+ end
53
+
54
+ it "should work if the argument name is given in uppercase" do
55
+ ENV['TEST_ARG'] = 'file.js'
56
+ JSU.path_from_command_line('test_arg').should == 'file.js'
57
+ end
58
+
59
+ it "should return nil if the argument isn't set" do
60
+ JSU.path_from_command_line('crash').should be_nil
61
+ end
62
+
63
+ after :each do
64
+ ENV['test_arg'] = nil
65
+ ENV['TEST_ARG'] = nil
66
+ end
67
+ end
68
+
69
+ describe "paths_from_command_line" do
70
+ it "should extract an array of paths from command line argument" do
71
+ ENV['test_arg'] = 'one,two,three'
72
+ JSU.paths_from_command_line('test_arg').should == ['one', 'two', 'three']
73
+ end
74
+
75
+ it "should also work if the argument name is given in uppercase" do
76
+ ENV['TEST_ARG'] = 'ruby,python,js'
77
+ JSU.paths_from_command_line('test_arg').should == ['ruby', 'python', 'js']
78
+ end
79
+
80
+ it "should return nil if the argument isn't set" do
81
+ JSU.paths_from_command_line('crash').should be_nil
82
+ end
83
+
84
+ after :each do
85
+ ENV['test_arg'] = nil
86
+ ENV['TEST_ARG'] = nil
87
+ end
88
+ end
89
+
90
+ describe "load_config_file" do
91
+
92
+ before :all do
93
+ create_file 'sample.yml', 'framework: rails'
94
+ Dir.mkdir("tmp")
95
+ end
96
+
97
+ it "should load a YAML file if it can be read" do
98
+ JSU.load_config_file("sample.yml").should == { 'framework' => 'rails' }
99
+ end
100
+
101
+ it "should return an empty hash if file name is nil" do
102
+ JSU.load_config_file(nil).should == {}
103
+ end
104
+
105
+ it "should return an empty hash if file doesn't exist" do
106
+ JSU.load_config_file("crack.exe").should == {}
107
+ end
108
+
109
+ it "should return an empty hash if file is not a file" do
110
+ JSU.load_config_file("tmp").should == {}
111
+ end
112
+
113
+ it "should return an empty hash if file is not readable" do
114
+ File.should_receive(:readable?).once.with("sample.yml").and_return(false)
115
+ JSU.load_config_file("sample.yml").should == {}
116
+ end
117
+ end
118
+
119
+ describe "unique and exclude" do
120
+ def set_identical(file1, file2, value)
121
+ File.should_receive(:identical?).with(file1, file2).once.and_return(value)
122
+ end
123
+
124
+ it "should remove duplicate files from a list" do
125
+ set_identical('config.yml', 'lib/../config.yml', true)
126
+ set_identical('config.yml', 'Rakefile', false)
127
+ set_identical('config.yml', 'Config.yml', true)
128
+ unique = JSU.unique_files(['config.yml', 'lib/../config.yml', 'Rakefile', 'Config.yml'])
129
+ unique.should == ['config.yml', 'Rakefile']
130
+ end
131
+
132
+ it "should subtract files on the second list from the first list" do
133
+ set_identical('RaKeFiLe', 'config.yml', false)
134
+ set_identical('wtf', 'config.yml', false)
135
+ set_identical('Config.yml', 'config.yml', true)
136
+ set_identical('RaKeFiLe', 'Rakefile', true)
137
+ set_identical('RaKeFiLe', 'Gemfile', false)
138
+ set_identical('wtf', 'Gemfile', false)
139
+ set_identical('Config.yml', 'Gemfile', false)
140
+ included = JSU.exclude_files(['config.yml', 'Rakefile', 'Gemfile'], ['RaKeFiLe', 'wtf', 'Config.yml'])
141
+ included.should == ['Gemfile']
142
+ end
143
+ end
144
+
145
+ describe "copy_config_file" do
146
+ it "should copy default config to config_path" do
147
+ JSHint.config_path = "newfile.yml"
148
+ FileUtils.should_receive(:copy).with(JSHint::DEFAULT_CONFIG_FILE, "newfile.yml")
149
+ JSHint::Utils.copy_config_file
150
+ end
151
+
152
+ it "should not overwrite the file if it exists" do
153
+ JSHint.config_path = "newfile2.yml"
154
+ create_file 'newfile2.yml', 'qwe'
155
+ FileUtils.should_not_receive(:copy)
156
+ JSHint::Utils.copy_config_file
157
+ end
158
+ end
159
+
160
+ describe "remove_config_file" do
161
+ it "should remove the file if it's identical to default one" do
162
+ JSHint.config_path = "newfile3.yml"
163
+ File.open("newfile3.yml", "w") { |f| f.print("default config file") }
164
+ File.exists?("newfile3.yml").should be_true
165
+ JSHint::Utils.remove_config_file
166
+ File.exists?("newfile3.yml").should be_false
167
+ end
168
+
169
+ it "should not remove the file if it's not identical to default one" do
170
+ JSHint.config_path = "newfile4.yml"
171
+ File.open("newfile4.yml", "w") { |f| f.puts("something's changed") }
172
+ File.exists?("newfile4.yml").should be_true
173
+ JSHint::Utils.remove_config_file
174
+ File.exists?("newfile4.yml").should be_true
175
+ end
176
+
177
+ it "should not remove the file if it doesn't exist" do
178
+ JSHint.config_path = "this_doesnt_exist.yml"
179
+ lambda { JSHint::Utils.remove_config_file }.should_not raise_error
180
+ end
181
+
182
+ it "should not remove the file if it's not a file" do
183
+ Dir.mkdir("public")
184
+ JSHint.config_path = "public"
185
+ lambda { JSHint::Utils.remove_config_file }.should_not raise_error
186
+ File.exist?("public").should be_true
187
+ File.directory?("public").should be_true
188
+ end
189
+ end
190
+
191
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jshint-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Gouveia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: execjs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fakefs
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.3.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.3.2
83
+ description: JSHint wrapped in a Ruby gem for easier use
84
+ email: brunogouveia@buzungo.com.br
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - Changelog.markdown
90
+ - Gemfile
91
+ - MIT-LICENSE
92
+ - README.markdown
93
+ - Rakefile
94
+ - lib/jshint.rb
95
+ - lib/jshint/config/jshint.yml
96
+ - lib/jshint/errors.rb
97
+ - lib/jshint/lint.rb
98
+ - lib/jshint/rails.rb
99
+ - lib/jshint/railtie.rb
100
+ - lib/jshint/tasks.rb
101
+ - lib/jshint/utils.rb
102
+ - lib/jshint/vendor/jshint.js
103
+ - lib/jshint_on_rails.rb
104
+ - spec/jslint_spec.rb
105
+ - spec/lint_spec.rb
106
+ - spec/spec_helper.rb
107
+ - spec/utils_spec.rb
108
+ homepage: http://github.com/josephholsten/jshint.rb
109
+ licenses: []
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements:
126
+ - JS engine compatible with execjs
127
+ - JSON engine compatible with multi_json
128
+ rubyforge_project:
129
+ rubygems_version: 2.2.2
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: JSHint is a little more flexible JavaScript checker, wrapped in a Ruby gem
133
+ for easier use
134
+ test_files: []