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) + }) +})