-
Notifications
You must be signed in to change notification settings - Fork 2
/
java.test.js
265 lines (231 loc) · 8.28 KB
/
java.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import fs from "fs"
import Java from "./java"
import {
runAction,
runActionExpectError,
cleanPath,
Cleaner,
Mute,
ignoreInstalled,
} from "./testutil"
Mute.all()
const rcfile = ".sdkmanrc"
const describeJava = process.env.NO_TEST_JAVA ? describe.skip : describe
describeJava("runAction java", () => {
const desiredVersion = "17.0.5"
const sdkmanVersionIdentifier = `${desiredVersion}-tem`
const cleaner = new Cleaner(Java, "sdkman", [rcfile])
afterEach(cleaner.clean)
it.slow = process.env.TEST_FAST ? it.skip : it
it.slow("works", async () => {
const env = {
INPUT_JAVA: sdkmanVersionIdentifier,
...ignoreInstalled(),
}
return runAction("index", env).then((proc) => {
expect(proc.stderr.toString()).toBe("")
expect(proc.stdout).toContain(`java -version: ${desiredVersion}`)
expect(proc.stdout).toContain("java success!")
})
})
it("fails with bad SDKMAN_DIR", () => {
const env = {
INPUT_JAVA: sdkmanVersionIdentifier,
SDKMAN_DIR: "/tmp/.sdkman",
PATH: cleanPath("sdkman"),
...ignoreInstalled(),
}
cleaner.root = env.SDKMAN_DIR
return expect(
runActionExpectError("index", env).catch((err) => {
cleaner.captureRoot(err.stdout ?? err.stderr)
throw new Error(`${err.stdout}\n${err.stderr}`)
}),
).rejects.toThrow(/::error::SDKMAN_DIR misconfigured/)
})
})
describeJava("Java", () => {
const cleaner = new Cleaner(Java, "sdkman", [rcfile])
afterEach(cleaner.clean)
it("works when java isn't wanted", async () => {
ignoreInstalled()
const tool = new Java()
return tool.setup()
})
// This case is well covered by the parseSdkmanrc suite below
it.skip("parses a simple .sdkmanrc", async () => {
fs.writeFileSync(rcfile, "java=17.0.5-tem\n")
const tool = new Java()
cleaner.root = await tool.findRoot()
return tool.setup()
})
// This case is well covered by the validateVersion suite below
it.skip("works with java 1.8 nonsense", async () => {
const tool = new Java()
cleaner.root = await tool.findRoot()
return tool.setup("8.0.282-librca")
})
it("works and sets a sensible JAVA_HOME", async () => {
ignoreInstalled()
const tool = new Java()
return tool.setup("8.0.282-librca").then(() => {
expect(process.env.JAVA_HOME).toBe(
`${process.env.SDKMAN_DIR}/candidates/java/current`,
)
})
})
it("works if sdkman already is installed on the default", async () => {
const tool = new Java()
tool.info("-- Initialized")
// Install to our defaultRoot if it doesn't exist
if (!fs.existsSync(tool.defaultRoot)) {
tool.info("-- Installing")
// Install without the shim
await tool.install(tool.defaultRoot, true)
cleaner.root = tool.defaultRoot
}
tool.info("-- Setup")
await tool.setup("8.0.282-librca")
})
})
describeJava("install", () => {
const cleaner = new Cleaner(Java, "sdkman", [rcfile])
afterEach(cleaner.clean)
it("works", async () => {
const root = Java.tempRoot()
cleaner.root = await new Java().install(root)
expect(cleaner.root).toMatch(/\/\.sdkman/)
})
})
describe("parseSdkmanrc", () => {
const cleaner = new Cleaner(Java, "sdkman", [rcfile])
afterEach(cleaner.clean)
it("parses a simple .sdkmanrc", () => {
fs.writeFileSync(rcfile, "java=17.0.5-tem\n")
const tool = new Java()
const found = tool.parseSdkmanrc()
expect(found).toBe("17.0.5-tem")
})
it("returns nothing if there's no rc file", () => {
const tool = new Java()
const found = tool.parseSdkmanrc()
expect(found).toBeFalsy()
})
it("parses a more complex .sdkmanrc", () => {
fs.writeFileSync(rcfile, "java=1.8.1\nkotlin=1.2.3\npython=2.7.2\n")
const tool = new Java()
const found = tool.parseSdkmanrc()
expect(found).toBe("1.8.1")
})
/*
// Debugging/testing locally only don't uncomment
it("works against the real dunlop .sdkmanrc", () => {
const tool = new Java()
const found = tool.parseSdkmanrc("../../turo/dunlop/.sdkmanrc")
expect(found).toBe('8.0.282-librca')
})
*/
})
describe("checkSdkmanSettings", () => {
const target = "/tmp/action-setup-tools.sdkman.config"
const cleaner = new Cleaner(Java, "sdkman", [rcfile])
afterEach(cleaner.clean)
afterEach(() => fs.rmSync(target, { force: true }))
it("writes a fresh config file", () => {
const tool = new Java()
tool.configFile = target
tool.checkSdkmanSettings(target)
expect(fs.existsSync(target)).toBe(true)
expect(fs.readFileSync(target, "utf8")).toMatch(
/sdkman_auto_answer=true/,
)
})
it("overwrites bad settings", () => {
fs.writeFileSync(target, "sdkman_auto_answer=false")
const tool = new Java()
tool.configFile = target
tool.checkSdkmanSettings(target)
expect(fs.existsSync(target)).toBe(true)
expect(fs.readFileSync(target, "utf8")).toMatch(
/sdkman_auto_answer=true/,
)
})
})
describe("getJavaVersion", () => {
const cleaner = new Cleaner(Java, "sdkman", [rcfile])
afterEach(cleaner.clean)
it("works with action input", () => {
const underTest = new Java()
const [checkVersion, isVersionOverridden] = underTest.getJavaVersion(
"1.2.1",
null,
)
expect(checkVersion).toBe("1.2.1")
expect(isVersionOverridden).toBe(true)
})
it("works with .sdkmanrc", () => {
const desiredVersion = "17.0.5-tem"
const fileContents =
"# Enable auto-env through the sdkman_auto_env config\n" +
"# Add key=value pairs of SDKs to use below\n" +
`java=${desiredVersion}\n`
fs.writeFileSync(rcfile, fileContents)
const underTest = new Java()
const [checkVersion, isVersionOverridden] = underTest.getJavaVersion(
null,
rcfile,
)
expect(checkVersion).toBe("17.0.5-tem")
expect(isVersionOverridden).toBe(false)
})
it("works when action desired version and desired version present in dot file", () => {
const desiredVersion = "17.0.3-tem"
const fileContents =
"# Enable auto-env through the sdkman_auto_env config\n" +
"# Add key=value pairs of SDKs to use below\n" +
`java=${desiredVersion}`
fs.writeFileSync(rcfile, fileContents)
const underTest = new Java()
const [checkVersion, isVersionOverridden] = underTest.getJavaVersion(
"1.2.3",
rcfile,
)
expect(checkVersion).toBe("1.2.3")
expect(isVersionOverridden).toBe(true)
})
it("works when empty version present in dot file", () => {
fs.writeFileSync(rcfile, "")
const underTest = new Java()
const [checkVersion, isVersionOverridden] = underTest.getJavaVersion(
null,
rcfile,
)
expect(checkVersion).toBe(null)
expect(isVersionOverridden).toBe(null)
})
it("works when no desired version present", () => {
const underTest = new Java()
const [checkVersion, isVersionOverridden] = underTest.getJavaVersion(
null,
null,
)
expect(checkVersion).toBe(null)
expect(isVersionOverridden).toBe(null)
})
})
describe("versionParser", () => {
const versionString =
`openjdk version "1.8.0_282"\\n` +
`OpenJDK Runtime Environment (build 1.8.0_282-b08)\\n` +
`OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)`
it("handles java 8/1.8 nonsense output", () => {
const tool = new Java()
const version = tool.versionParser(versionString)
expect(version[0]).toBe("8.0.282")
})
it("handles java 11+ versions", () => {
const tool = new Java()
const version = tool.versionParser(`openjdk version "17.0.5_temurin"`)
expect(version[0]).toBe("17.0.5")
})
})