test: refactor setup and cleanup helpers

This commit is contained in:
Amin Yahyaabadi 2021-09-14 03:26:53 -05:00
parent 41200c9f2f
commit 0a3aa6cc9c
3 changed files with 34 additions and 36 deletions

View File

@ -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 { setupCmake } from "../cmake"
import { spawnSync as spawn } from "child_process" import { spawnSync as spawn } from "child_process"
import { tmpdir } from "os" import { setupTmpDir, cleanupTmpDir } from "../../utils/tests/test-helpers"
jest.setTimeout(30 * 1000) jest.setTimeout(30 * 1000)
const tempDirectory = path.join(tmpdir(), "setup-cpp", "setup-cmake")
describe("setup-cmake", () => { describe("setup-cmake", () => {
beforeEach(async () => { beforeEach(async () => {
await io.rmRF(tempDirectory) await setupTmpDir("setup-cmake")
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")
}) })
afterAll(async () => { afterAll(async () => {
try { await cleanupTmpDir("setup-cmake")
await io.rmRF(tempDirectory)
} catch {
console.log("Failed to remove test directories")
}
}, 100000) }, 100000)
it("should download CMake", async () => { it("should setup CMake", async () => {
const cmakePath = await setupCmake("3.20.2") const cmakePath = await setupCmake("3.20.2")
expect(cmakePath).toBeDefined() expect(cmakePath).toBeDefined()
expect(cmakePath).not.toHaveLength(0) expect(cmakePath).not.toHaveLength(0)

View File

@ -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 { setupNinja } from "../ninja"
import { spawnSync as spawn } from "child_process" import { spawnSync as spawn } from "child_process"
import { tmpdir } from "os" import { setupTmpDir, cleanupTmpDir } from "../../utils/tests/test-helpers"
const tempDirectory = path.join(tmpdir(), "setup-cpp", "setup-ninja")
jest.setTimeout(30 * 1000) jest.setTimeout(30 * 1000)
describe("setup-ninja", () => { describe("setup-ninja", () => {
beforeEach(async () => { beforeEach(async () => {
await io.rmRF(tempDirectory) await setupTmpDir("setup-cmake")
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")
}) })
afterAll(async () => { afterAll(async () => {
try { await cleanupTmpDir("setup-cmake")
await io.rmRF(tempDirectory)
} catch {
console.error("Failed to remove test directories")
}
}, 100000) }, 100000)
it("should fetch Ninja 1.10.2", async () => { it("should setup Ninja", async () => {
const ninjaPath = await setupNinja("1.10.2") const ninjaPath = await setupNinja("1.10.2")
expect(ninjaPath).toBeDefined() expect(ninjaPath).toBeDefined()
expect(ninjaPath).not.toHaveLength(0) expect(ninjaPath).not.toHaveLength(0)

View File

@ -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")
}
}