-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtool.test.js
242 lines (206 loc) · 7.47 KB
/
tool.test.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import fs from "fs"
import { jest } from "@jest/globals"
import Tool from "./tool"
import { Cleaner, TestTool, Mute } from "./testutil"
Mute.all()
describe("getVersion", () => {
const repoToolVersionFilename = ".go-version"
afterEach(() => {
fs.rmSync(repoToolVersionFilename, { force: true })
})
it("works when action desired version present", () => {
const tool = new TestTool()
const [checkVersion, isVersionOverridden] = tool.getVersion(
"1.2.1",
null,
)
expect(checkVersion).toBe("1.2.1")
expect(isVersionOverridden).toBe(true)
})
it("works when desired version present in dot file", () => {
fs.writeFileSync(repoToolVersionFilename, "1.2.2")
const tool = new TestTool()
const [checkVersion, isVersionOverridden] = tool.getVersion(
null,
repoToolVersionFilename,
)
expect(checkVersion).toBe("1.2.2")
expect(isVersionOverridden).toBe(false)
})
it("works when action desired version and desired version present in dot file", () => {
fs.writeFileSync(repoToolVersionFilename, "1.1.2")
const tool = new TestTool()
const [checkVersion, isVersionOverridden] = tool.getVersion(
"1.2.3",
repoToolVersionFilename,
)
expect(checkVersion).toBe("1.2.3")
expect(isVersionOverridden).toBe(true)
})
it("works when empty version present in dot file", () => {
fs.writeFileSync(repoToolVersionFilename, "")
const tool = new TestTool()
const [checkVersion, isVersionOverridden] = tool.getVersion(
null,
repoToolVersionFilename,
)
expect(checkVersion).toBe(null)
expect(isVersionOverridden).toBe(null)
})
it("works when no desired version present", () => {
const tool = new TestTool()
const [checkVersion, isVersionOverridden] = tool.getVersion(null, null)
expect(checkVersion).toBe(null)
expect(isVersionOverridden).toBe(null)
})
})
describe("version", () => {
const mockProcessExit = jest
.spyOn(process, "exit")
.mockImplementation((code) => {
throw new Error(`process.exit(${code})`)
})
beforeEach(() => {
mockProcessExit.mockClear()
})
it("works", async () => {
const tool = new TestTool()
const version = await tool.version("bash --version")
expect(version).toMatch(/^\d+\.\d+\.\d+$/)
})
// Skipping this test because it makes tests look like they fail
it("errors sensibly if a thing isn't found", () => {
const tool = new TestTool()
return expect(tool.version("fake-version")).rejects.toThrow(
/Unable to locate executable file: fake-version/,
)
})
it("handles java 1.8.0 output (regression)", () => {
const tool = new TestTool()
return expect(
tool.version(
`sh -c "echo -e 'openjdk version 1.8.0_282\\nOpenJDK Runtime Environment (build 1.8.0_282-b08)\\nOpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)'"`,
),
).resolves.toMatch(/1\.8\.0/)
})
})
describe("findRoot", () => {
beforeEach(() => {
for (let k of Object.keys(process.env)) {
if (/_ROOT$/.test(k)) {
delete process.env[k]
}
}
})
it("works", async () => {
const tool = new TestTool()
const found = await tool.findRoot()
expect(found).toContain(tool.installerPath)
})
it("throws if there's a bad tool root", () => {
const tool = new TestTool()
process.env.TESTENV_ROOT = "/tmp/nonexistent"
return expect(tool.findRoot()).rejects.toThrow(
/TESTENV_ROOT misconfigured:/,
)
})
})
describe("tempRoot", () => {
const cleaner = new Cleaner(TestTool, "testenv")
afterEach(cleaner.clean)
it("creates a temporary directory", () => {
const tool = new TestTool()
const temp = tool.tempRoot
cleaner.root = temp
expect(temp).toContain(tool.defaults.RUNNER_TEMP)
expect(temp).toContain("/.test")
expect(fs.existsSync(temp)).toBe(true)
})
it("works as a static method", () => {
const temp = TestTool.tempRoot()
cleaner.root = temp
expect(temp).toContain(TestTool.defaults.RUNNER_TEMP)
expect(temp).toContain("/.test")
expect(fs.existsSync(temp)).toBe(true)
})
})
describe("subprocessShell", () => {
const tool = new TestTool()
it("works", async () => {
const proc = await tool.subprocessShell("echo hello")
expect(proc.stdout).toBe("hello\n")
})
it("actually uses the PATH we give it", async () => {
const env = { env: { PATH: "/bin:/usr/bin" } }
const proc = await tool.subprocessShell('echo "PATH=${PATH}"', env)
expect(proc.stdout).toMatch(new RegExp(`^PATH=.*${env.env.PATH}.*\n$`))
})
})
describe("tokenizeArgs", () => {
const tool = new TestTool()
it("works", () => {
const cmd = "echo arg1 arg2 arg3"
const args = tool.tokenizeArgs(cmd)
expect(args).toEqual(["echo", "arg1", "arg2", "arg3"])
})
it("handles escaped spaces", () => {
const cmd = "echo arg1 arg2\\ arg3"
const args = tool.tokenizeArgs(cmd)
expect(args).toEqual(["echo", "arg1", "arg2 arg3"])
})
it("handles single quotes", () => {
const cmd = "echo arg1 'arg2 arg3'"
const args = tool.tokenizeArgs(cmd)
expect(args).toEqual(["echo", "arg1", "arg2 arg3"])
})
it("handles nested quotes", () => {
const cmd = "echo \"arg1 'arg2' arg3\""
const args = tool.tokenizeArgs(cmd)
expect(args).toEqual(["echo", "arg1 'arg2' arg3"])
})
it("handles nested quotes 2", () => {
const cmd = "echo 'arg1 \"arg2\" arg3'"
const args = tool.tokenizeArgs(cmd)
expect(args).toEqual(["echo", 'arg1 "arg2" arg3'])
})
it("handles smushy quotes", () => {
const cmd = `echo "arg 1"'arg 2'"arg 3"`
const args = tool.tokenizeArgs(cmd)
expect(args).toEqual(["echo", "arg 1arg 2arg 3"])
})
it("handles escaped quotes", () => {
const cmd = 'echo \\"arg1\\" arg2 arg3'
const args = tool.tokenizeArgs(cmd)
expect(args).toEqual(["echo", '\\"arg1\\"', "arg2", "arg3"])
})
it("handles escaped nested quotes", () => {
const cmd = 'echo "arg1 \\"arg2\\" arg3"'
const args = tool.tokenizeArgs(cmd)
expect(args).toEqual(["echo", 'arg1 "arg2" arg3'])
})
it("handles backtick quotes", () => {
const cmd = "echo `whoami` arg2 arg3"
const args = tool.tokenizeArgs(cmd)
expect(args).toEqual(["echo", "`whoami`", "arg2", "arg3"])
})
it("handles bash wrapped commands", () => {
const cmd = 'bash -c "echo \\"PATH=$PATH\\""'
const args = tool.tokenizeArgs(cmd)
expect(args).toEqual(["bash", "-c", 'echo "PATH=$PATH"'])
})
})
describe("all", () => {
it("gives us no tools when nothing is registered", () => {
const all = Tool.all()
expect(all).toHaveLength(0)
})
it("gives us all the self-registered tools", () => {
return import("./golang").then((module) => {
// The dynamic import doesn't name the default export,
// interestingly
module.default.register()
const all = Tool.all()
expect(all).toHaveLength(1)
})
})
})