This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
/
command.js
76 lines (62 loc) 路 2.4 KB
/
command.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
import isWindowsMock from '../is-windows'
import commandConvert from '../command'
jest.mock('../is-windows')
const env = {
test: 'a',
test1: 'b',
test2: 'c',
test3: 'd',
'empty_var': ''
}
beforeEach(() => {
isWindowsMock.__mock.reset()
})
test(`converts unix-style env variable usage for windows`, () => {
isWindowsMock.__mock.returnValue = true
expect(commandConvert('$test', env)).toBe('%test%')
})
test(`leaves command unchanged when not a variable`, () => {
expect(commandConvert('test', env)).toBe('test')
})
test(`doesn't convert windows-style env variable`, () => {
isWindowsMock.__mock.returnValue = false
expect(commandConvert('%test%', env)).toBe('%test%')
})
test(`leaves variable unchanged when using correct operating system`, () => {
isWindowsMock.__mock.returnValue = false
expect(commandConvert('$test', env)).toBe('$test')
})
test(`is stateless`, () => {
// this test prevents falling into regexp traps like this:
// http://stackoverflow.com/a/1520853/971592
isWindowsMock.__mock.returnValue = true
expect(commandConvert('$test', env)).toBe(commandConvert('$test', env))
})
test(`converts embedded unix-style env variables usage for windows`, () => {
isWindowsMock.__mock.returnValue = true
expect(commandConvert('$test1/$test2/$test3', env)).toBe('%test1%/%test2%/%test3%')
})
// eslint-disable-next-line max-len
test(`leaves embedded variables unchanged when using correct operating system`, () => {
isWindowsMock.__mock.returnValue = false
expect(commandConvert('$test1/$test2/$test3', env)).toBe('$test1/$test2/$test3')
})
test(`converts braced unix-style env variable usage for windows`, () => {
isWindowsMock.__mock.returnValue = true
// eslint-disable-next-line no-template-curly-in-string
expect(commandConvert('${test}', env)).toBe('%test%')
})
test(`removes non-existent variables from the converted command`, () => {
isWindowsMock.__mock.returnValue = true
expect(commandConvert('$test1/$foo/$test2', env)).toBe('%test1%//%test2%')
})
test(`removes empty variables from the converted command`, () => {
isWindowsMock.__mock.returnValue = true
expect(commandConvert('$foo/$test/$empty_var', env)).toBe('/%test%/')
})
test(`normalizes command on windows`, () => {
isWindowsMock.__mock.returnValue = true
// index.js calls `commandConvert` with `normalize` param
// as `true` for command only
expect(commandConvert('./cmd.bat', env, true)).toBe('cmd.bat')
})