Skip to content

Commit

Permalink
✨ feat(core): Add ensureCacheDirExists function
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception committed Oct 3, 2023
1 parent 7015c43 commit 79cda1a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CACHE_DIR_NAME = '.cache-synpress'
9 changes: 9 additions & 0 deletions packages/core/src/ensureCacheDirExists.ts
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './downloadFile'
export * from './unzipArchive'
export * from './ensureCacheDirExists'
39 changes: 39 additions & 0 deletions packages/core/test/ensureCacheDirExists.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit 79cda1a

Please sign in to comment.