Archived
1
0

Move tmpdir test helper to test helpers file

This commit is contained in:
Asher 2021-05-05 11:02:34 -05:00
parent 0e4672f6b9
commit 52cf2fcf29
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
5 changed files with 33 additions and 29 deletions

View File

@ -1,10 +1,10 @@
import { test, expect } from "@playwright/test" import { expect, test } from "@playwright/test"
import * as cp from "child_process" import * as cp from "child_process"
import * as fs from "fs" import * as fs from "fs"
// import { tmpdir } from "os"
import * as path from "path" import * as path from "path"
import util from "util" 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" import { CodeServer } from "./models/CodeServer"
test.describe("Integrated Terminal", () => { test.describe("Integrated Terminal", () => {

View File

@ -1,5 +1,3 @@
import * as fs from "fs"
import { tmpdir } from "../../test/utils/constants"
import { loggerModule } from "../utils/helpers" import { loggerModule } from "../utils/helpers"
// jest.mock is hoisted above the imports so we must use `require` here. // 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)
})
})
})

14
test/unit/helpers.test.ts Normal file
View File

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

View File

@ -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 CODE_SERVER_ADDRESS = process.env.CODE_SERVER_ADDRESS || "http://localhost:8080"
export const PASSWORD = process.env.PASSWORD || "e45432jklfdsab" export const PASSWORD = process.env.PASSWORD || "e45432jklfdsab"
export const STORAGE = process.env.STORAGE || "" export const STORAGE = process.env.STORAGE || ""
export async function tmpdir(testName: string): Promise<string> {
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" })
}

View File

@ -1,3 +1,7 @@
import * as fs from "fs"
import * as os from "os"
import * as path from "path"
export const loggerModule = { export const loggerModule = {
field: jest.fn(), field: jest.fn(),
level: 2, level: 2,
@ -9,3 +13,15 @@ export const loggerModule = {
warn: jest.fn(), 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<string> {
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" })
}