-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
102 lines (86 loc) · 2.54 KB
/
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
import * as fs from 'fs';
import test from 'ava';
import execa from 'execa';
import pify from 'pify';
import pathExists from 'path-exists';
import astVisit from 'bash-ast-traverser';
import visitor from '.';
const readFile = pify(fs.readFile);
const unlink = pify(fs.unlink);
test('Command node - run external process for unknown command names', async t => {
const {stderr, stdout} = await execa('node', ['fixtures/nonwell-known-command.js']);
t.is(stdout, 'ciao');
t.is(stderr, '');
});
test('Command node - support numbered redirections', async t => {
const {stderr, stdout} = await execa('node', ['fixtures/numbered-redirections.js']);
t.is(stdout, '');
t.is(stderr, 'ciao');
});
test('Command node - support redirections to files', async t => {
const fileName = '/tmp/test';
if (await pathExists(fileName)) {
await unlink(fileName);
}
const {stderr, stdout} = await execa('node', ['fixtures/files-redirections.js']);
t.is(stdout, '');
t.is(stderr, '');
t.is(await readFile(fileName, 'utf8'), 'ciao\n');
await unlink(fileName);
});
test('Command node - support appending to files', async t => {
const fileName = '/tmp/test-append';
if (await pathExists(fileName)) {
await unlink(fileName);
}
await execa('node', ['fixtures/files-append.js']);
const {stderr, stdout} = await execa('node', ['fixtures/files-append.js']);
t.is(stdout, '');
t.is(stderr, '');
t.is(await readFile(fileName, 'utf8'), 'ciao\nciao\n');
await unlink(fileName);
});
test('Command node - stdin redirection from file', async t => {
const {stderr, stdout} = await execa('node', ['fixtures/input.js']);
t.is(stdout.trim(), '4');
t.is(stderr, '');
});
function commandNode(command, ...args) {
const node = {
type: 'Command',
name: {
type: 'Word',
text: command
},
suffix: args.map(arg => ({
type: 'Word',
text: arg
}))
};
return astVisit(node, visitor);
}
test('Command node - return a promise to an exit code', async t => {
const runner = commandNode('node', 'fixtures/exit-42.js');
const exitCode = await runner.run({
env: process.env
});
t.is(exitCode, 42);
});
test('Command node - pass env vars to child process', async t => {
const runner = commandNode('node', 'fixtures/env-spy.js');
const exitCode = await runner.run({
env: Object.assign(
{thisIsATest: 43},
process.env
)
});
t.is(exitCode, 43);
});
test('Command node - pass cwd to child process', async t => {
const runner = commandNode('node', 'check-cwd.js');
const exitCode = await runner.run({
cwd: `${__dirname}/fixtures`,
env: process.env
});
t.is(exitCode, 142);
});