Skip to content
Myr

command.js

This file

Path

test/methods/command.js

110 lines · 4.7 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 08eb743 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–3why, one step back
1import assert from 'node:assert/strict';
2import test from 'node:test';3import {assertThrows} from '../helpers/assert.js';

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 4–14why, one step back
4import {
5 execa,6 execaSync,7 $,8 execaNode,9 parseCommandString,10} from '../../index.js';11import {12 setFixtureDirectory,13 FIXTURES_DIRECTORY_URL,14} from '../helpers/fixtures-directory.js';

Upgrade `tsd` and `c8` (#982)ehmickybefore adoption08eb743

lines 15–16why, one step back
15
16setFixtureDirectory();

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 17why, one step back
17const ECHO_FIXTURE_URL = new URL('echo.js', FIXTURES_DIRECTORY_URL);

Upgrade `tsd` and `c8` (#982)ehmickybefore adoption08eb743

line 18why, one step back
18

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 19–20why, one step back
19const parseAndRunCommand = command => execa`${parseCommandString(command)}`;
20

TweaksSindre Sorhusbefore adoption698a1b8

line 21why, one step back
21test('parseCommandString() + execa()', async () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 22why, one step back
22 const {stdout} = await execa('echo.js', parseCommandString('foo bar'));

TweaksSindre Sorhusbefore adoption698a1b8

line 23why, one step back
23 assert.equal(stdout, 'foo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 24–25why, one step back
24});
25

TweaksSindre Sorhusbefore adoption698a1b8

line 26why, one step back
26test('parseCommandString() + execaSync()', () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 27why, one step back
27 const {stdout} = execaSync('echo.js', parseCommandString('foo bar'));

TweaksSindre Sorhusbefore adoption698a1b8

line 28why, one step back
28 assert.equal(stdout, 'foo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 29–30why, one step back
29});
30

TweaksSindre Sorhusbefore adoption698a1b8

line 31why, one step back
31test('parseCommandString() + execa`...`', async () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 32why, one step back
32 const {stdout} = await execa`${parseCommandString('echo.js foo bar')}`;

TweaksSindre Sorhusbefore adoption698a1b8

line 33why, one step back
33 assert.equal(stdout, 'foo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 34–35why, one step back
34});
35

TweaksSindre Sorhusbefore adoption698a1b8

line 36why, one step back
36test('parseCommandString() + execa`...`, only arguments', async () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 37why, one step back
37 const {stdout} = await execa`echo.js ${parseCommandString('foo bar')}`;

TweaksSindre Sorhusbefore adoption698a1b8

line 38why, one step back
38 assert.equal(stdout, 'foo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 39–40why, one step back
39});
40

TweaksSindre Sorhusbefore adoption698a1b8

line 41why, one step back
41test('parseCommandString() + execa`...`, only some arguments', async () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 42why, one step back
42 const {stdout} = await execa`echo.js ${'foo bar'} ${parseCommandString('foo bar')}`;

TweaksSindre Sorhusbefore adoption698a1b8

line 43why, one step back
43 assert.equal(stdout, 'foo bar\nfoo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 44–45why, one step back
44});
45

TweaksSindre Sorhusbefore adoption698a1b8

line 46why, one step back
46test('parseCommandString() + execaSync`...`', () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 47why, one step back
47 const {stdout} = execaSync`${parseCommandString('echo.js foo bar')}`;

TweaksSindre Sorhusbefore adoption698a1b8

line 48why, one step back
48 assert.equal(stdout, 'foo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 49–50why, one step back
49});
50

TweaksSindre Sorhusbefore adoption698a1b8

line 51why, one step back
51test('parseCommandString() + execaSync`...`, only arguments', () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 52why, one step back
52 const {stdout} = execaSync`echo.js ${parseCommandString('foo bar')}`;

TweaksSindre Sorhusbefore adoption698a1b8

line 53why, one step back
53 assert.equal(stdout, 'foo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 54–55why, one step back
54});
55

TweaksSindre Sorhusbefore adoption698a1b8

line 56why, one step back
56test('parseCommandString() + execaSync`...`, only some arguments', () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 57why, one step back
57 const {stdout} = execaSync`echo.js ${'foo bar'} ${parseCommandString('foo bar')}`;

TweaksSindre Sorhusbefore adoption698a1b8

line 58why, one step back
58 assert.equal(stdout, 'foo bar\nfoo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 59–60why, one step back
59});
60

TweaksSindre Sorhusbefore adoption698a1b8

line 61why, one step back
61test('parseCommandString() + $', async () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 62why, one step back
62 const {stdout} = await $`${parseCommandString('echo.js foo bar')}`;

TweaksSindre Sorhusbefore adoption698a1b8

line 63why, one step back
63 assert.equal(stdout, 'foo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 64–65why, one step back
64});
65

TweaksSindre Sorhusbefore adoption698a1b8

line 66why, one step back
66test('parseCommandString() + $.sync', () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 67why, one step back
67 const {stdout} = $.sync`${parseCommandString('echo.js foo bar')}`;

TweaksSindre Sorhusbefore adoption698a1b8

line 68why, one step back
68 assert.equal(stdout, 'foo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 69–70why, one step back
69});
70

TweaksSindre Sorhusbefore adoption698a1b8

line 71why, one step back
71test('parseCommandString() + execaNode', async () => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 72why, one step back
72 const {stdout} = await execaNode(ECHO_FIXTURE_URL, parseCommandString('foo bar'));

TweaksSindre Sorhusbefore adoption698a1b8

line 73why, one step back
73 assert.equal(stdout, 'foo\nbar');

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 74–75why, one step back
74});
75

TweaksSindre Sorhusbefore adoption698a1b8

lines 76–77why, one step back
76const testInvalidArgumentsParse = command => {
77 assertThrows(() => parseCommandString(command), {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 78–81why, one step back
78 message: /The command must be a string/,
79 });80};81

TweaksSindre Sorhusbefore adoption698a1b8

lines 82–87why, one step back
82test('parseCommandString() must not pass a number', () => testInvalidArgumentsParse(0));
83test('parseCommandString() must not pass undefined', () => testInvalidArgumentsParse(undefined));84test('parseCommandString() must not pass null', () => testInvalidArgumentsParse(null));85test('parseCommandString() must not pass a symbol', () => testInvalidArgumentsParse(Symbol('test')));86test('parseCommandString() must not pass an object', () => testInvalidArgumentsParse({}));87test('parseCommandString() must not pass an array', () => testInvalidArgumentsParse([]));

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 88why, one step back
88

TweaksSindre Sorhusbefore adoption698a1b8

line 89why, one step back
89const testParseCommandOutput = async (command, expectedOutput, execaMethod) => {

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 90why, one step back
90 const {stdout} = await execaMethod(command);

TweaksSindre Sorhusbefore adoption698a1b8

line 91why, one step back
91 assert.equal(stdout, expectedOutput);

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 92–93why, one step back
92};
93

TweaksSindre Sorhusbefore adoption698a1b8

lines 94–101why, one step back
94test('parseCommandString() allows escaping spaces in commands', () => testParseCommandOutput('command\\ with\\ space.js foo bar', 'foo\nbar', parseAndRunCommand));
95test('parseCommandString() trims', () => testParseCommandOutput(' echo.js foo bar ', 'foo\nbar', parseAndRunCommand));96test('parseCommandString() ignores consecutive spaces', () => testParseCommandOutput('echo.js foo bar', 'foo\nbar', parseAndRunCommand));97test('parseCommandString() escapes other whitespaces', () => testParseCommandOutput('echo.js foo\tbar', 'foo\tbar', parseAndRunCommand));98test('parseCommandString() allows escaping spaces', () => testParseCommandOutput('echo.js foo\\ bar', 'foo bar', parseAndRunCommand));99test('parseCommandString() allows escaping backslashes before spaces', () => testParseCommandOutput('echo.js foo\\\\ bar', 'foo\\ bar', parseAndRunCommand));100test('parseCommandString() allows escaping multiple backslashes before spaces', () => testParseCommandOutput('echo.js foo\\\\\\\\ bar', 'foo\\\\\\ bar', parseAndRunCommand));101test('parseCommandString() allows escaping backslashes not before spaces', () => testParseCommandOutput('echo.js foo\\bar baz', 'foo\\bar\nbaz', parseAndRunCommand));

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 102why, one step back
102

TweaksSindre Sorhusbefore adoption698a1b8

lines 103–104why, one step back
103test('parseCommandString() can get empty strings', () => {
104 assert.deepEqual(parseCommandString(''), []);

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

lines 105–106why, one step back
105});
106

TweaksSindre Sorhusbefore adoption698a1b8

line 107why, one step back
107test('parseCommandString() can get only whitespaces', () => {

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

line 108why, one step back
108 // eslint-disable-next-line unicorn/prefer-string-repeat

TweaksSindre Sorhusbefore adoption698a1b8

line 109why, one step back
109 assert.deepEqual(parseCommandString(' '), []);

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 110why, one step back
110});