-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
command.test.ts
139 lines (120 loc) · 3.84 KB
/
command.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
134
135
136
137
138
139
import * as command from '../src/command'
import * as os from 'os'
/* eslint-disable @typescript-eslint/unbound-method */
let originalWriteFunction: (str: string) => boolean
describe('@actions/core/src/command', () => {
beforeAll(() => {
originalWriteFunction = process.stdout.write
})
beforeEach(() => {
process.stdout.write = jest.fn()
})
afterEach(() => {})
afterAll(() => {
process.stdout.write = (originalWriteFunction as unknown) as (
str: string
) => boolean
})
it('command only', () => {
command.issueCommand('some-command', {}, '')
assertWriteCalls([`::some-command::${os.EOL}`])
})
it('command escapes message', () => {
// Verify replaces each instance, not just first instance
command.issueCommand(
'some-command',
{},
'percent % percent % cr \r cr \r lf \n lf \n'
)
assertWriteCalls([
`::some-command::percent %25 percent %25 cr %0D cr %0D lf %0A lf %0A${os.EOL}`
])
// Verify literal escape sequences
process.stdout.write = jest.fn()
command.issueCommand('some-command', {}, '%25 %25 %0D %0D %0A %0A')
assertWriteCalls([
`::some-command::%2525 %2525 %250D %250D %250A %250A${os.EOL}`
])
})
it('command escapes property', () => {
// Verify replaces each instance, not just first instance
command.issueCommand(
'some-command',
{
name:
'percent % percent % cr \r cr \r lf \n lf \n colon : colon : comma , comma ,'
},
''
)
assertWriteCalls([
`::some-command name=percent %25 percent %25 cr %0D cr %0D lf %0A lf %0A colon %3A colon %3A comma %2C comma %2C::${os.EOL}`
])
// Verify literal escape sequences
process.stdout.write = jest.fn()
command.issueCommand(
'some-command',
{},
'%25 %25 %0D %0D %0A %0A %3A %3A %2C %2C'
)
assertWriteCalls([
`::some-command::%2525 %2525 %250D %250D %250A %250A %253A %253A %252C %252C${os.EOL}`
])
})
it('command with message', () => {
command.issueCommand('some-command', {}, 'some message')
assertWriteCalls([`::some-command::some message${os.EOL}`])
})
it('command with message and properties', () => {
command.issueCommand(
'some-command',
{prop1: 'value 1', prop2: 'value 2'},
'some message'
)
assertWriteCalls([
`::some-command prop1=value 1,prop2=value 2::some message${os.EOL}`
])
})
it('command with one property', () => {
command.issueCommand('some-command', {prop1: 'value 1'}, '')
assertWriteCalls([`::some-command prop1=value 1::${os.EOL}`])
})
it('command with two properties', () => {
command.issueCommand(
'some-command',
{prop1: 'value 1', prop2: 'value 2'},
''
)
assertWriteCalls([`::some-command prop1=value 1,prop2=value 2::${os.EOL}`])
})
it('command with three properties', () => {
command.issueCommand(
'some-command',
{prop1: 'value 1', prop2: 'value 2', prop3: 'value 3'},
''
)
assertWriteCalls([
`::some-command prop1=value 1,prop2=value 2,prop3=value 3::${os.EOL}`
])
})
it('should handle issueing commands for non-string objects', () => {
command.issueCommand(
'some-command',
{
prop1: ({test: 'object'} as unknown) as string,
prop2: (123 as unknown) as string,
prop3: (true as unknown) as string
},
({test: 'object'} as unknown) as string
)
assertWriteCalls([
`::some-command prop1={"test"%3A"object"},prop2=123,prop3=true::{"test":"object"}${os.EOL}`
])
})
})
// Assert that process.stdout.write calls called only with the given arguments.
function assertWriteCalls(calls: string[]): void {
expect(process.stdout.write).toHaveBeenCalledTimes(calls.length)
for (let i = 0; i < calls.length; i++) {
expect(process.stdout.write).toHaveBeenNthCalledWith(i + 1, calls[i])
}
}