-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathutils.test.ts
133 lines (107 loc) · 3.78 KB
/
utils.test.ts
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
122
123
124
125
126
127
128
129
130
131
132
133
import {
getFormattedNodeOptionsWithoutInspect,
getParsedDebugAddress,
formatNodeOptions,
tokenizeArgs,
} from './utils'
const originalNodeOptions = process.env.NODE_OPTIONS
afterAll(() => {
process.env.NODE_OPTIONS = originalNodeOptions
})
describe('tokenizeArgs', () => {
it('splits arguments by spaces', () => {
const result = tokenizeArgs('--spaces "thing with spaces" --normal 1234')
expect(result).toEqual([
'--spaces',
'thing with spaces',
'--normal',
'1234',
])
})
it('supports quoted values', () => {
const result = tokenizeArgs(
'--spaces "thing with spaces" --spacesAndQuotes "thing with \\"spaces\\"" --normal 1234'
)
expect(result).toEqual([
'--spaces',
'thing with spaces',
'--spacesAndQuotes',
'thing with "spaces"',
'--normal',
'1234',
])
})
})
describe('formatNodeOptions', () => {
it('wraps values with spaces in quotes', () => {
const result = formatNodeOptions({
spaces: 'thing with spaces',
spacesAndQuotes: 'thing with "spaces"',
normal: '1234',
})
expect(result).toBe(
'--spaces="thing with spaces" --spacesAndQuotes="thing with \\"spaces\\"" --normal=1234'
)
})
})
describe('getParsedDebugAddress', () => {
it('supports the flag with an equal sign', () => {
process.env.NODE_OPTIONS = '--inspect=1234'
const result = getParsedDebugAddress()
expect(result).toEqual({ host: undefined, port: 1234 })
})
it('supports the flag without an equal sign', () => {
process.env.NODE_OPTIONS = '--inspect 1234'
const result = getParsedDebugAddress()
expect(result).toEqual({ host: undefined, port: 1234 })
})
})
describe('getFormattedNodeOptionsWithoutInspect', () => {
it('removes --inspect option', () => {
process.env.NODE_OPTIONS = '--other --inspect --additional'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe('--other --additional')
})
it('removes --inspect option at end of line', () => {
process.env.NODE_OPTIONS = '--other --inspect'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe('--other')
})
it('handles options with spaces', () => {
process.env.NODE_OPTIONS =
'--other --inspect --additional --spaces "/some/path with spaces"'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe(
'--other --additional --spaces="/some/path with spaces"'
)
})
it('handles options with quotes', () => {
process.env.NODE_OPTIONS =
'--require "./file with spaces to-require-with-node-require-option.js"'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe(
'--require="./file with spaces to-require-with-node-require-option.js"'
)
})
it('removes --inspect option with parameters', () => {
process.env.NODE_OPTIONS = '--other --inspect=0.0.0.0:1234 --additional'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe('--other --additional')
})
it('removes --inspect-brk option', () => {
process.env.NODE_OPTIONS = '--other --inspect-brk --additional'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe('--other --additional')
})
it('removes --inspect-brk option with parameters', () => {
process.env.NODE_OPTIONS = '--other --inspect-brk=0.0.0.0:1234 --additional'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe('--other --additional')
})
it('ignores unrelated options starting with --inspect-', () => {
process.env.NODE_OPTIONS =
'--other --inspect-port=0.0.0.0:1234 --additional'
const result = getFormattedNodeOptionsWithoutInspect()
expect(result).toBe('--other --inspect-port=0.0.0.0:1234 --additional')
})
})