test: add tests for finding the tool in the cache

This commit is contained in:
Amin Yahyaabadi 2021-09-14 11:08:23 -05:00
parent 22ae571908
commit 04dd26cc88
4 changed files with 64 additions and 48 deletions

View File

@ -1,32 +1,30 @@
import { setupCmake } from "../cmake" import { setupCmake } from "../cmake"
import { spawnSync as spawn } from "child_process" import { setupTmpDir, cleanupTmpDir, testBin } from "../../utils/tests/test-helpers"
import { setupTmpDir, cleanupTmpDir } from "../../utils/tests/test-helpers"
import { addBinExtension } from "../../utils/setup/setupBin"
import { join } from "path"
jest.setTimeout(100000) jest.setTimeout(100000)
async function testCmake(directory: string) {
const { binDir } = await setupCmake("3.20.2", directory)
testBin("cmake", binDir)
return binDir
}
describe("setup-cmake", () => { describe("setup-cmake", () => {
let directory: string let directory: string
beforeEach(async () => { beforeAll(async () => {
directory = await setupTmpDir("setup-cmake") directory = await setupTmpDir("setup-cmake")
}) })
it("should setup CMake", async () => {
await testCmake(directory)
})
it("should find CMake in the cache", async () => {
const binDir = await testCmake(directory)
expect(binDir.includes("ToolCache")).toBeTruthy()
})
afterAll(async () => { afterAll(async () => {
await cleanupTmpDir("setup-cmake") await cleanupTmpDir("setup-cmake")
}, 100000) }, 100000)
it("should setup CMake", async () => {
const { binDir } = await setupCmake("3.20.2", directory)
expect(binDir).toBeDefined()
expect(binDir).not.toHaveLength(0)
const cmakeBin = join(binDir, addBinExtension("cmake"))
const { status, error } = spawn(cmakeBin, ["--version"], {
encoding: "utf8",
})
expect(error).toBeUndefined()
expect(status).toBe(0)
})
}) })

View File

@ -16,6 +16,11 @@ async function testUrl(version: string) {
} }
describe("setup-llvm", () => { describe("setup-llvm", () => {
let directory: string
beforeAll(async () => {
directory = await setupTmpDir("setup-llvm")
})
it("Finds valid LLVM URLs", async () => { it("Finds valid LLVM URLs", async () => {
await Promise.all( await Promise.all(
[ [
@ -39,15 +44,6 @@ describe("setup-llvm", () => {
) )
}) })
let directory: string
beforeAll(async () => {
directory = await setupTmpDir("setup-llvm")
})
afterAll(async () => {
await cleanupTmpDir("setup-llvm")
}, 100000)
it("should setup LLVM", async () => { it("should setup LLVM", async () => {
const { binDir } = await setupLLVM("12.0.0", directory) const { binDir } = await setupLLVM("12.0.0", directory)
expect(binDir).toBeDefined() expect(binDir).toBeDefined()
@ -60,4 +56,8 @@ describe("setup-llvm", () => {
}) })
expect(status).toBe(0) expect(status).toBe(0)
}) })
afterAll(async () => {
await cleanupTmpDir("setup-llvm")
}, 100000)
}) })

View File

@ -1,31 +1,30 @@
import { setupNinja } from "../ninja" import { setupNinja } from "../ninja"
import { spawnSync as spawn } from "child_process" import { setupTmpDir, cleanupTmpDir, testBin } from "../../utils/tests/test-helpers"
import { setupTmpDir, cleanupTmpDir } from "../../utils/tests/test-helpers"
import { addBinExtension } from "../../utils/setup/setupBin"
import { join } from "path"
jest.setTimeout(100000) jest.setTimeout(100000)
async function testNinja(directory: string) {
const { binDir } = await setupNinja("1.10.2", directory)
testBin("ninja", binDir)
return binDir
}
describe("setup-ninja", () => { describe("setup-ninja", () => {
let directory: string let directory: string
beforeEach(async () => { beforeEach(async () => {
directory = await setupTmpDir("setup-ninja") directory = await setupTmpDir("setup-ninja")
}) })
afterAll(async () => { it("should setup Ninja", async () => {
await testNinja(directory)
})
it("should find Ninja in the cache", async () => {
const binDir = await testNinja(directory)
expect(binDir.includes("ToolCache")).toBeTruthy()
})
afterEach(async () => {
await cleanupTmpDir("setup-ninja") await cleanupTmpDir("setup-ninja")
}, 100000) }, 100000)
it("should setup Ninja", async () => {
const { binDir } = await setupNinja("1.10.2", directory)
expect(binDir).toBeDefined()
expect(binDir).not.toHaveLength(0)
const ninjaBin = join(binDir, addBinExtension("ninja"))
const { status } = spawn(ninjaBin, ["--version"], {
encoding: "utf8",
})
expect(status).toBe(0)
})
}) })

View File

@ -1,14 +1,19 @@
import * as io from "@actions/io" import * as io from "@actions/io"
import { tmpdir } from "os" import { tmpdir } from "os"
import * as path from "path" import * as path from "path"
import { spawnSync as spawn } from "child_process"
import { addBinExtension } from "../setup/setupBin"
import { join } from "path"
export async function setupTmpDir(testName: string) { export async function setupTmpDir(testName: string) {
const tempDirectory = path.join(tmpdir(), "setup-cpp", testName) const tempDirectory = path.join(tmpdir(), "setup-cpp", testName)
await io.rmRF(tempDirectory) await io.rmRF(tempDirectory)
await io.mkdirP(tempDirectory) await io.mkdirP(tempDirectory)
process.env.SETUP_CPP_DIR = tempDirectory process.env.SETUP_CPP_DIR = tempDirectory
process.env.RUNNER_TOOL_CACHE = process.env.RUNNER_TOOL_CACH ?? path.join(tempDirectory, "ToolCache")
const toolCache = path.join(tmpdir(), "setup-cpp", "ToolCache")
process.env.RUNNER_TOOL_CACHE = process.env.RUNNER_TOOL_CACH ?? toolCache
return tempDirectory return tempDirectory
} }
@ -23,3 +28,17 @@ export async function cleanupTmpDir(testName: string) {
} }
} }
} }
export function testBin(binName: string, binDir: string) {
expect(binDir).toBeDefined()
expect(binDir).not.toHaveLength(0)
const cmakeBin = join(binDir, addBinExtension(binName))
const { status } = spawn(cmakeBin, ["--version"], {
encoding: "utf8",
})
expect(status).toBe(0)
return binDir
}