From 0584183731e7d8356c029ade6e2f76138f12262c Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Mon, 23 Oct 2023 23:09:05 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(core):=20Add=20`createTempCont?= =?UTF-8?q?extDir`=20function=20(#945)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/src/utils/createTempContextDir.ts | 14 ++++ .../test/utils/createTempContextDir.test.ts | 70 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 packages/core/src/utils/createTempContextDir.ts create mode 100644 packages/core/test/utils/createTempContextDir.test.ts diff --git a/packages/core/src/utils/createTempContextDir.ts b/packages/core/src/utils/createTempContextDir.ts new file mode 100644 index 000000000..d3ed872ac --- /dev/null +++ b/packages/core/src/utils/createTempContextDir.ts @@ -0,0 +1,14 @@ +import os from 'node:os' +import path from 'node:path' +import fs from 'fs-extra' + +/** + * Creates a temporary context directory. + * This directory should be created per-worker basis. + * + * This function mirrors the one found in the Playwright source code: + * https://github.com/microsoft/playwright/blob/d1d5fc67dc684a5d4b682749e59bba8cc0ad14de/packages/playwright-core/src/server/browserType.ts#L161 + */ +export async function createTempContextDir(browserName: string, testId: string) { + return await fs.mkdtemp(path.join(os.tmpdir(), `synpress_${browserName}_${testId}_`)) +} diff --git a/packages/core/test/utils/createTempContextDir.test.ts b/packages/core/test/utils/createTempContextDir.test.ts new file mode 100644 index 000000000..bb897f715 --- /dev/null +++ b/packages/core/test/utils/createTempContextDir.test.ts @@ -0,0 +1,70 @@ +import os from 'node:os' +import path from 'node:path' +import { fs, vol } from 'memfs' +import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { createTempContextDir } from '../../src/utils/createTempContextDir' + +const BROWSER_NAME = 'chromium' +const TEST_ID = 'test-id' +const SUFFIX = '123456' + +const ROOT_DIR = '/tmp' +const TEMP_DIR_PATH = path.join(ROOT_DIR, `synpress_${BROWSER_NAME}_${TEST_ID}_${SUFFIX}`) + +vi.mock('fs-extra', async () => { + return { + default: { + ...fs, + mkdtemp: async (prefix: string) => { + const dirName = prefix + SUFFIX + await fs.promises.mkdir(dirName) + return dirName + } + } + } +}) + +vi.mock('node:os', async () => { + return { + default: { + tmpdir: () => ROOT_DIR + } + } +}) + +describe('createTempContextDir', () => { + afterAll(() => { + vi.resetAllMocks() + }) + + beforeEach(() => { + vol.mkdirSync(ROOT_DIR) + }) + + afterEach(() => { + vol.reset() // Clear the in-memory file system after each test + vi.clearAllMocks() + }) + + it('calls os.tmpdir', async () => { + const osTmpDirSpy = vi.spyOn(os, 'tmpdir') + + await createTempContextDir(BROWSER_NAME, TEST_ID) + + expect(osTmpDirSpy).toHaveBeenCalledOnce() + }) + + it('returns path to the directory', async () => { + const tempDirPath = await createTempContextDir(BROWSER_NAME, TEST_ID) + expect(tempDirPath).toEqual(TEMP_DIR_PATH) + }) + + it('creates a directory with the correct name', async () => { + expect(fs.readdirSync(ROOT_DIR)).toHaveLength(0) + + const tempDirPath = await createTempContextDir(BROWSER_NAME, TEST_ID) + + expect(fs.readdirSync(ROOT_DIR)).toHaveLength(1) + expect(fs.existsSync(tempDirPath)).toBeTruthy() + }) +})