From 04dd26cc880500c9f6cacd42719081e136ce7e66 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Tue, 14 Sep 2021 11:08:23 -0500 Subject: [PATCH] test: add tests for finding the tool in the cache --- src/cmake/__tests__/cmake.test.ts | 36 +++++++++++++++---------------- src/llvm/__tests__/llvm.test.ts | 18 ++++++++-------- src/ninja/__tests__/ninja.test.ts | 35 +++++++++++++++--------------- src/utils/tests/test-helpers.ts | 23 ++++++++++++++++++-- 4 files changed, 64 insertions(+), 48 deletions(-) diff --git a/src/cmake/__tests__/cmake.test.ts b/src/cmake/__tests__/cmake.test.ts index f6ed7917..bcd97d3b 100644 --- a/src/cmake/__tests__/cmake.test.ts +++ b/src/cmake/__tests__/cmake.test.ts @@ -1,32 +1,30 @@ import { setupCmake } from "../cmake" -import { spawnSync as spawn } from "child_process" -import { setupTmpDir, cleanupTmpDir } from "../../utils/tests/test-helpers" -import { addBinExtension } from "../../utils/setup/setupBin" -import { join } from "path" +import { setupTmpDir, cleanupTmpDir, testBin } from "../../utils/tests/test-helpers" jest.setTimeout(100000) +async function testCmake(directory: string) { + const { binDir } = await setupCmake("3.20.2", directory) + testBin("cmake", binDir) + return binDir +} + describe("setup-cmake", () => { let directory: string - beforeEach(async () => { + beforeAll(async () => { 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 () => { await cleanupTmpDir("setup-cmake") }, 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) - }) }) diff --git a/src/llvm/__tests__/llvm.test.ts b/src/llvm/__tests__/llvm.test.ts index b33254af..a7fb9ee5 100644 --- a/src/llvm/__tests__/llvm.test.ts +++ b/src/llvm/__tests__/llvm.test.ts @@ -16,6 +16,11 @@ async function testUrl(version: string) { } describe("setup-llvm", () => { + let directory: string + beforeAll(async () => { + directory = await setupTmpDir("setup-llvm") + }) + it("Finds valid LLVM URLs", async () => { 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 () => { const { binDir } = await setupLLVM("12.0.0", directory) expect(binDir).toBeDefined() @@ -60,4 +56,8 @@ describe("setup-llvm", () => { }) expect(status).toBe(0) }) + + afterAll(async () => { + await cleanupTmpDir("setup-llvm") + }, 100000) }) diff --git a/src/ninja/__tests__/ninja.test.ts b/src/ninja/__tests__/ninja.test.ts index 2f90fdeb..5df2fd30 100644 --- a/src/ninja/__tests__/ninja.test.ts +++ b/src/ninja/__tests__/ninja.test.ts @@ -1,31 +1,30 @@ import { setupNinja } from "../ninja" -import { spawnSync as spawn } from "child_process" -import { setupTmpDir, cleanupTmpDir } from "../../utils/tests/test-helpers" -import { addBinExtension } from "../../utils/setup/setupBin" -import { join } from "path" +import { setupTmpDir, cleanupTmpDir, testBin } from "../../utils/tests/test-helpers" jest.setTimeout(100000) +async function testNinja(directory: string) { + const { binDir } = await setupNinja("1.10.2", directory) + testBin("ninja", binDir) + return binDir +} + describe("setup-ninja", () => { let directory: string beforeEach(async () => { 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") }, 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) - }) }) diff --git a/src/utils/tests/test-helpers.ts b/src/utils/tests/test-helpers.ts index 9e7faab3..389d06d1 100644 --- a/src/utils/tests/test-helpers.ts +++ b/src/utils/tests/test-helpers.ts @@ -1,14 +1,19 @@ import * as io from "@actions/io" import { tmpdir } from "os" 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) { const tempDirectory = path.join(tmpdir(), "setup-cpp", testName) - await io.rmRF(tempDirectory) await io.mkdirP(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 } @@ -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 +}