|
| 1 | +import test from 'ava' |
| 2 | +import { rmSync, rm } from '../index.js' |
| 3 | +import { mkdirSync, writeFileSync, existsSync } from 'node:fs' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { tmpdir } from 'node:os' |
| 6 | + |
| 7 | +// Helper function to create a temporary directory |
| 8 | +function createTempDir(): string { |
| 9 | + const tempDir = join(tmpdir(), `hyper-fs-test-${Date.now()}-${Math.random().toString(36).substring(7)}`) |
| 10 | + mkdirSync(tempDir, { recursive: true }) |
| 11 | + return tempDir |
| 12 | +} |
| 13 | + |
| 14 | +test('sync: should remove a file', (t) => { |
| 15 | + const tempDir = createTempDir() |
| 16 | + const testFile = join(tempDir, 'test.txt') |
| 17 | + writeFileSync(testFile, 'test content') |
| 18 | + |
| 19 | + t.true(existsSync(testFile), 'File should exist before removal') |
| 20 | + rmSync(testFile) |
| 21 | + t.false(existsSync(testFile), 'File should not exist after removal') |
| 22 | +}) |
| 23 | + |
| 24 | +test('async: should remove a file', async (t) => { |
| 25 | + const tempDir = createTempDir() |
| 26 | + const testFile = join(tempDir, 'test.txt') |
| 27 | + writeFileSync(testFile, 'test content') |
| 28 | + |
| 29 | + t.true(existsSync(testFile), 'File should exist before removal') |
| 30 | + await rm(testFile) |
| 31 | + t.false(existsSync(testFile), 'File should not exist after removal') |
| 32 | +}) |
| 33 | + |
| 34 | +test('sync: should remove an empty directory', (t) => { |
| 35 | + const tempDir = createTempDir() |
| 36 | + const testDir = join(tempDir, 'empty-dir') |
| 37 | + mkdirSync(testDir) |
| 38 | + |
| 39 | + t.true(existsSync(testDir), 'Directory should exist before removal') |
| 40 | + rmSync(testDir) |
| 41 | + t.false(existsSync(testDir), 'Directory should not exist after removal') |
| 42 | +}) |
| 43 | + |
| 44 | +test('async: should remove an empty directory', async (t) => { |
| 45 | + const tempDir = createTempDir() |
| 46 | + const testDir = join(tempDir, 'empty-dir') |
| 47 | + mkdirSync(testDir) |
| 48 | + |
| 49 | + t.true(existsSync(testDir), 'Directory should exist before removal') |
| 50 | + await rm(testDir) |
| 51 | + t.false(existsSync(testDir), 'Directory should not exist after removal') |
| 52 | +}) |
| 53 | + |
| 54 | +test('sync: should remove a directory recursively when recursive=true', (t) => { |
| 55 | + const tempDir = createTempDir() |
| 56 | + const testDir = join(tempDir, 'nested-dir') |
| 57 | + const nestedDir = join(testDir, 'nested') |
| 58 | + const testFile = join(nestedDir, 'file.txt') |
| 59 | + |
| 60 | + mkdirSync(nestedDir, { recursive: true }) |
| 61 | + writeFileSync(testFile, 'content') |
| 62 | + |
| 63 | + t.true(existsSync(testDir), 'Directory should exist before removal') |
| 64 | + t.true(existsSync(testFile), 'Nested file should exist before removal') |
| 65 | + |
| 66 | + rmSync(testDir, { recursive: true }) |
| 67 | + |
| 68 | + t.false(existsSync(testDir), 'Directory should not exist after removal') |
| 69 | + t.false(existsSync(testFile), 'Nested file should not exist after removal') |
| 70 | +}) |
| 71 | + |
| 72 | +test('async: should remove a directory recursively when recursive=true', async (t) => { |
| 73 | + const tempDir = createTempDir() |
| 74 | + const testDir = join(tempDir, 'nested-dir') |
| 75 | + const nestedDir = join(testDir, 'nested') |
| 76 | + const testFile = join(nestedDir, 'file.txt') |
| 77 | + |
| 78 | + mkdirSync(nestedDir, { recursive: true }) |
| 79 | + writeFileSync(testFile, 'content') |
| 80 | + |
| 81 | + t.true(existsSync(testDir), 'Directory should exist before removal') |
| 82 | + t.true(existsSync(testFile), 'Nested file should exist before removal') |
| 83 | + |
| 84 | + await rm(testDir, { recursive: true }) |
| 85 | + |
| 86 | + t.false(existsSync(testDir), 'Directory should not exist after removal') |
| 87 | + t.false(existsSync(testFile), 'Nested file should not exist after removal') |
| 88 | +}) |
| 89 | + |
| 90 | +test('sync: should throw error when removing non-empty directory without recursive', (t) => { |
| 91 | + const tempDir = createTempDir() |
| 92 | + const testDir = join(tempDir, 'non-empty-dir') |
| 93 | + const testFile = join(testDir, 'file.txt') |
| 94 | + |
| 95 | + mkdirSync(testDir) |
| 96 | + writeFileSync(testFile, 'content') |
| 97 | + |
| 98 | + t.true(existsSync(testDir), 'Directory should exist') |
| 99 | + t.throws(() => rmSync(testDir), { message: /ENOTEMPTY|EEXIST/ }) |
| 100 | +}) |
| 101 | + |
| 102 | +test('async: should throw error when removing non-empty directory without recursive', async (t) => { |
| 103 | + const tempDir = createTempDir() |
| 104 | + const testDir = join(tempDir, 'non-empty-dir') |
| 105 | + const testFile = join(testDir, 'file.txt') |
| 106 | + |
| 107 | + mkdirSync(testDir) |
| 108 | + writeFileSync(testFile, 'content') |
| 109 | + |
| 110 | + t.true(existsSync(testDir), 'Directory should exist') |
| 111 | + await t.throwsAsync(async () => await rm(testDir), { message: /ENOTEMPTY|EEXIST/ }) |
| 112 | +}) |
| 113 | + |
| 114 | +test('sync: should throw error when file does not exist and force=false', (t) => { |
| 115 | + const tempDir = createTempDir() |
| 116 | + const nonExistentFile = join(tempDir, 'non-existent.txt') |
| 117 | + |
| 118 | + t.false(existsSync(nonExistentFile), 'File should not exist') |
| 119 | + t.throws(() => rmSync(nonExistentFile), { message: /ENOENT/ }) |
| 120 | +}) |
| 121 | + |
| 122 | +test('async: should throw error when file does not exist and force=false', async (t) => { |
| 123 | + const tempDir = createTempDir() |
| 124 | + const nonExistentFile = join(tempDir, 'non-existent.txt') |
| 125 | + |
| 126 | + t.false(existsSync(nonExistentFile), 'File should not exist') |
| 127 | + await t.throwsAsync(async () => await rm(nonExistentFile), { message: /ENOENT/ }) |
| 128 | +}) |
| 129 | + |
| 130 | +test('sync: should not throw error when file does not exist and force=true', (t) => { |
| 131 | + const tempDir = createTempDir() |
| 132 | + const nonExistentFile = join(tempDir, 'non-existent.txt') |
| 133 | + |
| 134 | + t.false(existsSync(nonExistentFile), 'File should not exist') |
| 135 | + // Should not throw |
| 136 | + t.notThrows(() => rmSync(nonExistentFile, { force: true })) |
| 137 | +}) |
| 138 | + |
| 139 | +test('async: should not throw error when file does not exist and force=true', async (t) => { |
| 140 | + const tempDir = createTempDir() |
| 141 | + const nonExistentFile = join(tempDir, 'non-existent.txt') |
| 142 | + |
| 143 | + t.false(existsSync(nonExistentFile), 'File should not exist') |
| 144 | + // Should not throw |
| 145 | + await t.notThrowsAsync(async () => await rm(nonExistentFile, { force: true })) |
| 146 | +}) |
| 147 | + |
| 148 | +test('sync: should remove file when force=true (even if file exists)', (t) => { |
| 149 | + const tempDir = createTempDir() |
| 150 | + const testFile = join(tempDir, 'test.txt') |
| 151 | + writeFileSync(testFile, 'content') |
| 152 | + |
| 153 | + t.true(existsSync(testFile), 'File should exist before removal') |
| 154 | + rmSync(testFile, { force: true }) |
| 155 | + t.false(existsSync(testFile), 'File should not exist after removal') |
| 156 | +}) |
| 157 | + |
| 158 | +test('async: should remove file when force=true (even if file exists)', async (t) => { |
| 159 | + const tempDir = createTempDir() |
| 160 | + const testFile = join(tempDir, 'test.txt') |
| 161 | + writeFileSync(testFile, 'content') |
| 162 | + |
| 163 | + t.true(existsSync(testFile), 'File should exist before removal') |
| 164 | + await rm(testFile, { force: true }) |
| 165 | + t.false(existsSync(testFile), 'File should not exist after removal') |
| 166 | +}) |
| 167 | + |
| 168 | +test('sync: should work with recursive=false explicitly', (t) => { |
| 169 | + const tempDir = createTempDir() |
| 170 | + const testDir = join(tempDir, 'empty-dir') |
| 171 | + mkdirSync(testDir) |
| 172 | + |
| 173 | + t.true(existsSync(testDir), 'Directory should exist before removal') |
| 174 | + rmSync(testDir, { recursive: false }) |
| 175 | + t.false(existsSync(testDir), 'Directory should not exist after removal') |
| 176 | +}) |
| 177 | + |
| 178 | +test('async: should work with recursive=false explicitly', async (t) => { |
| 179 | + const tempDir = createTempDir() |
| 180 | + const testDir = join(tempDir, 'empty-dir') |
| 181 | + mkdirSync(testDir) |
| 182 | + |
| 183 | + t.true(existsSync(testDir), 'Directory should exist before removal') |
| 184 | + await rm(testDir, { recursive: false }) |
| 185 | + t.false(existsSync(testDir), 'Directory should not exist after removal') |
| 186 | +}) |
| 187 | + |
| 188 | +test('sync: should remove deep nested directory with concurrency', (t) => { |
| 189 | + const tempDir = createTempDir() |
| 190 | + const testDir = join(tempDir, 'nested-dir-concurrency') |
| 191 | + // Create a structure: nested-dir/subdir1/file, nested-dir/subdir2/file, ... |
| 192 | + mkdirSync(testDir) |
| 193 | + for (let i = 0; i < 10; i++) { |
| 194 | + const subDir = join(testDir, `sub-${i}`) |
| 195 | + mkdirSync(subDir) |
| 196 | + writeFileSync(join(subDir, 'file.txt'), 'content') |
| 197 | + } |
| 198 | + |
| 199 | + t.true(existsSync(testDir)) |
| 200 | + rmSync(testDir, { recursive: true, concurrency: 4 }) |
| 201 | + t.false(existsSync(testDir)) |
| 202 | +}) |
| 203 | + |
| 204 | +test('async: should remove deep nested directory with concurrency', async (t) => { |
| 205 | + const tempDir = createTempDir() |
| 206 | + const testDir = join(tempDir, 'nested-dir-async-concurrency') |
| 207 | + mkdirSync(testDir) |
| 208 | + for (let i = 0; i < 10; i++) { |
| 209 | + const subDir = join(testDir, `sub-${i}`) |
| 210 | + mkdirSync(subDir) |
| 211 | + writeFileSync(join(subDir, 'file.txt'), 'content') |
| 212 | + } |
| 213 | + |
| 214 | + t.true(existsSync(testDir)) |
| 215 | + await rm(testDir, { recursive: true, concurrency: 4 }) |
| 216 | + t.false(existsSync(testDir)) |
| 217 | +}) |
0 commit comments