/
bind.js
109 lines (89 loc) · 5.24 KB
/
bind.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
import {join} from 'node:path';
import test from 'ava';
import {
execa,
execaSync,
execaNode,
$,
} from '../../index.js';
import {foobarString, foobarUppercase} from '../helpers/input.js';
import {uppercaseGenerator} from '../helpers/generator.js';
import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';
setFixtureDirectory();
const NOOP_PATH = join(FIXTURES_DIRECTORY, 'noop.js');
const PRINT_ENV_PATH = join(FIXTURES_DIRECTORY, 'environment.js');
const testBindOptions = async (t, execaMethod) => {
const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString]);
t.is(stdout, `${foobarString}\n`);
};
test('execa() can bind options', testBindOptions, execa);
test('execaNode() can bind options', testBindOptions, execaNode);
test('$ can bind options', testBindOptions, $);
const testBindOptionsSync = (t, execaMethod) => {
const {stdout} = execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString]);
t.is(stdout, `${foobarString}\n`);
};
test('execaSync() can bind options', testBindOptionsSync, execaSync);
test('$.sync can bind options', testBindOptionsSync, $.sync);
const testBindPriority = async (t, execaMethod) => {
const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: true});
t.is(stdout, foobarString);
};
test('execa() bound options have lower priority', testBindPriority, execa);
test('execaSync() bound options have lower priority', testBindPriority, execaSync);
test('execaNode() bound options have lower priority', testBindPriority, execaNode);
test('$ bound options have lower priority', testBindPriority, $);
test('$.sync bound options have lower priority', testBindPriority, $.sync);
const testBindUndefined = async (t, execaMethod) => {
const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: undefined});
t.is(stdout, foobarString);
};
test('execa() undefined options use default value', testBindUndefined, execa);
test('execaSync() undefined options use default value', testBindUndefined, execaSync);
test('execaNode() undefined options use default value', testBindUndefined, execaNode);
test('$ undefined options use default value', testBindUndefined, $);
test('$.sync undefined options use default value', testBindUndefined, $.sync);
const testMergeEnv = async (t, execaMethod) => {
const {stdout} = await execaMethod({env: {FOO: 'foo'}})(PRINT_ENV_PATH, {env: {BAR: 'bar'}});
t.is(stdout, 'foo\nbar');
};
test('execa() bound options are merged', testMergeEnv, execa);
test('execaSync() bound options are merged', testMergeEnv, execaSync);
test('execaNode() bound options are merged', testMergeEnv, execaNode);
test('$ bound options are merged', testMergeEnv, $);
test('$.sync bound options are merged', testMergeEnv, $.sync);
const testMergeMultiple = async (t, execaMethod) => {
const {stdout} = await execaMethod({env: {FOO: 'baz'}})({env: {BAR: 'bar'}})(PRINT_ENV_PATH, {env: {FOO: 'foo'}});
t.is(stdout, 'foo\nbar');
};
test('execa() bound options are merged multiple times', testMergeMultiple, execa);
test('execaSync() bound options are merged multiple times', testMergeMultiple, execaSync);
test('execaNode() bound options are merged multiple times', testMergeMultiple, execaNode);
test('$ bound options are merged multiple times', testMergeMultiple, $);
test('$.sync bound options are merged multiple times', testMergeMultiple, $.sync);
const testMergeFdSpecific = async (t, execaMethod) => {
const {isMaxBuffer, shortMessage} = await t.throwsAsync(execaMethod({maxBuffer: {stdout: 1}})(NOOP_PATH, [foobarString], {maxBuffer: {stderr: 100}}));
t.true(isMaxBuffer);
t.true(shortMessage.includes('Command\'s stdout was larger than 1'));
};
test('execa() bound options merge fd-specific ones', testMergeFdSpecific, execa);
test('execaNode() bound options merge fd-specific ones', testMergeFdSpecific, execaNode);
test('$ bound options merge fd-specific ones', testMergeFdSpecific, $);
const testMergeEnvUndefined = async (t, execaMethod) => {
const {stdout} = await execaMethod({env: {FOO: 'foo'}})(PRINT_ENV_PATH, {env: {BAR: undefined}});
t.is(stdout, 'foo\nundefined');
};
test('execa() bound options are merged even if undefined', testMergeEnvUndefined, execa);
test('execaSync() bound options are merged even if undefined', testMergeEnvUndefined, execaSync);
test('execaNode() bound options are merged even if undefined', testMergeEnvUndefined, execaNode);
test('$ bound options are merged even if undefined', testMergeEnvUndefined, $);
test('$.sync bound options are merged even if undefined', testMergeEnvUndefined, $.sync);
const testMergeSpecific = async (t, execaMethod) => {
const {stdout} = await execaMethod({stdout: {transform: uppercaseGenerator().transform, objectMode: true}})(NOOP_PATH, {stdout: {transform: uppercaseGenerator().transform}});
t.is(stdout, foobarUppercase);
};
test('execa() bound options only merge specific ones', testMergeSpecific, execa);
test('execaSync() bound options only merge specific ones', testMergeSpecific, execaSync);
test('execaNode() bound options only merge specific ones', testMergeSpecific, execaNode);
test('$ bound options only merge specific ones', testMergeSpecific, $);
test('$.sync bound options only merge specific ones', testMergeSpecific, $.sync);