-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a37baa5
commit f2fb5a9
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import fs from 'fs-extra' | ||
|
||
export async function isDirEmpty(dirPath: string) { | ||
const files = await fs.readdir(dirPath) | ||
|
||
return files.length === 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import path from 'node:path' | ||
import { fs, vol } from 'memfs' | ||
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { isDirEmpty } from '../../src/utils/isDirEmpty' | ||
|
||
const ROOT_DIR = '/tmp' | ||
const FILE_PATH = path.join(ROOT_DIR, 'file.txt') | ||
|
||
vi.mock('fs-extra', async () => { | ||
return { | ||
default: fs.promises | ||
} | ||
}) | ||
|
||
describe('isDirEmpty', () => { | ||
afterAll(() => { | ||
vi.resetAllMocks() | ||
}) | ||
|
||
beforeEach(() => { | ||
vol.mkdirSync(ROOT_DIR) | ||
}) | ||
|
||
afterEach(() => { | ||
vol.reset() // Clear the in-memory file system after each test | ||
}) | ||
|
||
it('returns `true` if the directory is empty', async () => { | ||
const isEmpty = await isDirEmpty(ROOT_DIR) | ||
expect(isEmpty).toEqual(true) | ||
}) | ||
|
||
it('returns `false` if the directory contains files', async () => { | ||
fs.writeFileSync(FILE_PATH, 'Hello there! 👋') | ||
|
||
const isEmpty = await isDirEmpty(ROOT_DIR) | ||
expect(isEmpty).toEqual(false) | ||
}) | ||
}) |