-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
301 lines (274 loc) · 7.85 KB
/
test.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2015, 2016 Simon Lydell
// X11 (“MIT”) Licensed. (See LICENSE.)
var fs = require('fs')
var test = require('ava')
var testCli = require('test-cli')
var lib = require('./')
var opened
lib.open = function (url) { opened = url }
var cli = require('./cli')
var run = testCli.bind(null, cli)
function testUrl (t, url) {
t.regexTest(/^https?:\/\/[^/]+\/[^#]+#.{100,}\n?$/, url)
}
function success (t, fn) {
return function (stdout, stderr, code) {
t.is(stderr, '')
t.is(code, 0)
fn(stdout, function (stdout2, stderr2, code2) {
t.is(stdout2, stdout)
t.is(stderr2, stderr)
t.is(code2, code)
})
}
}
function failure (t, fn) {
return function (stdout, stderr, code) {
t.is(stdout, '')
t.is(code, 1)
fn(stderr)
}
}
function okUrlOutput (t) {
return success(t, function (stdout) {
testUrl(t, stdout)
})
}
test('help and no arguments', function (t) {
t.plan(9)
var helpText = fs.readFileSync('help.txt').toString()
run(success(t, function (stdout, identicalOutput) {
t.is(stdout, helpText)
run('--help', identicalOutput)
run('-h', identicalOutput)
}))
})
test('version', function (t) {
t.plan(6)
var version = require('./package.json').version
run('--version', success(t, function (stdout, identicalOutput) {
t.is(stdout, version + '\n')
run('-v', identicalOutput)
}))
})
test('too many arguments', function (t) {
t.plan(3)
run('one', 'two', 'three', failure(t, function (stderr) {
t.is(stderr, 'Too many arguments\n')
}))
})
test('unknown option', function (t) {
t.plan(6)
run('one', '--unknown', 'two', failure(t, function (stderr) {
t.is(stderr, 'Unknown option --unknown\n')
}))
run('-ä', failure(t, function (stderr) {
t.is(stderr, 'Unknown option -ä\n')
}))
})
test('non-existent file', function (t) {
t.plan(3)
run('non-existent.js', failure(t, function (stderr) {
t.regexTest(/no such file.+non-existent\.js.*\n$/, stderr)
}))
})
test('directory as target', function (t) {
t.plan(3)
run('fixtures', failure(t, function (stderr) {
t.regexTest(/directory.*\n$/, stderr)
}))
})
test('non-existent map', function (t) {
t.plan(3)
run(
'fixtures/no-comment.js', 'non-existent.map',
failure(t, function (stderr) {
t.regexTest(/no such file.+non-existent\.map.*\n\n.+/, stderr)
})
)
})
test('directory as map target', function (t) {
t.plan(3)
run('fixtures/no-comment.js', 'fixtures', failure(t, function (stderr) {
t.regexTest(/directory.*\n\n.+/, stderr)
}))
})
test('non-existent source', function (t) {
t.plan(3)
run(
'fixtures/no-comment.js', 'fixtures/non-existent-source.map', '-p',
okUrlOutput(t)
)
})
test('missing sourceMappingURL comment', function (t) {
t.plan(3)
run('fixtures/no-comment.js', failure(t, function (stderr) {
t.is(stderr, 'No sourceMappingURL found in fixtures/no-comment.js\n')
}))
})
test('invalid json', function (t) {
t.plan(4)
run(
'fixtures/no-comment.js', 'fixtures/invalid-json.map',
failure(t, function (stderr) {
t.regexTest(/^Invalid JSON in source map: .+\n\n.+/, stderr)
var jsonText = fs.readFileSync('fixtures/invalid-json.map').toString()
t.is(stderr.slice(-jsonText.length - 1), jsonText + '\n')
})
)
})
test('regular usage and print', function (t) {
t.plan(14)
t.is(opened, undefined)
run('fixtures/comment.js', success(t, function (stdout) {
t.is(stdout, '')
testUrl(t, opened)
run(
'fixtures/comment.js', '--print',
success(t, function (stdout2, identicalOutput) {
t.is(stdout2, opened + '\n')
run('fixtures/comment.js', '-p', identicalOutput)
run('-p', 'fixtures/comment.js', identicalOutput)
})
)
}))
})
test('data uri', function (t) {
t.plan(6)
run(
'fixtures/data-uri.js', '-p',
success(t, function (stdout, identicalOutput) {
testUrl(t, stdout)
run('fixtures/data-uri.js', 'fixtures/data-uri-ed.map', '-p',
identicalOutput)
})
)
})
test('invalid source map as data uri', function (t) {
t.plan(3)
run(
'fixtures/invalid-data-uri.js',
failure(t, function (stderr) {
t.is(stderr, [
'"version" is a required argument.',
'',
'sourceMappingURL: data:application/json;base64,eyJtYXBwaW5ncyI6IkFBQUEiLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W119',
'Source map URL: null',
'Sources fetched relative to: fixtures/invalid-data-uri.js',
'Source map content: {',
' "mappings": "AAAA",',
' "sources": [',
' "foo.js"',
' ],',
' "names": []',
'}'
].join('\n') + '\n')
})
)
})
test('invalid source map as data uri', function (t) {
t.plan(3)
run(
'fixtures/no-comment.js', 'fixtures/missing-mappings.map',
failure(t, function (stderr) {
t.is(stderr, [
'"mappings" is a required argument.',
'',
'sourceMappingURL: null',
'Source map URL: fixtures/missing-mappings.map',
'Sources fetched relative to: fixtures/missing-mappings.map',
'Source map content: {',
' "version": 3,',
' "sources": [',
' "source1.js"',
' ]',
'}'
].join('\n') + '\n')
})
)
})
test('sourceRoot', function (t) {
t.plan(15)
run(
'fixtures/no-comment.js', 'fixtures/source-root.map', '-p',
success(t, function (stdout) {
testUrl(t, stdout)
run(
'fixtures/no-comment.js', 'fixtures/source-root.map', '-p',
'--no-source-root',
success(t, function (stdout2, identicalOutput) {
t.not(stdout2, stdout)
run('fixtures/no-comment.js', 'fixtures/source-root.map', '-p',
'--no-r', identicalOutput)
run('fixtures/no-comment.js', 'fixtures/source-root.map', '-p',
'--source-root', '', identicalOutput)
run('fixtures/no-comment.js', 'fixtures/source-root.map',
'-pr', '.', identicalOutput)
})
)
})
)
})
test('coffee', function (t) {
t.plan(3)
run('fixtures/example.js', '-p', okUrlOutput(t))
})
test('complex', function (t) {
t.plan(3)
run('fixtures/sub/complex.js', '-p', okUrlOutput(t))
})
test('complex – lib', function (t) {
t.plan(8)
lib.resolve(
{generatedFilePath: 'fixtures/sub/complex.js'},
function (error, result) {
t.error(error)
t.is(result.generatedContent, [
'// generated',
'/*# sourceMappingURL=../../fixtures/./complex.map */',
''
].join('\n'))
t.same(result.map, {
version: 3,
mappings: 'AAAA',
sourceRoot: '',
sources: ['source1.js', '../.gitignore',
'http://example.com/virtual.js', './sub/source1.js'],
sourcesContent: [null, null, '// virtual'],
names: []
})
var s = result.sourcesContent
t.is(s.length, 4)
t.is(s[0], '// source1\n')
t.regexTest(/node_modules/, s[1])
t.is(s[2], '// virtual')
t.is(s[3], '// source1-sub\n')
}
)
})
test('displaySourceMap – lib', function (t) {
t.plan(1)
t.is(lib.displaySourceMap({
version: 3,
mappings: ';AAAA;AAAA,MAAA,8BAAA;IAAA;;;EAAM;IACS,gBAAC,IAAD;MAAC,IAAC,CAAA,OAAD;MACZ,IAAC',
sources: ['source1.js', '../.gitignore',
'http://example.com/virtual.js', './sub/source1.js'],
sourcesContent: [null, null, 'function add(first, second) \n { return first + second;\n}\n// comment at the end']
}), [
'{',
' "version": 3,',
' "mappings": ";AAAA;AAAA,MAAA,8BAAA;IAAA;;;EAAM;IACS,gBAAC,IAAD;MAAC,IAAC,CAAA,OA...",',
' "sources": [',
' "source1.js",',
' "../.gitignore",',
' "http://example.com/virtual.js",',
' "./sub/source1.js"',
' ],',
' "sourcesContent": [',
' null,',
' null,',
' "function add(first, second) \\n { return first + second;\\n}\\n// commen..."',
' ]',
'}'
].join('\n'))
})