From 7bfdd13cb308940388cb98ac71729dedb2b67379 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Fri, 23 Apr 2021 14:28:39 -0700 Subject: [PATCH] refactor: tmpdir and add to test utils --- test/e2e/models/CodeServer.ts | 2 +- test/e2e/terminal.test.ts | 46 +++++++++++------------------------ test/unit/constants.test.ts | 15 ++++++++++++ test/utils/constants.ts | 11 +++++++++ 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/test/e2e/models/CodeServer.ts b/test/e2e/models/CodeServer.ts index d08e993be..7dc2bd9ae 100644 --- a/test/e2e/models/CodeServer.ts +++ b/test/e2e/models/CodeServer.ts @@ -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 }) } diff --git a/test/e2e/terminal.test.ts b/test/e2e/terminal.test.ts index 53f4dcca6..22a951ca2 100644 --- a/test/e2e/terminal.test.ts +++ b/test/e2e/terminal.test.ts @@ -1,10 +1,10 @@ import { test, expect } from "@playwright/test" +import * as cp from "child_process" 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", () => { @@ -14,8 +14,8 @@ test.describe("Integrated Terminal", () => { const testFileName = "pipe" const testString = "new string test from e2e test" let codeServer: CodeServer - let tmpFolderPath: string = "" - let tmpFile: string = "" + let tmpFolderPath = "" + let tmpFile = "" // TODO@jsjoeio // Fix this once https://github.com/microsoft/playwright-test/issues/240 @@ -26,20 +26,19 @@ test.describe("Integrated Terminal", () => { storageState, } } - 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")) + test.beforeAll(async () => { + tmpFolderPath = await tmpdir("integrated-terminal") tmpFile = path.join(tmpFolderPath, testFileName) }) - test.afterEach(async () => { + test.beforeEach(async ({ page }) => { + codeServer = new CodeServer(page) + await codeServer.setup() + }) + + 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 }) => { @@ -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/) }) }) diff --git a/test/unit/constants.test.ts b/test/unit/constants.test.ts index e4b14a6cd..e07338234 100644 --- a/test/unit/constants.test.ts +++ b/test/unit/constants.test.ts @@ -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. @@ -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.promises.rmdir(pathToTempDir) + }) + }) +}) diff --git a/test/utils/constants.ts b/test/utils/constants.ts index ac2250e1c..a6abd209d 100644 --- a/test/utils/constants.ts +++ b/test/utils/constants.ts @@ -1,3 +1,14 @@ +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" }) +}