Skip to content

Commit

Permalink
refactor: tmpdir and add to test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jsjoeio committed Apr 23, 2021
1 parent b0ecff3 commit f40f468
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion test/e2e/models/CodeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class CodeServer {
*/
async isEditorVisible() {
// Make sure the editor actually loaded
// If it's not visible after 2 seconds, something is wrong
// If it's not visible after 5 seconds, something is wrong
await this.page.waitForLoadState("networkidle")
return await this.page.isVisible("div.monaco-workbench", { timeout: 5000 })
}
Expand Down
36 changes: 9 additions & 27 deletions test/e2e/terminal.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { test, expect } from "@playwright/test"
import * as fs from "fs"
import { tmpdir } from "os"
// import { tmpdir } from "os"
import * as path from "path"
import util from "util"
import * as cp from "child_process"
import { STORAGE } from "../utils/constants"
import { STORAGE, tmpdir } from "../utils/constants"
import { CodeServer } from "./models/CodeServer"

test.describe("Integrated Terminal", () => {
Expand All @@ -26,20 +26,19 @@ test.describe("Integrated Terminal", () => {
storageState,
}
}
test.beforeAll(async () => {
tmpFolderPath = await tmpdir("integrated-terminal")
tmpFile = path.join(tmpFolderPath, testFileName)
})

test.beforeEach(async ({ page }) => {
codeServer = new CodeServer(page)
await codeServer.setup()
// NOTE@jsjoeio
// We're not using tmpdir from src/node/constants
// because Playwright doesn't fully support ES modules from
// the erorrs I'm seeing
tmpFolderPath = fs.mkdtempSync(path.join(tmpdir(), "code-server-test"))
tmpFile = path.join(tmpFolderPath, testFileName)
})

test.afterEach(async () => {
test.afterAll(async () => {
// Ensure directory was removed
fs.rmdirSync(tmpFolderPath, { recursive: true })
await fs.promises.rmdir(tmpFolderPath, { recursive: true })
})

test("should echo a string to a file", options, async ({ page }) => {
Expand All @@ -56,22 +55,5 @@ test.describe("Integrated Terminal", () => {

const { stdout } = await output
expect(stdout).toMatch(testString)

// .access checks if the file exists without opening it
// it doesn't return anything hence why we expect it to
// resolve to undefined
// If the promise rejects (i.e. the file doesn't exist)
// then the assertion will fail
await expect(fs.promises.access(tmpFile)).resolves.toBeUndefined()

await fs.promises.rmdir(tmpFolderPath, { recursive: true })
// Make sure neither file nor folder exist
// Note: We have to use ts-ignore because of an upstream typing error
// See: https://github.com/microsoft/folio/issues/230#event-4621948411
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
expect(fs.promises.access(tmpFile)).rejects.toThrowError(/no such file or directory/)
// @ts-ignore
expect(fs.promises.access(tmpFolderPath)).rejects.toThrowError(/no such file or directory/)
})
})
15 changes: 15 additions & 0 deletions test/unit/constants.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as fs from "fs"
import { commit, getPackageJson, version } from "../../src/node/constants"
import { tmpdir } from "../../test/utils/constants"
import { loggerModule } from "../utils/helpers"

// jest.mock is hoisted above the imports so we must use `require` here.
Expand Down Expand Up @@ -51,3 +53,16 @@ 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.rmdir(pathToTempDir, () => {})
})
})
})
11 changes: 11 additions & 0 deletions test/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import * as os from "os"
import * as path from "path"
import * as fs from "fs"

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<string> {
const dir = path.join(os.tmpdir(), "code-server")
await fs.mkdir(dir, { recursive: true }, () => {})

return await fs.promises.mkdtemp(path.join(dir, `test-${testName}-`), { encoding: "utf8" })
}

0 comments on commit f40f468

Please sign in to comment.