Archived
1
0

refactor: tmpdir and add to test utils

This commit is contained in:
Joe Previte 2021-04-23 14:28:39 -07:00
parent b0ecff338f
commit 7bfdd13cb3
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
4 changed files with 41 additions and 33 deletions

View File

@ -47,7 +47,7 @@ export class CodeServer {
*/ */
async isEditorVisible() { async isEditorVisible() {
// Make sure the editor actually loaded // 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") await this.page.waitForLoadState("networkidle")
return await this.page.isVisible("div.monaco-workbench", { timeout: 5000 }) return await this.page.isVisible("div.monaco-workbench", { timeout: 5000 })
} }

View File

@ -1,10 +1,10 @@
import { test, expect } from "@playwright/test" import { test, expect } from "@playwright/test"
import * as cp from "child_process"
import * as fs from "fs" import * as fs from "fs"
import { tmpdir } from "os" // import { tmpdir } from "os"
import * as path from "path" import * as path from "path"
import util from "util" import util from "util"
import * as cp from "child_process" import { STORAGE, tmpdir } from "../utils/constants"
import { STORAGE } from "../utils/constants"
import { CodeServer } from "./models/CodeServer" import { CodeServer } from "./models/CodeServer"
test.describe("Integrated Terminal", () => { test.describe("Integrated Terminal", () => {
@ -14,8 +14,8 @@ test.describe("Integrated Terminal", () => {
const testFileName = "pipe" const testFileName = "pipe"
const testString = "new string test from e2e test" const testString = "new string test from e2e test"
let codeServer: CodeServer let codeServer: CodeServer
let tmpFolderPath: string = "" let tmpFolderPath = ""
let tmpFile: string = "" let tmpFile = ""
// TODO@jsjoeio // TODO@jsjoeio
// Fix this once https://github.com/microsoft/playwright-test/issues/240 // Fix this once https://github.com/microsoft/playwright-test/issues/240
@ -26,20 +26,19 @@ test.describe("Integrated Terminal", () => {
storageState, storageState,
} }
} }
test.beforeEach(async ({ page }) => { test.beforeAll(async () => {
codeServer = new CodeServer(page) tmpFolderPath = await tmpdir("integrated-terminal")
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) 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 // 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 }) => { test("should echo a string to a file", options, async ({ page }) => {
@ -56,22 +55,5 @@ test.describe("Integrated Terminal", () => {
const { stdout } = await output const { stdout } = await output
expect(stdout).toMatch(testString) 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/)
}) })
}) })

View File

@ -1,4 +1,6 @@
import * as fs from "fs"
import { commit, getPackageJson, version } from "../../src/node/constants" import { commit, getPackageJson, version } from "../../src/node/constants"
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.
@ -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)
})
})
})

View File

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