This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
/
command.js
81 lines (67 loc) 路 2.43 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
77
78
79
80
81
const isWindowsMock = require('../is-windows')
const commandConvert = require('../command')
jest.mock('../is-windows')
const env = {
test: 'a',
test1: 'b',
test2: 'c',
test3: 'd',
// eslint-disable-next-line babel/camelcase
empty_var: '',
}
afterEach(() => {
jest.clearAllMocks()
})
test(`converts unix-style env variable usage for windows`, () => {
isWindowsMock.mockReturnValue(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.mockReturnValue(false)
expect(commandConvert('%test%', env)).toBe('%test%')
})
test(`leaves variable unchanged when using correct operating system`, () => {
isWindowsMock.mockReturnValue(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.mockReturnValue(true)
expect(commandConvert('$test', env)).toBe(commandConvert('$test', env))
})
test(`converts embedded unix-style env variables usage for windows`, () => {
isWindowsMock.mockReturnValue(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.mockReturnValue(false)
expect(commandConvert('$test1/$test2/$test3', env)).toBe(
'$test1/$test2/$test3',
)
})
test(`converts braced unix-style env variable usage for windows`, () => {
isWindowsMock.mockReturnValue(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.mockReturnValue(true)
expect(commandConvert('$test1/$foo/$test2', env)).toBe('%test1%//%test2%')
})
test(`removes empty variables from the converted command`, () => {
isWindowsMock.mockReturnValue(true)
expect(commandConvert('$foo/$test/$empty_var', env)).toBe('/%test%/')
})
test(`normalizes command on windows`, () => {
isWindowsMock.mockReturnValue(true)
// index.js calls `commandConvert` with `normalize` param
// as `true` for command only
expect(commandConvert('./cmd.bat', env, true)).toBe('cmd.bat')
})