From 52cf2fcf29400ecc1f1ab196920e440cff0aba40 Mon Sep 17 00:00:00 2001 From: Asher Date: Wed, 5 May 2021 11:02:34 -0500 Subject: [PATCH] Move tmpdir test helper to test helpers file --- test/e2e/terminal.test.ts | 6 +++--- test/unit/constants.test.ts | 15 --------------- test/unit/helpers.test.ts | 14 ++++++++++++++ test/utils/constants.ts | 11 ----------- test/utils/helpers.ts | 16 ++++++++++++++++ 5 files changed, 33 insertions(+), 29 deletions(-) create mode 100644 test/unit/helpers.test.ts diff --git a/test/e2e/terminal.test.ts b/test/e2e/terminal.test.ts index f92419c6..15fd5394 100644 --- a/test/e2e/terminal.test.ts +++ b/test/e2e/terminal.test.ts @@ -1,10 +1,10 @@ -import { test, expect } from "@playwright/test" +import { expect, test } from "@playwright/test" import * as cp from "child_process" import * as fs from "fs" -// import { tmpdir } from "os" import * as path from "path" import util from "util" -import { STORAGE, tmpdir } from "../utils/constants" +import { STORAGE } from "../utils/constants" +import { tmpdir } from "../utils/helpers" import { CodeServer } from "./models/CodeServer" test.describe("Integrated Terminal", () => { diff --git a/test/unit/constants.test.ts b/test/unit/constants.test.ts index d7d3c66b..88654457 100644 --- a/test/unit/constants.test.ts +++ b/test/unit/constants.test.ts @@ -1,5 +1,3 @@ -import * as fs from "fs" -import { tmpdir } from "../../test/utils/constants" import { loggerModule } from "../utils/helpers" // jest.mock is hoisted above the imports so we must use `require` here. @@ -89,16 +87,3 @@ describe("constants", () => { }) }) }) - -describe("test constants", () => { - describe("tmpdir", () => { - it("should return a temp directory", async () => { - const testName = "temp-dir" - const pathToTempDir = await tmpdir(testName) - - expect(pathToTempDir).toContain(testName) - - await fs.promises.rmdir(pathToTempDir) - }) - }) -}) diff --git a/test/unit/helpers.test.ts b/test/unit/helpers.test.ts new file mode 100644 index 00000000..74485475 --- /dev/null +++ b/test/unit/helpers.test.ts @@ -0,0 +1,14 @@ +import { promises as fs } from "fs" +import { tmpdir } from "../../test/utils/helpers" + +/** + * This file is for testing test helpers (not core code). + */ +describe("test helpers", () => { + it("should return a temp directory", async () => { + const testName = "temp-dir" + const pathToTempDir = await tmpdir(testName) + expect(pathToTempDir).toContain(testName) + expect(fs.access(pathToTempDir)).resolves.toStrictEqual(undefined) + }) +}) diff --git a/test/utils/constants.ts b/test/utils/constants.ts index a6abd209..ac2250e1 100644 --- a/test/utils/constants.ts +++ b/test/utils/constants.ts @@ -1,14 +1,3 @@ -import * as fs from "fs" -import * as os from "os" -import * as path from "path" - export const CODE_SERVER_ADDRESS = process.env.CODE_SERVER_ADDRESS || "http://localhost:8080" export const PASSWORD = process.env.PASSWORD || "e45432jklfdsab" export const STORAGE = process.env.STORAGE || "" - -export async function tmpdir(testName: string): Promise { - const dir = path.join(os.tmpdir(), "code-server") - await fs.promises.mkdir(dir, { recursive: true }) - - return await fs.promises.mkdtemp(path.join(dir, `test-${testName}-`), { encoding: "utf8" }) -} diff --git a/test/utils/helpers.ts b/test/utils/helpers.ts index b4580401..96a772b9 100644 --- a/test/utils/helpers.ts +++ b/test/utils/helpers.ts @@ -1,3 +1,7 @@ +import * as fs from "fs" +import * as os from "os" +import * as path from "path" + export const loggerModule = { field: jest.fn(), level: 2, @@ -9,3 +13,15 @@ export const loggerModule = { warn: jest.fn(), }, } + +/** + * Create a uniquely named temporary directory. + * + * These directories are placed under a single temporary code-server directory. + */ +export async function tmpdir(testName: string): Promise { + const dir = path.join(os.tmpdir(), "code-server") + await fs.promises.mkdir(dir, { recursive: true }) + + return await fs.promises.mkdtemp(path.join(dir, `test-${testName}-`), { encoding: "utf8" }) +}