This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
/
variable.js
121 lines (97 loc) 路 3.78 KB
/
variable.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
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
115
116
117
118
119
120
121
import isWindowsMock from '../is-windows'
import varValueConvert from '../variable'
jest.mock('../is-windows')
const JSON_VALUE = '{\\"foo\\":\\"bar\\"}'
beforeEach(() => {
process.env.VAR1 = 'value1'
process.env.VAR2 = 'value2'
process.env.JSON_VAR = JSON_VALUE
isWindowsMock.__mock.reset()
})
afterEach(() => {
const {env} = process
delete env.VAR1
delete env.VAR2
delete env.JSON_VAR
})
test(`doesn't affect simple variable values`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('foo')).toBe('foo')
})
test(`doesn't convert a ; into a : on UNIX`, () => {
isWindowsMock.__mock.returnValue = false
expect(varValueConvert('foo;bar', 'PATH')).toBe('foo;bar')
})
test(`doesn't convert a ; into a : for non-PATH on UNIX`, () => {
isWindowsMock.__mock.returnValue = false
expect(varValueConvert('foo;bar', 'FOO')).toBe('foo;bar')
})
test(`doesn't convert a ; into a : for non-PATH on Windows`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('foo;bar', 'FOO')).toBe('foo;bar')
})
test(`converts a : into a ; on Windows if PATH`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('foo:bar', 'PATH')).toBe('foo;bar')
})
test(`doesn't convert already valid separators`, () => {
isWindowsMock.__mock.returnValue = false
expect(varValueConvert('foo:bar')).toBe('foo:bar')
})
test(`doesn't convert escaped separators on Windows if PATH`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('foo\\:bar', 'PATH')).toBe('foo:bar')
})
test(`doesn't convert escaped separators on UNIX`, () => {
isWindowsMock.__mock.returnValue = false
expect(varValueConvert('foo\\:bar', 'PATH')).toBe('foo:bar')
})
test(`converts a separator even if preceded by an escaped backslash`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('foo\\\\:bar', 'PATH')).toBe('foo\\\\;bar')
})
test(`converts multiple separators if PATH`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('foo:bar:baz', 'PATH')).toBe('foo;bar;baz')
})
test(`resolves an env variable value`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('foo-$VAR1')).toBe('foo-value1')
})
test(`resolves an env variable value with curly syntax`, () => {
isWindowsMock.__mock.returnValue = true
// eslint-disable-next-line no-template-curly-in-string
expect(varValueConvert('foo-${VAR1}')).toBe('foo-value1')
})
test(`resolves multiple env variable values`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('foo-$VAR1-$VAR2')).toBe('foo-value1-value2')
})
test(`resolves an env variable value for non-existant variable`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('foo-$VAR_POTATO')).toBe('foo-')
})
test(`resolves an env variable with a JSON string value on Windows`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('$JSON_VAR')).toBe(JSON_VALUE)
})
test(`resolves an env variable with a JSON string value on UNIX`, () => {
isWindowsMock.__mock.returnValue = false
expect(varValueConvert('$JSON_VAR')).toBe(JSON_VALUE)
})
test(`does not resolve an env variable prefixed with \\ on Windows`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('\\$VAR1')).toBe('$VAR1')
})
test(`does not resolve an env variable prefixed with \\ on UNIX`, () => {
isWindowsMock.__mock.returnValue = false
expect(varValueConvert('\\$VAR1')).toBe('$VAR1')
})
test(`resolves an env variable prefixed with \\\\ on Windows`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('\\\\$VAR1')).toBe('\\value1')
})
test(`resolves an env variable prefixed with \\\\ on UNIX`, () => {
isWindowsMock.__mock.returnValue = false
expect(varValueConvert('\\\\$VAR1')).toBe('\\value1')
})