Skip to content

Commit 52cf2fc

Browse files
committed
Move tmpdir test helper to test helpers file
1 parent 0e4672f commit 52cf2fc

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,5 +1,3 @@
1-
import * as fs from "fs"
2-
import { tmpdir } from "../../test/utils/constants"
31
import { loggerModule } from "../utils/helpers"
42

53
// jest.mock is hoisted above the imports so we must use `require` here.
@@ -89,16 +87,3 @@ describe("constants", () => {
8987
})
9088
})
9189
})
92-
93-
describe("test constants", () => {
94-
describe("tmpdir", () => {
95-
it("should return a temp directory", async () => {
96-
const testName = "temp-dir"
97-
const pathToTempDir = await tmpdir(testName)
98-
99-
expect(pathToTempDir).toContain(testName)
100-
101-
await fs.promises.rmdir(pathToTempDir)
102-
})
103-
})
104-
})

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)