|
1 | 1 | import isWindowsMock from 'is-windows'
|
2 | 2 | import commandConvert from '../command'
|
3 | 3 |
|
| 4 | +const env = { |
| 5 | + test: 'a', |
| 6 | + test1: 'b', |
| 7 | + test2: 'c', |
| 8 | + test3: 'd', |
| 9 | + 'empty_var': '' |
| 10 | +} |
| 11 | + |
4 | 12 | beforeEach(() => {
|
5 | 13 | isWindowsMock.__mock.reset()
|
6 | 14 | })
|
7 | 15 |
|
8 | 16 | test(`converts unix-style env variable usage for windows`, () => {
|
9 | 17 | isWindowsMock.__mock.returnValue = true
|
10 |
| - expect(commandConvert('$test')).toBe('%test%') |
| 18 | + expect(commandConvert('$test', env)).toBe('%test%') |
11 | 19 | })
|
12 | 20 |
|
13 | 21 | test(`leaves command unchanged when not a variable`, () => {
|
14 |
| - expect(commandConvert('test')).toBe('test') |
| 22 | + expect(commandConvert('test', env)).toBe('test') |
15 | 23 | })
|
16 | 24 |
|
17 | 25 | test(`doesn't convert windows-style env variable`, () => {
|
18 | 26 | isWindowsMock.__mock.returnValue = false
|
19 |
| - expect(commandConvert('%test%')).toBe('%test%') |
| 27 | + expect(commandConvert('%test%', env)).toBe('%test%') |
20 | 28 | })
|
21 | 29 |
|
22 | 30 | test(`leaves variable unchanged when using correct operating system`, () => {
|
23 | 31 | isWindowsMock.__mock.returnValue = false
|
24 |
| - expect(commandConvert('$test')).toBe('$test') |
| 32 | + expect(commandConvert('$test', env)).toBe('$test') |
25 | 33 | })
|
26 | 34 |
|
27 | 35 | test(`is stateless`, () => {
|
28 | 36 | // this test prevents falling into regexp traps like this:
|
29 | 37 | // http://stackoverflow.com/a/1520853/971592
|
30 | 38 | isWindowsMock.__mock.returnValue = true
|
31 |
| - expect(commandConvert('$test')).toBe(commandConvert('$test')) |
| 39 | + expect(commandConvert('$test', env)).toBe(commandConvert('$test', env)) |
32 | 40 | })
|
33 | 41 |
|
34 | 42 | test(`converts embedded unix-style env variables usage for windows`, () => {
|
35 | 43 | isWindowsMock.__mock.returnValue = true
|
36 |
| - expect(commandConvert('$test1/$test2/$test3')).toBe('%test1%/%test2%/%test3%') |
| 44 | + expect(commandConvert('$test1/$test2/$test3', env)).toBe('%test1%/%test2%/%test3%') |
37 | 45 | })
|
38 | 46 |
|
39 | 47 | // eslint-disable-next-line max-len
|
40 | 48 | test(`leaves embedded variables unchanged when using correct operating system`, () => {
|
41 | 49 | isWindowsMock.__mock.returnValue = false
|
42 |
| - expect(commandConvert('$test1/$test2/$test3')).toBe('$test1/$test2/$test3') |
| 50 | + expect(commandConvert('$test1/$test2/$test3', env)).toBe('$test1/$test2/$test3') |
43 | 51 | })
|
44 | 52 |
|
45 | 53 | test(`converts braced unix-style env variable usage for windows`, () => {
|
46 | 54 | isWindowsMock.__mock.returnValue = true
|
47 | 55 | // eslint-disable-next-line no-template-curly-in-string
|
48 |
| - expect(commandConvert('${test}')).toBe('%test%') |
| 56 | + expect(commandConvert('${test}', env)).toBe('%test%') |
| 57 | +}) |
| 58 | + |
| 59 | +test(`removes non-existent variables from the converted command`, () => { |
| 60 | + isWindowsMock.__mock.returnValue = true |
| 61 | + expect(commandConvert('$test1/$foo/$test2', env)).toBe('%test1%//%test2%') |
| 62 | +}) |
| 63 | + |
| 64 | +test(`removes empty variables from the converted command`, () => { |
| 65 | + isWindowsMock.__mock.returnValue = true |
| 66 | + expect(commandConvert('$foo/$test/$empty_var', env)).toBe('/%test%/') |
49 | 67 | })
|
50 | 68 |
|
51 | 69 | test(`normalizes command on windows`, () => {
|
52 | 70 | isWindowsMock.__mock.returnValue = true
|
53 | 71 | // index.js calls `commandConvert` with `normalize` param
|
54 | 72 | // as `true` for command only
|
55 |
| - expect(commandConvert('./cmd.bat', true)).toBe('cmd.bat') |
| 73 | + expect(commandConvert('./cmd.bat', env, true)).toBe('cmd.bat') |
56 | 74 | })
|
0 commit comments