-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkdir.spec.ts
More file actions
118 lines (96 loc) · 3.69 KB
/
mkdir.spec.ts
File metadata and controls
118 lines (96 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import test from 'ava'
import { mkdirSync, mkdir, rmdirSync } from '../index.js'
import * as nodeFs from 'node:fs'
import { existsSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
function tmpPath(name: string): string {
return join(tmpdir(), `hyper-fs-test-mkdir-${Date.now()}-${name}`)
}
test('mkdirSync: should create a directory', (t) => {
const dir = tmpPath('basic')
mkdirSync(dir)
t.true(existsSync(dir))
rmdirSync(dir)
})
test('mkdirSync: should throw on existing directory without recursive', (t) => {
const dir = tmpPath('existing')
mkdirSync(dir)
t.throws(() => mkdirSync(dir), { message: /EEXIST/ })
rmdirSync(dir)
})
test('mkdirSync: recursive should create nested dirs', (t) => {
const dir = tmpPath('recursive')
const nested = join(dir, 'a', 'b', 'c')
mkdirSync(nested, { recursive: true })
t.true(existsSync(nested))
// cleanup
rmdirSync(join(dir, 'a', 'b', 'c'))
rmdirSync(join(dir, 'a', 'b'))
rmdirSync(join(dir, 'a'))
rmdirSync(dir)
})
test('mkdirSync: recursive should not throw if dir already exists', (t) => {
const dir = tmpPath('recursive-exists')
mkdirSync(dir)
t.notThrows(() => mkdirSync(dir, { recursive: true }))
rmdirSync(dir)
})
test('mkdirSync: recursive should throw EEXIST when target is a file', (t) => {
const dir = tmpPath('recursive-file-exists')
const file = join(dir, 'file.txt')
mkdirSync(dir)
nodeFs.writeFileSync(file, 'x')
const err = t.throws(() => mkdirSync(file, { recursive: true }), { message: /EEXIST/ })
t.true((err?.message ?? '').includes(file))
nodeFs.rmSync(dir, { recursive: true, force: true })
})
test('mkdirSync: recursive should throw ENOTDIR when ancestor is a file', (t) => {
const dir = tmpPath('recursive-not-dir')
const file = join(dir, 'file.txt')
const nested = join(file, 'child')
mkdirSync(dir)
nodeFs.writeFileSync(file, 'x')
const err = t.throws(() => mkdirSync(nested, { recursive: true }), { message: /ENOTDIR/ })
t.true((err?.message ?? '').includes(nested))
nodeFs.rmSync(dir, { recursive: true, force: true })
})
test('mkdir: async should create a directory', async (t) => {
const dir = tmpPath('async')
await mkdir(dir)
t.true(existsSync(dir))
rmdirSync(dir)
})
test('mkdir: async recursive', async (t) => {
const dir = tmpPath('async-recursive')
const nested = join(dir, 'x', 'y')
await mkdir(nested, { recursive: true })
t.true(existsSync(nested))
rmdirSync(join(dir, 'x', 'y'))
rmdirSync(join(dir, 'x'))
rmdirSync(dir)
})
// ===== dual-run comparison =====
test('dual-run: mkdirSync recursive should create same structure as node:fs', (t) => {
const nodeDir = tmpPath('node-recursive')
const hyperDir = tmpPath('hyper-recursive')
nodeFs.mkdirSync(join(nodeDir, 'a', 'b'), { recursive: true })
mkdirSync(join(hyperDir, 'a', 'b'), { recursive: true })
t.is(existsSync(join(nodeDir, 'a', 'b')), existsSync(join(hyperDir, 'a', 'b')))
t.is(existsSync(join(nodeDir, 'a')), existsSync(join(hyperDir, 'a')))
nodeFs.rmSync(nodeDir, { recursive: true })
nodeFs.rmSync(hyperDir, { recursive: true })
})
test('dual-run: mkdirSync should return first created path like node:fs', (t) => {
const nodeDir = tmpPath('node-return')
const hyperDir = tmpPath('hyper-return')
const nodeResult = nodeFs.mkdirSync(join(nodeDir, 'a', 'b'), { recursive: true })
const hyperResult = mkdirSync(join(hyperDir, 'a', 'b'), { recursive: true })
t.is(typeof hyperResult, typeof nodeResult)
if (nodeResult !== undefined && hyperResult !== undefined) {
t.true(nodeResult.endsWith('node-return'))
t.true(hyperResult.endsWith('hyper-return'))
}
nodeFs.rmSync(nodeDir, { recursive: true })
nodeFs.rmSync(hyperDir, { recursive: true })
})