From 0a3aa6cc9c569fb7c6ed99f142d90162386be115 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Tue, 14 Sep 2021 03:26:53 -0500 Subject: [PATCH] test: refactor setup and cleanup helpers --- src/cmake/__tests__/cmake.test.ts | 22 ++++------------------ src/ninja/__tests__/ninja.test.ts | 22 ++++------------------ src/utils/tests/test-helpers.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 36 deletions(-) create mode 100644 src/utils/tests/test-helpers.ts diff --git a/src/cmake/__tests__/cmake.test.ts b/src/cmake/__tests__/cmake.test.ts index 1c0a9ec0..38c0c47a 100644 --- a/src/cmake/__tests__/cmake.test.ts +++ b/src/cmake/__tests__/cmake.test.ts @@ -1,33 +1,19 @@ -import * as process from "process" -import * as path from "path" -import * as io from "@actions/io" import { setupCmake } from "../cmake" import { spawnSync as spawn } from "child_process" -import { tmpdir } from "os" +import { setupTmpDir, cleanupTmpDir } from "../../utils/tests/test-helpers" jest.setTimeout(30 * 1000) -const tempDirectory = path.join(tmpdir(), "setup-cpp", "setup-cmake") - describe("setup-cmake", () => { beforeEach(async () => { - await io.rmRF(tempDirectory) - await io.mkdirP(tempDirectory) - process.env.INPUT_DESTINATION = tempDirectory - process.env.GITHUB_WORKSPACE = tempDirectory - process.env.RUNNER_TEMP = path.join(tempDirectory, "temp") - process.env.RUNNER_TOOL_CACHE = path.join(tempDirectory, "tempToolCache") + await setupTmpDir("setup-cmake") }) afterAll(async () => { - try { - await io.rmRF(tempDirectory) - } catch { - console.log("Failed to remove test directories") - } + await cleanupTmpDir("setup-cmake") }, 100000) - it("should download CMake", async () => { + it("should setup CMake", async () => { const cmakePath = await setupCmake("3.20.2") expect(cmakePath).toBeDefined() expect(cmakePath).not.toHaveLength(0) diff --git a/src/ninja/__tests__/ninja.test.ts b/src/ninja/__tests__/ninja.test.ts index 3649d148..5d7bfeb5 100644 --- a/src/ninja/__tests__/ninja.test.ts +++ b/src/ninja/__tests__/ninja.test.ts @@ -1,33 +1,19 @@ -import * as process from "process" -import * as path from "path" -import * as io from "@actions/io" import { setupNinja } from "../ninja" import { spawnSync as spawn } from "child_process" -import { tmpdir } from "os" - -const tempDirectory = path.join(tmpdir(), "setup-cpp", "setup-ninja") +import { setupTmpDir, cleanupTmpDir } from "../../utils/tests/test-helpers" jest.setTimeout(30 * 1000) describe("setup-ninja", () => { beforeEach(async () => { - await io.rmRF(tempDirectory) - await io.mkdirP(tempDirectory) - process.env.INPUT_DESTINATION = tempDirectory - process.env.GITHUB_WORKSPACE = tempDirectory - process.env.RUNNER_TEMP = path.join(tempDirectory, "temp") - process.env.RUNNER_TOOL_CACHE = path.join(tempDirectory, "tempToolCache") + await setupTmpDir("setup-cmake") }) afterAll(async () => { - try { - await io.rmRF(tempDirectory) - } catch { - console.error("Failed to remove test directories") - } + await cleanupTmpDir("setup-cmake") }, 100000) - it("should fetch Ninja 1.10.2", async () => { + it("should setup Ninja", async () => { const ninjaPath = await setupNinja("1.10.2") expect(ninjaPath).toBeDefined() expect(ninjaPath).not.toHaveLength(0) diff --git a/src/utils/tests/test-helpers.ts b/src/utils/tests/test-helpers.ts new file mode 100644 index 00000000..dde719f8 --- /dev/null +++ b/src/utils/tests/test-helpers.ts @@ -0,0 +1,26 @@ +import * as io from "@actions/io" +import { tmpdir } from "os" +import * as path 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.INPUT_DESTINATION = tempDirectory + process.env.GITHUB_WORKSPACE = tempDirectory + process.env.RUNNER_TEMP = path.join(tempDirectory, "temp") + process.env.RUNNER_TOOL_CACHE = path.join(tempDirectory, "tempToolCache") + + return tempDirectory +} + +export async function cleanupTmpDir(testName: string) { + const tempDirectory = path.join(tmpdir(), "setup-cpp", testName) + + try { + await io.rmRF(tempDirectory) + } catch { + console.log("Failed to remove test directories") + } +}