-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
command.js
89 lines (69 loc) · 3.04 KB
/
command.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
import path from 'node:path';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import {execa, execaSync, execaCommand, execaCommandSync} from '../index.js';
process.env.PATH = fileURLToPath(new URL('fixtures', import.meta.url)) + path.delimiter + process.env.PATH;
const command = async (t, expected, ...args) => {
const {command: failCommand} = await t.throwsAsync(execa('fail.js', args));
t.is(failCommand, `fail.js${expected}`);
const {command} = await execa('noop.js', args);
t.is(command, `noop.js${expected}`);
};
command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;
test(command, ' foo bar', 'foo', 'bar');
test(command, ' baz quz', 'baz', 'quz');
test(command, '');
const testEscapedCommand = async (t, expected, args) => {
const {escapedCommand: failEscapedCommand} = await t.throwsAsync(execa('fail.js', args));
t.is(failEscapedCommand, `fail.js ${expected}`);
const {escapedCommand: failEscapedCommandSync} = t.throws(() => {
execaSync('fail.js', args);
});
t.is(failEscapedCommandSync, `fail.js ${expected}`);
const {escapedCommand} = await execa('noop.js', args);
t.is(escapedCommand, `noop.js ${expected}`);
const {escapedCommand: escapedCommandSync} = execaSync('noop.js', args);
t.is(escapedCommandSync, `noop.js ${expected}`);
};
testEscapedCommand.title = (message, expected) => `escapedCommand is: ${JSON.stringify(expected)}`;
test(testEscapedCommand, 'foo bar', ['foo', 'bar']);
test(testEscapedCommand, '"foo bar"', ['foo bar']);
test(testEscapedCommand, '"\\"foo\\""', ['"foo"']);
test(testEscapedCommand, '"*"', ['*']);
test('allow commands with spaces and no array arguments', async t => {
const {stdout} = await execa('command with space.js');
t.is(stdout, '');
});
test('allow commands with spaces and array arguments', async t => {
const {stdout} = await execa('command with space.js', ['foo', 'bar']);
t.is(stdout, 'foo\nbar');
});
test('execaCommand()', async t => {
const {stdout} = await execaCommand('node test/fixtures/echo.js foo bar');
t.is(stdout, 'foo\nbar');
});
test('execaCommand() ignores consecutive spaces', async t => {
const {stdout} = await execaCommand('node test/fixtures/echo.js foo bar');
t.is(stdout, 'foo\nbar');
});
test('execaCommand() allows escaping spaces in commands', async t => {
const {stdout} = await execaCommand('command\\ with\\ space.js foo bar');
t.is(stdout, 'foo\nbar');
});
test('execaCommand() allows escaping spaces in arguments', async t => {
const {stdout} = await execaCommand('node test/fixtures/echo.js foo\\ bar');
t.is(stdout, 'foo bar');
});
test('execaCommand() escapes other whitespaces', async t => {
const {stdout} = await execaCommand('node test/fixtures/echo.js foo\tbar');
t.is(stdout, 'foo\tbar');
});
test('execaCommand() trims', async t => {
const {stdout} = await execaCommand(' node test/fixtures/echo.js foo bar ');
t.is(stdout, 'foo\nbar');
});
test('execaCommandSync()', t => {
const {stdout} = execaCommandSync('node test/fixtures/echo.js foo bar');
t.is(stdout, 'foo\nbar');
});