setup-cpp/src/gcc/__tests__/gcc.test.ts

75 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-09-04 16:49:14 +08:00
import path, { join } from "path"
2024-09-04 15:51:32 +08:00
import { fileURLToPath } from "url"
2023-04-22 17:19:33 +08:00
import { execaSync } from "execa"
import { chmod } from "fs/promises"
2024-09-04 16:49:14 +08:00
import { addExeExt } from "patha"
import { isUbuntu } from "../../utils/env/isUbuntu.js"
import { ubuntuVersion } from "../../utils/env/ubuntu_version.js"
import { cleanupTmpDir, setupTmpDir, testBin } from "../../utils/tests/test-helpers.js"
import { getVersion } from "../../versions/versions.js"
import { setupGcc } from "../gcc.js"
2021-09-16 22:03:54 +08:00
2024-09-04 15:51:32 +08:00
const dirname = typeof __dirname === "string" ? __dirname : path.dirname(fileURLToPath(import.meta.url))
jest.setTimeout(3000000)
2021-09-16 22:03:54 +08:00
describe("setup-gcc", () => {
2022-05-21 08:07:25 +08:00
let directory: string
beforeAll(async () => {
directory = await setupTmpDir("gcc")
2022-05-21 08:07:25 +08:00
})
2021-09-16 22:03:54 +08:00
it("should setup gcc", async () => {
const ubuntuVersionOutput = await ubuntuVersion()
const version = getVersion("gcc", undefined, ubuntuVersionOutput)
2022-05-21 08:07:25 +08:00
const installInfo = await setupGcc(version, directory, process.arch)
2021-09-16 22:03:54 +08:00
2021-09-17 07:15:25 +08:00
let gpp = "g++"
if (isUbuntu()) {
const ubuntuMajorVersion = ubuntuVersionOutput?.[0]
// https://packages.ubuntu.com/search?keywords=gcc
switch (ubuntuMajorVersion) {
case 26:
case 25:
gpp = "g++-14"
break
case 24:
case 23:
gpp = "g++-13"
break
case 22:
case 21:
gpp = "g++-11"
break
case 20:
gpp = "g++-9"
break
default: {
// ignore
}
}
} else if (process.platform === "darwin") {
// https://formulae.brew.sh/formula/gcc
// As of 3, Sep, 2024
gpp = "g++-14"
2021-09-17 07:15:25 +08:00
}
2021-09-17 07:15:25 +08:00
await testBin(gpp, ["--version"], installInfo?.binDir)
expect(process.env.CC?.includes("gcc")).toBeTruthy()
expect(process.env.CXX?.includes("g++")).toBeTruthy()
// test compilation
2024-09-04 15:51:32 +08:00
const file = join(dirname, "main.cpp")
const main_exe = join(dirname, addExeExt("main"))
execaSync("g++", [file, "-o", main_exe], { cwd: dirname })
if (process.platform !== "win32") {
await chmod(main_exe, "755")
}
2024-09-04 15:51:32 +08:00
execaSync(main_exe, { cwd: dirname, stdio: "inherit" })
2021-09-16 22:03:54 +08:00
})
2022-05-21 08:07:25 +08:00
afterAll(async () => {
await cleanupTmpDir("gcc")
2022-05-21 08:07:25 +08:00
}, 100000)
2021-09-16 22:03:54 +08:00
})