From 86dcb4e6012cda4570478c96dee851b9f0c114d7 Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Tue, 3 Oct 2023 21:27:25 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(core):=20Add=20`ensureCacheDir?= =?UTF-8?q?Exists`=20function=20(#906)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/constants.ts | 1 + packages/core/src/ensureCacheDirExists.ts | 9 +++++ packages/core/src/index.ts | 1 + .../core/test/ensureCacheDirExists.test.ts | 39 +++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 packages/core/src/constants.ts create mode 100644 packages/core/src/ensureCacheDirExists.ts create mode 100644 packages/core/test/ensureCacheDirExists.test.ts diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts new file mode 100644 index 000000000..5a9c846de --- /dev/null +++ b/packages/core/src/constants.ts @@ -0,0 +1 @@ +export const CACHE_DIR_NAME = '.cache-synpress' diff --git a/packages/core/src/ensureCacheDirExists.ts b/packages/core/src/ensureCacheDirExists.ts new file mode 100644 index 000000000..00b5caf82 --- /dev/null +++ b/packages/core/src/ensureCacheDirExists.ts @@ -0,0 +1,9 @@ +import path from 'node:path' +import fs from 'fs-extra' +import { CACHE_DIR_NAME } from './constants' + +export function ensureCacheDirExists() { + const cacheDirPath = path.join(process.cwd(), CACHE_DIR_NAME) + fs.ensureDirSync(cacheDirPath) + return cacheDirPath +} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 1bc07af56..46051c3bd 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,2 +1,3 @@ export * from './downloadFile' export * from './unzipArchive' +export * from './ensureCacheDirExists' diff --git a/packages/core/test/ensureCacheDirExists.test.ts b/packages/core/test/ensureCacheDirExists.test.ts new file mode 100644 index 000000000..2ef6005c3 --- /dev/null +++ b/packages/core/test/ensureCacheDirExists.test.ts @@ -0,0 +1,39 @@ +import path from 'node:path' +import fs from 'fs-extra' +import { afterAll, afterEach, describe, expect, it, vi } from 'vitest' +import { CACHE_DIR_NAME } from '../src/constants' +import { ensureCacheDirExists } from '../src/ensureCacheDirExists' + +vi.mock('fs-extra', async () => { + return { + default: { + ensureDirSync: vi.fn() + } + } +}) + +const EXPECTED_CACHE_DIR_PATH = path.join(process.cwd(), CACHE_DIR_NAME) + +describe('ensureCacheDirExists', () => { + afterEach(() => { + vi.clearAllMocks() + }) + + afterAll(() => { + vi.resetAllMocks() + }) + + it('calls `fs.ensureDirSync` with correct cache directory path', async () => { + const ensureDirSyncSpy = vi.spyOn(fs, 'ensureDirSync') + + ensureCacheDirExists() + + expect(ensureDirSyncSpy).toHaveBeenCalledOnce() + expect(ensureDirSyncSpy).toHaveBeenCalledWith(EXPECTED_CACHE_DIR_PATH) + }) + + it('returns the cache directory path', async () => { + const cacheDirPath = ensureCacheDirExists() + expect(cacheDirPath).toEqual(EXPECTED_CACHE_DIR_PATH) + }) +})