mirror of
https://github.com/pnpm/pnpm.git
synced 2026-04-28 11:01:30 -04:00
* fix: reject empty files in isEmptyDirOrNothing * test(fs.is-empty-dir-or-nothing): isolate empty-file test in its own temp dir
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import fs from 'node:fs'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
|
|
import { isEmptyDirOrNothing } from '@pnpm/fs.is-empty-dir-or-nothing'
|
|
|
|
describe('isEmptyDirOrNothing', () => {
|
|
it('should return true on a non-existent path', () => {
|
|
const nonExistentPath = path.resolve(
|
|
import.meta.dirname,
|
|
'./__fixtures__/not-exists'
|
|
)
|
|
const result = isEmptyDirOrNothing(nonExistentPath)
|
|
expect(result).toBe(true)
|
|
})
|
|
|
|
it('should return true on an empty directory', () => {
|
|
const emptyDirPath = fs.mkdtempSync(`${os.tmpdir()}/empty-dir`)
|
|
const result = isEmptyDirOrNothing(emptyDirPath)
|
|
expect(result).toBe(true)
|
|
})
|
|
|
|
it('should return false on a directory with a file in it', () => {
|
|
const dirWithFilesPath = path.resolve(
|
|
import.meta.dirname,
|
|
'./__fixtures__/dir-with-files'
|
|
)
|
|
const result = isEmptyDirOrNothing(dirWithFilesPath)
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
it('should return false on a directory with a directory', () => {
|
|
const dirWithFilesPath = path.resolve(
|
|
import.meta.dirname,
|
|
'./__fixtures__/dir-with-dirs'
|
|
)
|
|
const result = isEmptyDirOrNothing(dirWithFilesPath)
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
it('should return false on an empty file', () => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'is-empty-dir-or-nothing-'))
|
|
const emptyFilePath = path.join(tempDir, 'empty-file')
|
|
fs.writeFileSync(emptyFilePath, '', 'utf8')
|
|
|
|
try {
|
|
const result = isEmptyDirOrNothing(emptyFilePath)
|
|
|
|
expect(result).toBe(false)
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|