forked from google/zx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.test.js
206 lines (175 loc) · 6.45 KB
/
cli.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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import assert from 'node:assert'
import { test, describe, before, beforeEach } from 'node:test'
import '../build/globals.js'
describe('cli', () => {
// Helps detect unresolved ProcessPromise.
let promiseResolved = false
beforeEach(() => {
$.verbose = false
process.on('exit', () => {
if (!promiseResolved) {
console.error('Error: ProcessPromise never resolved.')
process.exitCode = 1
}
})
})
test('promise resolved', async () => {
await $`echo`
promiseResolved = true
})
test('prints version', async () => {
assert.match((await $`node build/cli.js -v`).toString(), /\d+.\d+.\d+/)
})
test('prints help', async () => {
let p = $`node build/cli.js -h`
p.stdin.end()
let help = await p
assert.match(help.stdout, /zx/)
})
test('zx prints usage', async () => {
let p = $`node build/cli.js`
p.stdin.end()
let out = await p
assert.match(out.stdout, /A tool for writing better scripts/)
})
test('starts repl with --repl', async () => {
let p = $`node build/cli.js --repl`
p.stdin.write('await $`echo f"o"o`\n')
p.stdin.write('"b"+"ar"\n')
p.stdin.end()
let out = await p
assert.match(out.stdout, /foo/)
assert.match(out.stdout, /bar/)
})
test('starts repl with verbosity off', async () => {
let p = $`node build/cli.js --repl`
p.stdin.write('"verbose" + " is " + $.verbose\n')
p.stdin.end()
let out = await p
assert.match(out.stdout, /verbose is false/)
})
test('supports `--experimental` flag', async () => {
let out = await $`echo 'echo("test")' | node build/cli.js --experimental`
assert.match(out.stdout, /test/)
})
test('supports `--quiet` flag', async () => {
let p = await $`node build/cli.js test/fixtures/markdown.md`
assert.ok(!p.stderr.includes('ignore'), 'ignore was printed')
assert.ok(p.stderr.includes('hello'), 'no hello')
assert.ok(p.stdout.includes('world'), 'no world')
})
test('supports `--shell` flag ', async () => {
let shell = $.shell
let p =
await $`node build/cli.js --shell=${shell} <<< '$\`echo \${$.shell}\`'`
assert.ok(p.stderr.includes(shell))
})
test('supports `--prefix` flag ', async () => {
let prefix = 'set -e;'
let p =
await $`node build/cli.js --prefix=${prefix} <<< '$\`echo \${$.prefix}\`'`
assert.ok(p.stderr.includes(prefix))
})
test('scripts from https', async () => {
$`cat ${path.resolve('test/fixtures/echo.http')} | nc -l 8080`
let out = await $`node build/cli.js http://127.0.0.1:8080/echo.mjs`
assert.match(out.stderr, /test/)
})
test('scripts from https not ok', async () => {
$`echo $'HTTP/1.1 500\n\n' | nc -l 8081`
let out = await $`node build/cli.js http://127.0.0.1:8081`.nothrow()
assert.match(out.stderr, /Error: Can't get/)
})
test('scripts with no extension', async () => {
await $`node build/cli.js test/fixtures/no-extension`
assert.ok(
/Test file to verify no-extension didn't overwrite similarly name .mjs file./.test(
(await fs.readFile('test/fixtures/no-extension.mjs')).toString()
)
)
})
test('require() is working from stdin', async () => {
let out =
await $`node build/cli.js <<< 'console.log(require("./package.json").name)'`
assert.match(out.stdout, /zx/)
})
test('require() is working in ESM', async () => {
await $`node build/cli.js test/fixtures/require.mjs`
})
test('__filename & __dirname are defined', async () => {
await $`node build/cli.js test/fixtures/filename-dirname.mjs`
})
test('markdown scripts are working', async () => {
await $`node build/cli.js test/fixtures/markdown.md`
})
test('markdown scripts are working', async () => {
await $`node build/cli.js test/fixtures/markdown.md`
})
test('exceptions are caught', async () => {
let out1 = await $`node build/cli.js <<<${'await $`wtf`'}`.nothrow()
assert.match(out1.stderr, /Error:/)
let out2 = await $`node build/cli.js <<<'throw 42'`.nothrow()
assert.match(out2.stderr, /42/)
})
test('eval works', async () => {
assert.equal((await $`node build/cli.js --eval 'echo(42)'`).stdout, '42\n')
assert.equal((await $`node build/cli.js -e='echo(69)'`).stdout, '69\n')
})
test('eval works with stdin', async () => {
let p = $`(printf foo; sleep 0.1; printf bar) | node build/cli.js --eval 'echo(await stdin())'`
assert.equal((await p).stdout, 'foobar\n')
})
test('executes a script from $PATH', async () => {
const isWindows = process.platform === 'win32'
const oldPath = process.env.PATH
const envPathSeparator = isWindows ? ';' : ':'
process.env.PATH += envPathSeparator + path.resolve('/tmp/')
const toPOSIXPath = (_path) => _path.split(path.sep).join(path.posix.sep)
const zxPath = path.resolve('./build/cli.js')
const zxLocation = isWindows ? toPOSIXPath(zxPath) : zxPath
const scriptCode = `#!/usr/bin/env ${zxLocation}\nconsole.log('The script from path runs.')`
try {
await $`chmod +x ${zxLocation}`
await $`echo ${scriptCode}`.pipe(
fs.createWriteStream('/tmp/script-from-path', { mode: 0o744 })
)
await $`script-from-path`
} finally {
process.env.PATH = oldPath
fs.rmSync('/tmp/script-from-path')
}
})
test('argv works with zx and node', async () => {
assert.equal(
(await $`node build/cli.js test/fixtures/argv.mjs foo`).toString(),
`global {"_":["foo"]}\nimported {"_":["foo"]}\n`
)
assert.equal(
(await $`node test/fixtures/argv.mjs bar`).toString(),
`global {"_":["bar"]}\nimported {"_":["bar"]}\n`
)
assert.equal(
(
await $`node build/cli.js --eval 'console.log(argv._.join(''))' baz`
).toString(),
`baz\n`
)
})
test('exit code can be set', async () => {
let p = await $`node build/cli.js test/fixtures/exit-code.mjs`.nothrow()
assert.equal(p.exitCode, 42)
})
})