-
Notifications
You must be signed in to change notification settings - Fork 40
/
lint.cli.spec.js
260 lines (238 loc) · 9.73 KB
/
lint.cli.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* eslint-env mocha */
import { expect } from 'chai'
import { exec } from 'child_process'
describe('format-message lint', () => {
describe('input from stdin', () => {
it('outputs nothing when no errors found', done => {
const input = 'formatMessage("hello")'
exec('bin/format-message lint', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.equal('')
done(err)
}).stdin.end(input, 'utf8')
})
it('reports errors from stdin input to stderr', done => {
const input = 'formatMessage("{")'
exec('bin/format-message lint', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.match(/^SyntaxError\:/)
done(err)
}).stdin.end(input, 'utf8')
})
it('reports errors in translate calls', done => {
const input = 'formatMessage.translate("{")'
exec('bin/format-message lint', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.match(/^SyntaxError\:/)
done(err)
}).stdin.end(input, 'utf8')
})
it('reports filename as "stdin" by default', done => {
const input = 'formatMessage("{")'
exec('bin/format-message lint', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.contain('at formatMessage (stdin:1:0)')
done(err)
}).stdin.end(input, 'utf8')
})
it('reports passed -f filename in errors', done => {
const input = 'formatMessage("{")'
exec('bin/format-message lint -f a-file-name.js', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8'))
.to.contain('at formatMessage (a-file-name.js:1:0)')
done(err)
}).stdin.end(input, 'utf8')
})
it('reports passed --filename filename in errors', done => {
const input = 'formatMessage("{")'
exec('bin/format-message lint --filename b-file-name.js', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8'))
.to.contain('at formatMessage (b-file-name.js:1:0)')
done(err)
}).stdin.end(input, 'utf8')
})
})
describe('colors', () => {
it('outputs in color when specified', done => {
const input = 'formatMessage(top);formatMessage("{")'
exec('bin/format-message lint --color', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.contain('\x1b[')
done(err)
}).stdin.end(input, 'utf8')
})
it('outputs without color when specified', done => {
const input = 'formatMessage(top);formatMessage("{")'
exec('bin/format-message lint --no-color', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.not.contain('\x1b[')
done(err)
}).stdin.end(input, 'utf8')
})
})
describe('function name', () => {
it('finds functions with specified -n name', done => {
const input = '__(top)'
exec('bin/format-message lint -n __', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.equal(
'Warning: called without a literal pattern\n at __ (stdin:1:0)\n'
)
done(err)
}).stdin.end(input, 'utf8')
})
it('finds functions with specified --function-name name', done => {
const input = '__(top)'
exec('bin/format-message lint --function-name __', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.equal(
'Warning: called without a literal pattern\n at __ (stdin:1:0)\n'
)
done(err)
}).stdin.end(input, 'utf8')
})
it('doesn\'t find method calls with the specified name (__)', done => {
const input = 'top.__(top)'
exec('bin/format-message lint -n __', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.equal('')
done(err)
}).stdin.end(input, 'utf8')
})
it('doesn\'t find method calls with the specified name (format)', done => {
const input = 'top.formatMessage("{")'
exec('bin/format-message lint', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.equal('')
done(err)
}).stdin.end(input, 'utf8')
})
})
describe('translations and file input', done => {
it('-k literal', done => {
const cmd = 'bin/format-message lint' +
' -k literal' +
' -t test/translations/lint.literal.json' +
' test/format.spec.js'
exec(cmd, (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
stderr = stderr.toString('utf8')
expect(stderr).to.contain(
'Warning: no en translation found for key "Simple string with nothing special"'
)
expect(stderr).to.contain('Warning: called without a literal pattern')
expect(stderr).to.contain('Warning: called with a non-literal locale')
done(err)
})
})
it('--key-type normalized', done => {
const cmd = 'bin/format-message lint' +
' --key-type normalized' +
' -t test/translations/lint.normalized.json' +
' test/format.spec.js'
exec(cmd, (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
stderr = stderr.toString('utf8')
expect(stderr).to.contain(
'Warning: no en translation found for key "Simple string with nothing special"'
)
expect(stderr).to.contain('Warning: called without a literal pattern')
expect(stderr).to.contain('Warning: called with a non-literal locale')
done(err)
})
})
it('--key-type underscored', done => {
const cmd = 'bin/format-message lint' +
' --key-type underscored' +
' -t test/translations/lint.underscored.json' +
' test/format.spec.js'
exec(cmd, (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
stderr = stderr.toString('utf8')
expect(stderr).to.contain(
'Warning: no en translation found for key "simple_string_with_nothing_special"'
)
expect(stderr).to.contain('Warning: called without a literal pattern')
expect(stderr).to.contain('Warning: called with a non-literal locale')
done(err)
})
})
it('default underscored_crc32', done => {
const cmd = 'bin/format-message lint' +
' -t test/translations/lint.underscored_crc32.json' +
' test/format.spec.js'
exec(cmd, (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
stderr = stderr.toString('utf8')
expect(stderr).to.contain(
'Warning: no en translation found for key "simple_string_with_nothing_special_7eb61d5"'
)
expect(stderr).to.contain('Warning: called without a literal pattern')
expect(stderr).to.contain('Warning: called with a non-literal locale')
done(err)
})
})
})
describe('missing arguments', () => {
it('warns when required arguments object is missing', done => {
const input = 'formatMessage("{a}")'
exec('bin/format-message lint', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.match(/^TypeError\: pattern requires parameters/)
done(err)
}).stdin.end(input, 'utf8')
})
it('warns when required argument is missing from literal object', done => {
const input = 'formatMessage("{a}", { b:1 })'
exec('bin/format-message lint', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.match(/^TypeError\: pattern requires parameter/)
done(err)
}).stdin.end(input, 'utf8')
})
})
describe('autodetect function name', () => {
it('finds function name from require call', done => {
const input = 'var f=require("format-message");f("{")'
exec('bin/format-message lint', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.match(/^SyntaxError\:/)
done(err)
}).stdin.end(input, 'utf8')
})
it('handles multiple function names in function context', done => {
const input = `
function foo(){
var f=require("format-message");
f("{")
}
function bar(){
formatMessage(foo)
}`
exec('bin/format-message lint', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.contain('SyntaxError')
expect(stderr.toString('utf8')).to.contain('Warning')
done(err)
}).stdin.end(input, 'utf8')
})
it('finds function name from import', done => {
const input = 'import __ from "format-message";__("{")'
exec('bin/format-message lint', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.match(/^SyntaxError\:/)
done(err)
}).stdin.end(input, 'utf8')
})
it('is disabled by --no-auto', done => {
const input = 'import __ from "format-message";__("{")'
exec('bin/format-message lint --no-auto', (err, stdout, stderr) => {
expect(stdout.toString('utf8')).to.equal('')
expect(stderr.toString('utf8')).to.equal('')
done(err)
}).stdin.end(input, 'utf8')
})
})
})