rsnext/test/unit/recursive-delete.test.ts
Wyatt Johnson 0f822373ee
File Reader Improvements (#54645)
This replaces the existing recursive directory reading beheviour with a more efficient implementation. Rather than previously doing actual recursion with the function calls to go deeper into each directory, this has been rewritten to instead use a while loop and a stack. This should improve memory usage for projects with very deep directory structures. 

The updated design that performs directory scanning on all directories found at each layer, allowing more filesystem calls to be sent to the OS instead of waiting like the previous implementation did.

**Currently the new implementation is about 3x faster.**
2023-08-28 18:09:56 +00:00

50 lines
1.6 KiB
TypeScript

/* eslint-env jest */
import fs from 'fs-extra'
import { recursiveDelete } from 'next/dist/lib/recursive-delete'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
import { recursiveCopy } from 'next/dist/lib/recursive-copy'
import { join } from 'path'
const resolveDataDir = join(__dirname, 'isolated', '_resolvedata')
const testResolveDataDir = join(__dirname, 'isolated', 'test_resolvedata')
const testpreservefileDir = join(__dirname, 'isolated', 'preservefiles')
describe('recursiveDelete', () => {
if (process.platform === 'win32') {
it('should skip on windows to avoid symlink issues', () => {})
return
}
it('should work', async () => {
expect.assertions(1)
try {
await recursiveCopy(resolveDataDir, testResolveDataDir)
await fs.symlink('./aa', join(testResolveDataDir, 'symlink'))
await recursiveDelete(testResolveDataDir)
const result = await recursiveReadDir(testResolveDataDir)
expect(result.length).toBe(0)
} finally {
await recursiveDelete(testResolveDataDir)
}
})
it('should exclude', async () => {
expect.assertions(2)
try {
await recursiveCopy(resolveDataDir, testpreservefileDir, {
overwrite: true,
})
// preserve cache dir
await recursiveDelete(testpreservefileDir, /^cache/)
const result = await recursiveReadDir(testpreservefileDir)
expect(result.length).toBe(1)
} finally {
// Ensure test cleanup
await recursiveDelete(testpreservefileDir)
const cleanupResult = await recursiveReadDir(testpreservefileDir)
expect(cleanupResult.length).toBe(0)
}
})
})