Skip to content

Commit c07f670

Browse files
committed
Move tmpdir test helper to test helpers file
1 parent c1a4c78 commit c07f670

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

test/e2e/terminal.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { test, expect } from "@playwright/test"
1+
import { expect, test } from "@playwright/test"
22
import * as cp from "child_process"
33
import * as fs from "fs"
4-
// import { tmpdir } from "os"
54
import * as path from "path"
65
import util from "util"
7-
import { STORAGE, tmpdir } from "../utils/constants"
6+
import { STORAGE } from "../utils/constants"
7+
import { tmpdir } from "../utils/helpers"
88
import { CodeServer } from "./models/CodeServer"
99

1010
test.describe("Integrated Terminal", () => {

test/unit/constants.test.ts

-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as fs from "fs"
21
import { commit, getPackageJson, version } from "../../src/node/constants"
3-
import { tmpdir } from "../../test/utils/constants"
42
import { loggerModule } from "../utils/helpers"
53

64
// jest.mock is hoisted above the imports so we must use `require` here.
@@ -53,16 +51,3 @@ describe("constants", () => {
5351
})
5452
})
5553
})
56-
57-
describe("test constants", () => {
58-
describe("tmpdir", () => {
59-
it("should return a temp directory", async () => {
60-
const testName = "temp-dir"
61-
const pathToTempDir = await tmpdir(testName)
62-
63-
expect(pathToTempDir).toContain(testName)
64-
65-
await fs.promises.rmdir(pathToTempDir)
66-
})
67-
})
68-
})

test/unit/helpers.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { promises as fs } from "fs"
2+
import { tmpdir } from "../../test/utils/helpers"
3+
4+
/**
5+
* This file is for testing test helpers (not core code).
6+
*/
7+
describe("test helpers", () => {
8+
it("should return a temp directory", async () => {
9+
const testName = "temp-dir"
10+
const pathToTempDir = await tmpdir(testName)
11+
expect(pathToTempDir).toContain(testName)
12+
expect(fs.access(pathToTempDir)).resolves.toStrictEqual(undefined)
13+
})
14+
})

test/utils/constants.ts

-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
import * as fs from "fs"
2-
import * as os from "os"
3-
import * as path from "path"
4-
51
export const CODE_SERVER_ADDRESS = process.env.CODE_SERVER_ADDRESS || "http://localhost:8080"
62
export const PASSWORD = process.env.PASSWORD || "e45432jklfdsab"
73
export const STORAGE = process.env.STORAGE || ""
8-
9-
export async function tmpdir(testName: string): Promise<string> {
10-
const dir = path.join(os.tmpdir(), "code-server")
11-
await fs.promises.mkdir(dir, { recursive: true })
12-
13-
return await fs.promises.mkdtemp(path.join(dir, `test-${testName}-`), { encoding: "utf8" })
14-
}

test/utils/helpers.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import * as fs from "fs"
2+
import * as os from "os"
3+
import * as path from "path"
4+
15
export const loggerModule = {
26
field: jest.fn(),
37
level: 2,
@@ -9,3 +13,15 @@ export const loggerModule = {
913
warn: jest.fn(),
1014
},
1115
}
16+
17+
/**
18+
* Create a uniquely named temporary directory.
19+
*
20+
* These directories are placed under a single temporary code-server directory.
21+
*/
22+
export async function tmpdir(testName: string): Promise<string> {
23+
const dir = path.join(os.tmpdir(), "code-server")
24+
await fs.promises.mkdir(dir, { recursive: true })
25+
26+
return await fs.promises.mkdtemp(path.join(dir, `test-${testName}-`), { encoding: "utf8" })
27+
}

0 commit comments

Comments
 (0)