]> git.immae.eu Git - github/fretlink/pronto-hlint.git/blame - spec/pronto/eslint_spec.rb
Merge pull request #10 from doits/refactor_config_handling
[github/fretlink/pronto-hlint.git] / spec / pronto / eslint_spec.rb
CommitLineData
0ab4d181
MD
1# frozen_string_literal: true
2
9be00a32
MM
3require 'spec_helper'
4
5module Pronto
5001cb27
MD
6 describe ESLintNpm do
7 let(:eslint) { ESLintNpm.new(patches) }
1845f2e3 8 let(:patches) { [] }
9be00a32
MM
9
10 describe '#run' do
2add6782 11 subject(:run) { eslint.run }
9be00a32
MM
12
13 context 'patches are nil' do
14 let(:patches) { nil }
2add6782
MD
15
16 it 'returns an empty array' do
17 expect(run).to eql([])
18 end
9be00a32
MM
19 end
20
21 context 'no patches' do
22 let(:patches) { [] }
2add6782
MD
23
24 it 'returns an empty array' do
25 expect(run).to eql([])
26 end
9be00a32
MM
27 end
28
29509625 29 context 'patches with a one and a four warnings' do
9be00a32
MM
30 include_context 'test repo'
31
32 let(:patches) { repo.diff('master') }
33
2add6782
MD
34 it 'returns correct number of errors' do
35 expect(run.count).to eql(5)
36 end
37
38 it 'has correct first message' do
39 expect(run.first.msg).to eql("'foo' is not defined.")
40 end
1845f2e3
MD
41
42 context(
43 'with files to lint config that never matches',
212da35b 44 config: { 'files_to_lint' => 'will never match' }
1845f2e3
MD
45 ) do
46 it 'returns zero errors' do
47 expect(run.count).to eql(0)
48 end
49 end
50
51 context(
52 'with files to lint config that matches only .js',
fa137a53 53 config: { 'files_to_lint' => '\.js$' }
1845f2e3
MD
54 ) do
55 it 'returns correct amount of errors' do
56 expect(run.count).to eql(2)
57 end
58 end
59
60 context(
61 'with different eslint executable',
212da35b 62 config: { 'eslint_executable' => './custom_eslint.sh' }
1845f2e3
MD
63 ) do
64 it 'calls the custom eslint eslint_executable' do
65 expect { run }.to raise_error(JSON::ParserError, /custom eslint called/)
66 end
67 end
9be00a32 68 end
fb94b0e6 69
29509625 70 context 'repo with ignored and not ignored file, each with three warnings' do
fb94b0e6
MD
71 include_context 'eslintignore repo'
72
73 let(:patches) { repo.diff('master') }
74
2add6782
MD
75 it 'returns correct number of errors' do
76 expect(run.count).to eql(3)
77 end
78
79 it 'has correct first message' do
80 expect(run.first.msg).to eql("'HelloWorld' is defined but never used.")
81 end
fb94b0e6 82 end
9be00a32 83 end
3403f9d1 84
1845f2e3
MD
85 describe '#files_to_lint' do
86 subject(:files_to_lint) { eslint.files_to_lint }
3403f9d1
MD
87
88 it 'matches .js by default' do
89 expect(files_to_lint).to match('my_js.js')
90 end
91
92 it 'matches .es6 by default' do
93 expect(files_to_lint).to match('my_js.es6')
94 end
95 end
96
1845f2e3
MD
97 describe '#eslint_executable' do
98 subject(:eslint_executable) { eslint.eslint_executable }
3403f9d1
MD
99
100 it 'is `eslint` by default' do
101 expect(eslint_executable).to eql('eslint')
102 end
c89a62b8
MD
103
104 context(
105 'with different eslint executable config',
106 config: { 'eslint_executable' => 'custom_eslint' }
107 ) do
108 it 'is correct' do
109 eslint.read_config
110 expect(eslint_executable).to eql('custom_eslint')
111 end
112 end
113 end
114
115 describe '#eslint_command_line' do
116 subject(:eslint_command_line) { eslint.send(:eslint_command_line, path) }
117 let(:path) { '/some/path.rb' }
118
119 it 'adds json output flag' do
120 expect(eslint_command_line).to include('-f json')
121 end
122
123 it 'adds path' do
124 expect(eslint_command_line).to include(path)
125 end
126
127 it 'starts with eslint executable' do
128 expect(eslint_command_line).to start_with(eslint.eslint_executable)
129 end
130
131 context 'with path that should be escaped' do
132 let(:path) { '/must be/$escaped' }
133
134 it 'escapes the path correctly' do
135 expect(eslint_command_line).to include('/must\\ be/\\$escaped')
136 end
137
138 it 'does not include unescaped path' do
139 expect(eslint_command_line).not_to include(path)
140 end
141 end
3403f9d1 142 end
9be00a32
MM
143 end
144end