test-basic.js
3.04 KB
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
const assert = require('assert');
const defaultShell = require('../src/index');
const getShell = require('../src/get-shell');
const withEnv = require('./utils').withEnv;
const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform');
function testBasic() {
it('piping should work', (done) => {
const child = defaultShell.spawn('cat test/data/test.txt | grep 1', {
stdio: 'pipe',
});
child.stdout.on('data', (data) => {
assert.strictEqual(data.toString('utf8'), '1 äö☃\n');
});
child.on('close', (code) => {
assert.strictEqual(code, 0);
done();
});
});
it('&& operator should work', (done) => {
const child = defaultShell.spawn('echo 1 && node -e "process.exit(42)"');
child.on('close', (code) => {
assert.strictEqual(code, 42);
done();
});
});
describe('process.platform = "darwin"', () => {
before(() => {
Object.defineProperty(process, 'platform', { value: 'darwin' });
});
after(() => {
Object.defineProperty(process, 'platform', originalPlatform);
});
it('shell resolution order should be 1. SHELL 2. /bin/bash', () => {
withEnv({ SHELL: '' }, () => {
assert.strictEqual(getShell().shell, '/bin/bash');
assert.strictEqual(getShell().executeFlag, '-c');
});
withEnv({ SHELL: 'zsh' }, () => {
assert.strictEqual(getShell().shell, 'zsh');
assert.strictEqual(getShell().executeFlag, '-c');
});
});
});
describe('process.platform = "win32"', () => {
before(() => {
Object.defineProperty(process, 'platform', { value: 'win32' });
});
after(() => {
Object.defineProperty(process, 'platform', originalPlatform);
});
it('shell resolution order should be 1. SHELL 2. COMSPEC 3. cmd.exe', () => {
withEnv({ SHELL: '', COMSPEC: '' }, () => {
assert.strictEqual(getShell().shell, 'cmd.exe');
assert.strictEqual(getShell().executeFlag, '/c');
});
withEnv({ SHELL: '', COMSPEC: '\\C:\\cmd.exe' }, () => {
assert.strictEqual(getShell().shell, '\\C:\\cmd.exe');
assert.strictEqual(getShell().executeFlag, '/c');
});
withEnv({ SHELL: 'bash', COMSPEC: '\\C:\\cmd.exe' }, () => {
assert.strictEqual(getShell().shell, 'bash');
assert.strictEqual(getShell().executeFlag, '-c');
});
});
});
describe('process.platform = "linux" (other than win32 or darwin)', () => {
before(() => {
Object.defineProperty(process, 'platform', { value: 'linux' });
});
after(() => {
Object.defineProperty(process, 'platform', originalPlatform);
});
it('shell resolution order should be 1. SHELL 2. /bin/sh', () => {
withEnv({ SHELL: '' }, () => {
assert.strictEqual(getShell().shell, '/bin/sh');
assert.strictEqual(getShell().executeFlag, '-c');
});
withEnv({ SHELL: 'zsh' }, () => {
assert.strictEqual(getShell().shell, 'zsh');
assert.strictEqual(getShell().executeFlag, '-c');
});
});
});
}
module.exports = testBasic;