]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/test/tap/run-script.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / test / tap / run-script.js
1 var fs = require('graceful-fs')
2 var path = require('path')
3
4 var mkdirp = require('mkdirp')
5 var test = require('tap').test
6 var rimraf = require('rimraf')
7
8 var common = require('../common-tap')
9
10 var pkg = path.resolve(__dirname, 'run-script')
11 var cache = path.resolve(pkg, 'cache')
12 var tmp = path.resolve(pkg, 'tmp')
13
14 var opts = { cwd: pkg }
15
16 var fullyPopulated = {
17   'name': 'runscript',
18   'version': '1.2.3',
19   'scripts': {
20     'start': 'node -e "console.log(process.argv[1] || \'start\')"',
21     'prewith-pre': 'node -e "console.log(process.argv[1] || \'pre\')"',
22     'with-pre': 'node -e "console.log(process.argv[1] || \'main\')"',
23     'with-post': 'node -e "console.log(process.argv[1] || \'main\')"',
24     'postwith-post': 'node -e "console.log(process.argv[1] || \'post\')"',
25     'prewith-both': 'node -e "console.log(process.argv[1] || \'pre\')"',
26     'with-both': 'node -e "console.log(process.argv[1] || \'main\')"',
27     'postwith-both': 'node -e "console.log(process.argv[1] || \'post\')"',
28     'stop': 'node -e "console.log(process.argv[1] || \'stop\')"',
29     'env-vars': 'node -e "console.log(process.env.run_script_foo_var)"',
30     'npm-env-vars': 'node -e "console.log(process.env.npm_run_script_foo_var)"',
31     'package-env-vars': 'node -e "console.log(process.env.run_script_foo_var)"',
32     'prefixed-package-env-vars': 'node -e "console.log(process.env.npm_package_run_script_foo_var)"'
33   },
34   'run_script_foo_var': 'run_script_test_foo_val'
35 }
36
37 var lifecycleOnly = {
38   name: 'scripted',
39   version: '1.2.3',
40   scripts: {
41     'prestart': 'echo prestart'
42   }
43 }
44
45 var directOnly = {
46   name: 'scripted',
47   version: '1.2.3',
48   scripts: {
49     'whoa': 'echo whoa'
50   }
51 }
52
53 var both = {
54   name: 'scripted',
55   version: '1.2.3',
56   scripts: {
57     'prestart': 'echo prestart',
58     'whoa': 'echo whoa'
59   }
60 }
61
62 var preversionOnly = {
63   name: 'scripted',
64   version: '1.2.3',
65   scripts: {
66     'preversion': 'echo preversion'
67   }
68 }
69
70
71 function testOutput (t, command, er, code, stdout, stderr) {
72   var lines
73
74   if (er)
75     throw er
76
77   if (stderr)
78     throw new Error('npm ' + command + ' stderr: ' + stderr.toString())
79
80   lines = stdout.trim().split('\n')
81   stdout = lines.filter(function (line) {
82     return line.trim() !== '' && line[0] !== '>'
83   }).join(';')
84
85   t.equal(stdout, command)
86   t.end()
87 }
88
89 function writeMetadata (object) {
90   fs.writeFileSync(
91     path.resolve(pkg, 'package.json'),
92     JSON.stringify(object, null, 2) + '\n'
93   )
94 }
95
96 function cleanup () {
97   rimraf.sync(pkg)
98 }
99
100 test('setup', function (t) {
101   cleanup()
102   mkdirp.sync(cache)
103   mkdirp.sync(tmp)
104   writeMetadata(fullyPopulated)
105   t.end()
106 })
107
108 test('npm run-script start', function (t) {
109   common.npm(['run-script', 'start'], opts, testOutput.bind(null, t, 'start'))
110 })
111
112 test('npm run-script with args', function (t) {
113   common.npm(['run-script', 'start', '--', 'stop'], opts, testOutput.bind(null, t, 'stop'))
114 })
115
116 test('npm run-script with args that contain spaces', function (t) {
117   common.npm(['run-script', 'start', '--', 'hello world'], opts, testOutput.bind(null, t, 'hello world'))
118 })
119
120 test('npm run-script with args that contain single quotes', function (t) {
121   common.npm(['run-script', 'start', '--', 'they"re awesome'], opts, testOutput.bind(null, t, 'they"re awesome'))
122 })
123
124 test('npm run-script with args that contain double quotes', function (t) {
125   common.npm(['run-script', 'start', '--', 'what"s "up"?'], opts, testOutput.bind(null, t, 'what"s "up"?'))
126 })
127
128 test('npm run-script with args that contain ticks', function (t) {
129   common.npm(['run-script', 'start', '--', 'what\'s \'up\'?'], opts, testOutput.bind(null, t, 'what\'s \'up\'?'))
130 })
131
132 test('npm run-script with pre script', function (t) {
133   common.npm(['run-script', 'with-post'], opts, testOutput.bind(null, t, 'main;post'))
134 })
135
136 test('npm run-script with post script', function (t) {
137   common.npm(['run-script', 'with-pre'], opts, testOutput.bind(null, t, 'pre;main'))
138 })
139
140 test('npm run-script with both pre and post script', function (t) {
141   common.npm(['run-script', 'with-both'], opts, testOutput.bind(null, t, 'pre;main;post'))
142 })
143
144 test('npm run-script with both pre and post script and with args', function (t) {
145   common.npm(['run-script', 'with-both', '--', 'an arg'], opts, testOutput.bind(null, t, 'pre;an arg;post'))
146 })
147
148 test('npm run-script explicitly call pre script with arg', function (t) {
149   common.npm(['run-script', 'prewith-pre', '--', 'an arg'], opts, testOutput.bind(null, t, 'an arg'))
150 })
151
152 test('npm run-script test', function (t) {
153   common.npm(['run-script', 'test'], opts, function (er, code, stdout, stderr) {
154     t.ifError(er, 'npm run-script test ran without issue')
155     t.notOk(stderr, 'should not generate errors')
156     t.end()
157   })
158 })
159
160 test('npm run-script env', function (t) {
161   common.npm(['run-script', 'env'], opts, function (er, code, stdout, stderr) {
162     t.ifError(er, 'using default env script')
163     t.notOk(stderr, 'should not generate errors')
164     t.ok(stdout.indexOf('npm_config_init_version') > 0, 'expected values in var list')
165     t.end()
166   })
167 })
168
169 test('npm run-script nonexistent-script', function (t) {
170   common.npm(['run-script', 'nonexistent-script'], opts, function (er, code, stdout, stderr) {
171     t.ifError(er, 'npm run-script nonexistent-script did not cause npm to explode')
172     t.ok(stderr, 'should generate errors')
173     t.end()
174   })
175 })
176
177 test('npm run-script restart when there isn\'t restart', function (t) {
178   common.npm(['run-script', 'restart'], opts, testOutput.bind(null, t, 'stop;start'))
179 })
180
181 test('npm run-script nonexistent-script with --if-present flag', function (t) {
182   common.npm(['run-script', '--if-present', 'nonexistent-script'], opts, function (er, code, stdout, stderr) {
183     t.ifError(er, 'npm run-script --if-present non-existent-script ran without issue')
184     t.notOk(stderr, 'should not generate errors')
185     t.end()
186   })
187 })
188
189 test('npm run-script env vars accessible', function (t) {
190   process.env.run_script_foo_var = 'run_script_test_foo_val'
191   common.npm(['run-script', 'env-vars'], {
192     cwd: pkg
193   }, function (err, code, stdout, stderr) {
194     t.ifError(err, 'ran run-script without crashing')
195     t.equal(code, 0, 'exited normally')
196     t.equal(stderr, '', 'no error output')
197     t.match(stdout,
198       new RegExp(process.env.run_script_foo_var),
199       'script had env access')
200     t.end()
201   })
202 })
203
204 test('npm run-script package.json vars injected', function (t) {
205   common.npm(['run-script', 'package-env-vars'], {
206     cwd: pkg
207   }, function (err, code, stdout, stderr) {
208     t.ifError(err, 'ran run-script without crashing')
209     t.equal(code, 0, 'exited normally')
210     t.equal(stderr, '', 'no error output')
211     t.match(stdout,
212       new RegExp(fullyPopulated.run_script_foo_var),
213       'script injected package.json value')
214     t.end()
215   })
216 })
217
218 test('npm run-script package.json vars injected with prefix', function (t) {
219   common.npm(['run-script', 'prefixed-package-env-vars'], {
220     cwd: pkg
221   }, function (err, code, stdout, stderr) {
222     t.ifError(err, 'ran run-script without crashing')
223     t.equal(code, 0, 'exited normally')
224     t.equal(stderr, '', 'no error output')
225     t.match(stdout,
226       new RegExp(fullyPopulated.run_script_foo_var),
227       'script injected npm_package-prefixed package.json value')
228     t.end()
229   })
230 })
231
232 test('npm run-script env vars stripped npm-prefixed', function (t) {
233   process.env.npm_run_script_foo_var = 'run_script_test_foo_val'
234   common.npm(['run-script', 'npm-env-vars'], {
235     cwd: pkg
236   }, function (err, code, stdout, stderr) {
237     t.ifError(err, 'ran run-script without crashing')
238     t.equal(code, 0, 'exited normally')
239     t.equal(stderr, '', 'no error output')
240     t.notMatch(stdout,
241       new RegExp(process.env.npm_run_script_foo_var),
242       'script stripped npm-prefixed env var')
243     t.end()
244   })
245 })
246
247 test('npm run-script no-params (lifecycle only)', function (t) {
248   var expected = [
249     'Lifecycle scripts included in scripted:',
250     '  prestart',
251     '    echo prestart',
252     ''
253   ].join('\n')
254
255   writeMetadata(lifecycleOnly)
256
257   common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
258     t.ifError(err, 'ran run-script without parameters without crashing')
259     t.notOk(code, 'npm exited without error code')
260     t.notOk(stderr, 'npm printed nothing to stderr')
261     t.equal(stdout, expected, 'got expected output')
262     t.end()
263   })
264 })
265
266 test('npm run-script no-params (preversion only)', function (t) {
267   var expected = [
268     'Lifecycle scripts included in scripted:',
269     '  preversion',
270     '    echo preversion',
271     ''
272   ].join('\n')
273
274   writeMetadata(preversionOnly)
275
276   common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
277     t.ifError(err, 'ran run-script without parameters without crashing')
278     t.notOk(code, 'npm exited without error code')
279     t.notOk(stderr, 'npm printed nothing to stderr')
280     t.equal(stdout, expected, 'got expected output')
281     t.end()
282   })
283 })
284
285 test('npm run-script no-params (direct only)', function (t) {
286   var expected = [
287     'Scripts available in scripted via `npm run-script`:',
288     '  whoa',
289     '    echo whoa',
290     ''
291   ].join('\n')
292
293   writeMetadata(directOnly)
294
295   common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
296     t.ifError(err, 'ran run-script without parameters without crashing')
297     t.notOk(code, 'npm exited without error code')
298     t.notOk(stderr, 'npm printed nothing to stderr')
299     t.equal(stdout, expected, 'got expected output')
300     t.end()
301   })
302 })
303
304 test('npm run-script no-params (direct only)', function (t) {
305   var expected = [
306     'Lifecycle scripts included in scripted:',
307     '  prestart',
308     '    echo prestart',
309     '',
310     'available via `npm run-script`:',
311     '  whoa',
312     '    echo whoa',
313     ''
314   ].join('\n')
315
316   writeMetadata(both)
317
318   common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
319     t.ifError(err, 'ran run-script without parameters without crashing')
320     t.notOk(code, 'npm exited without error code')
321     t.notOk(stderr, 'npm printed nothing to stderr')
322     t.equal(stdout, expected, 'got expected output')
323     t.end()
324   })
325 })
326
327 test('cleanup', function (t) {
328   cleanup()
329   t.end()
330 })