index.test.js
3.07 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
106
107
108
109
110
111
112
113
114
import chai from 'chai';
import sinonChai from 'sinon-chai';
import sinon from 'sinon';
import proxyquire from 'proxyquire';
chai.use(sinonChai);
const {expect} = chai;
const spawned = {on: sinon.spy(), kill: sinon.spy()};
const proxied = {
'cross-spawn': {
spawn: sinon.spy(() => spawned)
}
};
const crossEnv = proxyquire('./index', proxied);
describe(`cross-env`, () => {
beforeEach(() => {
proxied['cross-spawn'].spawn.reset();
spawned.on.reset();
});
it(`should set environment variables and run the remaining command`, () => {
testEnvSetting({
FOO_ENV: 'production'
}, 'FOO_ENV=production');
});
it(`should APPDATA be undefined and not string`, () => {
testEnvSetting({
FOO_ENV: 'production',
APPDATA: 2
}, 'FOO_ENV=production');
});
it(`should handle multiple env variables`, () => {
testEnvSetting({
FOO_ENV: 'production',
BAR_ENV: 'dev',
APPDATA: '0'
}, 'FOO_ENV=production', 'BAR_ENV=dev', 'APPDATA=0');
});
it(`should handle special characters`, () => {
testEnvSetting({
FOO_ENV: './!?'
}, 'FOO_ENV=./!?');
});
it(`should handle single-quoted strings`, () => {
testEnvSetting({
FOO_ENV: 'bar env'
}, 'FOO_ENV=\'bar env\'');
});
it(`should handle double-quoted strings`, () => {
testEnvSetting({
FOO_ENV: 'bar env'
}, 'FOO_ENV="bar env"');
});
it(`should handle equality signs in quoted strings`, () => {
testEnvSetting({
FOO_ENV: 'foo=bar'
}, 'FOO_ENV="foo=bar"');
});
it(`should do nothing given no command`, () => {
crossEnv([]);
expect(proxied['cross-spawn'].spawn).to.have.not.been.called;
});
it(`should propagate kill signals`, () => {
testEnvSetting({
FOO_ENV: 'foo=bar'
}, 'FOO_ENV="foo=bar"');
process.emit('SIGTERM');
process.emit('SIGINT');
process.emit('SIGHUP');
process.emit('SIGBREAK');
expect(spawned.kill).to.have.been.calledWith('SIGTERM');
expect(spawned.kill).to.have.been.calledWith('SIGINT');
expect(spawned.kill).to.have.been.calledWith('SIGHUP');
expect(spawned.kill).to.have.been.calledWith('SIGBREAK');
});
function testEnvSetting(expected, ...envSettings) {
if (expected.APPDATA === 2) { // kill the APPDATA to test both is undefined
delete process.env.APPDATA;
delete expected.APPDATA;
} else if (!process.env.APPDATA && expected.APPDATA === '0') { // set APPDATA and test it
process.env.APPDATA = '0';
}
const ret = crossEnv([...envSettings, 'echo', 'hello world']);
const env = {};
if (process.env.APPDATA) {
env.APPDATA = process.env.APPDATA;
}
Object.assign(env, expected);
expect(ret, 'returns what spawn returns').to.equal(spawned);
expect(proxied['cross-spawn'].spawn).to.have.been.calledOnce;
expect(proxied['cross-spawn'].spawn).to.have.been.calledWith(
'echo', ['hello world'], {
stdio: 'inherit',
env: Object.assign({}, process.env, env)
}
);
expect(spawned.on).to.have.been.calledOnce;
expect(spawned.on).to.have.been.calledWith('exit');
}
});