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
|
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')
var test = require('tap').test
var rimraf = require('rimraf')
var common = require('../common-tap')
var pkg = path.resolve(__dirname, 'run-script')
var cache = path.resolve(pkg, 'cache')
var tmp = path.resolve(pkg, 'tmp')
var opts = { cwd: pkg }
function testOutput (t, command, er, code, stdout, stderr) {
var lines
if (er)
throw er
if (stderr)
throw new Error('npm ' + command + ' stderr: ' + stderr.toString())
lines = stdout.trim().split('\n')
stdout = lines.filter(function (line) {
return line.trim() !== '' && line[0] !== '>'
}).join(';')
t.equal(stdout, command)
t.end()
}
function writeMetadata (object) {
fs.writeFileSync(
path.resolve(pkg, 'package.json'),
JSON.stringify(object, null, 2) + '\n'
)
}
function cleanup () {
rimraf.sync(pkg)
}
test('setup', function (t) {
cleanup()
mkdirp.sync(cache)
mkdirp.sync(tmp)
writeMetadata(fullyPopulated)
t.end()
})
var fullyPopulated = {
'name': 'runscript',
'version': '1.2.3',
'scripts': {
'start': 'node -e "console.log(process.argv[1] || \'start\')"',
'prewith-pre': 'node -e "console.log(process.argv[1] || \'pre\')"',
'with-pre': 'node -e "console.log(process.argv[1] || \'main\')"',
'with-post': 'node -e "console.log(process.argv[1] || \'main\')"',
'postwith-post': 'node -e "console.log(process.argv[1] || \'post\')"',
'prewith-both': 'node -e "console.log(process.argv[1] || \'pre\')"',
'with-both': 'node -e "console.log(process.argv[1] || \'main\')"',
'postwith-both': 'node -e "console.log(process.argv[1] || \'post\')"',
'stop': 'node -e "console.log(process.argv[1] || \'stop\')"'
}
}
var lifecycleOnly = {
name: 'scripted',
version: '1.2.3',
scripts: {
'prestart': 'echo prestart'
}
}
var directOnly = {
name: 'scripted',
version: '1.2.3',
scripts: {
'whoa': 'echo whoa'
}
}
var both = {
name: 'scripted',
version: '1.2.3',
scripts: {
'prestart': 'echo prestart',
'whoa': 'echo whoa'
}
}
test('npm run-script start', function (t) {
common.npm(['run-script', 'start'], opts, testOutput.bind(null, t, 'start'))
})
test('npm run-script with args', function (t) {
common.npm(['run-script', 'start', '--', 'stop'], opts, testOutput.bind(null, t, 'stop'))
})
test('npm run-script with args that contain spaces', function (t) {
common.npm(['run-script', 'start', '--', 'hello world'], opts, testOutput.bind(null, t, 'hello world'))
})
test('npm run-script with args that contain single quotes', function (t) {
common.npm(['run-script', 'start', '--', 'they"re awesome'], opts, testOutput.bind(null, t, 'they"re awesome'))
})
test('npm run-script with args that contain double quotes', function (t) {
common.npm(['run-script', 'start', '--', 'what"s "up"?'], opts, testOutput.bind(null, t, 'what"s "up"?'))
})
test('npm run-script with pre script', function (t) {
common.npm(['run-script', 'with-post'], opts, testOutput.bind(null, t, 'main;post'))
})
test('npm run-script with post script', function (t) {
common.npm(['run-script', 'with-pre'], opts, testOutput.bind(null, t, 'pre;main'))
})
test('npm run-script with both pre and post script', function (t) {
common.npm(['run-script', 'with-both'], opts, testOutput.bind(null, t, 'pre;main;post'))
})
test('npm run-script with both pre and post script and with args', function (t) {
common.npm(['run-script', 'with-both', '--', 'an arg'], opts, testOutput.bind(null, t, 'pre;an arg;post'))
})
test('npm run-script explicitly call pre script with arg', function (t) {
common.npm(['run-script', 'prewith-pre', '--', 'an arg'], opts, testOutput.bind(null, t, 'an arg'))
})
test('npm run-script test', function (t) {
common.npm(['run-script', 'test'], opts, function (er, code, stdout, stderr) {
t.ifError(er, 'npm run-script test ran without issue')
t.notOk(stderr, 'should not generate errors')
t.end()
})
})
test('npm run-script env', function (t) {
common.npm(['run-script', 'env'], opts, function (er, code, stdout, stderr) {
t.ifError(er, 'using default env script')
t.notOk(stderr, 'should not generate errors')
t.ok(stdout.indexOf('npm_config_init_version') > 0, 'expected values in var list')
t.end()
})
})
test('npm run-script nonexistent-script', function (t) {
common.npm(['run-script', 'nonexistent-script'], opts, function (er, code, stdout, stderr) {
t.ifError(er, 'npm run-script nonexistent-script did not cause npm to explode')
t.ok(stderr, 'should generate errors')
t.end()
})
})
test('npm run-script restart when there isn"t restart', function (t) {
common.npm(['run-script', 'restart'], opts, testOutput.bind(null, t, 'stop;start'))
})
test('npm run-script no-params (lifecycle only)', function (t) {
var expected = [
'Lifecycle scripts included in scripted:',
' prestart',
' echo prestart',
''
].join('\n')
writeMetadata(lifecycleOnly)
common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
t.ifError(err, 'ran run-script without parameters without crashing')
t.notOk(code, 'npm exited without error code')
t.notOk(stderr, 'npm printed nothing to stderr')
t.equal(stdout, expected, 'got expected output')
t.end()
})
})
test('npm run-script no-params (direct only)', function (t) {
var expected = [
'Scripts available in scripted via `npm run-script`:',
' whoa',
' echo whoa',
''
].join('\n')
writeMetadata(directOnly)
common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
t.ifError(err, 'ran run-script without parameters without crashing')
t.notOk(code, 'npm exited without error code')
t.notOk(stderr, 'npm printed nothing to stderr')
t.equal(stdout, expected, 'got expected output')
t.end()
})
})
test('npm run-script no-params (direct only)', function (t) {
var expected = [
'Lifecycle scripts included in scripted:',
' prestart',
' echo prestart',
'',
'available via `npm run-script`:',
' whoa',
' echo whoa',
''
].join('\n')
writeMetadata(both)
common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
t.ifError(err, 'ran run-script without parameters without crashing')
t.notOk(code, 'npm exited without error code')
t.notOk(stderr, 'npm printed nothing to stderr')
t.equal(stdout, expected, 'got expected output')
t.end()
})
})
test('cleanup', function (t) {
cleanup()
t.end()
})
|