forked from pkgxdev/libpkgx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath.test.ts
More file actions
298 lines (240 loc) · 8.21 KB
/
Path.test.ts
File metadata and controls
298 lines (240 loc) · 8.21 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { assert, assertEquals, assertFalse, assertThrows, fail } from "@std/assert"
import { SEPARATOR as SEP } from "jsr:@std/path@1"
import Path from "./Path.ts"
Deno.test("test Path", async test => {
await test.step("creating files", () => {
const start = Deno.build.os == 'windows' ? 'C:' : ''
assertEquals(new Path("/a/b/c").components(), [start, "a", "b", "c"])
assertEquals(new Path("/a/b/c").split(), [new Path("/a/b"), "c"])
const tmp = Path.mktemp({prefix: "pkgx-"})
assert(tmp.isEmpty())
const child = tmp.join("a/b/c")
assertFalse(child.parent().isDirectory())
child.parent().mkdir('p')
assert(child.parent().isDirectory())
assertThrows(() => child.readlink()) // not found
assertFalse(child.isReadableFile())
child.touch()
assert(child.isReadableFile())
assert(child.string.startsWith(tmp.string))
assertFalse(tmp.isEmpty())
if (Deno.build.os != 'windows') {
assertEquals(child.readlink(), child) // not a link
}
const rs = Deno.build.os === "windows" ? "C:\\" : '/'
assertEquals(new Path("/").string, rs)
})
await test.step("write and read", async () => {
const tmp = Path.mktemp({prefix: "pkgx-"})
const data = tmp.join("test.dat")
data.write({text: "hello\nworld"})
const lines = await asyncIterToArray(data.readLines())
assertEquals(lines, ["hello", "world"])
// will throw with no force flag
assertThrows(() => data.write({ json: { hello: "world" } }))
data.write({ json: { hello: "world" }, force: true })
assertEquals(await data.readJSON(), { hello: "world" })
})
await test.step("test walk", async () => {
const tmp = Path.mktemp({prefix: "pkgx-"})
const a = tmp.join("a").mkdir()
a.join("a1").touch()
a.join("a2").touch()
const b = tmp.join("b").mkdir()
b.join("b1").touch()
b.join("b2").touch()
const c = tmp.join("c").mkdir()
c.join("c1").touch()
c.join("c2").touch()
assert(c.join("c2").isFile())
assert(c.isDirectory())
const walked = (await asyncIterToArray(tmp.walk()))
.map(([path, entry]) => {
return {name: path.basename(), isDir: entry.isDirectory}
})
.sort((a, b) => a.name.localeCompare(b.name))
assertEquals(walked, [
{ name: "a", isDir: true},
{ name: "a1", isDir: false},
{ name: "a2", isDir: false},
{ name: "b", isDir: true},
{ name: "b1", isDir: false},
{ name: "b2", isDir: false},
{ name: "c", isDir: true},
{ name: "c1", isDir: false},
{ name: "c2", isDir: false},
])
})
await test.step({
name: "test symlink created",
ignore: Deno.build.os == "windows",
fn() {
const tmp = Path.mktemp({prefix: "pkgx-"}).join("foo").mkdir()
const a = tmp.join("a").touch()
const b = tmp.join("b")
b.ln('s', { target: a })
assertEquals(b.readlink(), a)
assert(b.isSymlink())
}
})
})
Deno.test("Path.cwd", () => {
const cwd = Path.cwd()
assertEquals(cwd.string, Deno.cwd())
})
Deno.test("normalization", () => {
const start = Deno.build.os == 'windows' ? 'C:\\' : SEP
assertEquals(new Path("/a/b/").string, `${start}a${SEP}b`)
assertEquals(new Path("/a/b////").string, `${start}a${SEP}b`)
assertEquals(new Path("/a/b").string, `${start}a${SEP}b`)
assertEquals(new Path("/a////b").string, `${start}a${SEP}b`)
})
Deno.test("new Path(Path)", () => {
const p1 = new Path("/home/user/file.txt")
const p2 = new Path(p1)
assertEquals(p1, p2)
})
Deno.test("Path.join()", () => {
const path = new Path("/foo")
assert(path.eq(path.join()))
})
Deno.test({
name: "Path.isExecutableFile()",
ignore: Deno.build.os == "windows",
fn() {
const tmp = Path.mktemp({prefix: "pkgx-"}).mkdir()
const executable = tmp.join("executable").touch()
executable.chmod(0o755)
const notExecutable = tmp.join("not-executable").touch()
assert(executable.isExecutableFile())
assertFalse(notExecutable.isExecutableFile())
}
})
Deno.test("Path.extname()", () => {
const path = new Path("/home/user/file.txt")
assertEquals(path.extname(), ".txt")
})
Deno.test("Path.mv()", () => {
const tmp = Path.mktemp({prefix: "pkgx-"})
const a = tmp.join("a").touch()
const b = tmp.join("b")
a.mv({ to: b })
assertFalse(a.exists())
assert(b.exists())
const c = tmp.join("c").mkdir()
b.mv({ into: c })
assertFalse(b.exists())
assert(c.join("b").exists())
assertThrows(() => c.mv({ to: c }))
// for coverage
assert(b.neq(c))
})
Deno.test("Path.cp()", () => {
const tmp = Path.mktemp({prefix: "pkgx-"}).mkdir()
const a = tmp.join("a").touch()
const b = tmp.join("b").mkdir()
a.cp({ into: b })
assert(b.join("a").isReadableFile())
assert(a.isReadableFile())
})
Deno.test("Path.relative()", () => {
const a = new Path("/home/user/file.txt")
const b = new Path("/home/user/dir")
assertEquals(a.relative({ to: b }), `..${SEP}file.txt`)
assertEquals(b.relative({ to: a }), `..${SEP}dir`)
})
Deno.test({
name: "Path.realpath()",
ignore: Deno.build.os == "windows",
fn() {
const tmp = Path.mktemp({prefix: "pkgx-"}).mkdir()
const a = tmp.join("a").touch()
const b = tmp.join("b").ln('s', { target: a })
assertEquals(b.realpath(), a.realpath())
}
})
Deno.test("Path.prettyLocalString()", () => {
const path = Path.home().join(".config/pkgx/config.toml")
assertEquals(path.prettyLocalString(), `~${SEP}.config${SEP}pkgx${SEP}config.toml`)
const root = Deno.build.os == 'windows' ? 'C:\\' : '/'
assertEquals(new Path("/a/b").prettyLocalString(), `${root}a${SEP}b`)
})
Deno.test("Path.readYAMLAll()", async () => {
const path = Path.cwd().join("./fixtures/pathtests/readYAMLAll.yaml");
try {
const yamlData = await path.readYAMLAll(); // ✅ Use await
assertEquals(Array.isArray(yamlData), true, "Expected yamlData to be an array");
if (!Array.isArray(yamlData)) {
fail("Expected an array");
return;
}
assertEquals(yamlData.length, 2, "Expected exactly 2 YAML documents");
assertEquals(yamlData, [{ abc: "xyz" }, { ijk: "lmn" }], "YAML content mismatch");
} catch (err) {
console.error("Error reading YAML:", err);
fail("Error reading YAML");
}
});
Deno.test("Path.readYAMLAllErr()", async () => {
const path = Path.cwd().join("./fixtures/pathtests/invalid.yaml");
try {
await path.readYAMLAll();
fail("invalid file should not reach here")
} catch (err) {
if (err instanceof Error) {
assertEquals(err.name, "NotFound")
} else{
throw err;
}
}
});
Deno.test("Path.chuzzle()", () => {
const path = Path.mktemp().join("file.txt").touch()
assertEquals(path.chuzzle(), path)
const missingPath = path.parent().join("ghost.void")
assertEquals(missingPath.chuzzle(), undefined)
})
Deno.test("Path.ls()", async () => {
const tmp = Path.mktemp({prefix: "pkgx-"}).mkdir()
tmp.join("a").touch()
tmp.join("b").touch()
tmp.join("c").mkdir()
const entries = (await asyncIterToArray(tmp.ls())).map(([,{name}]) => name)
assertEquals(entries.sort(), ["a", "b", "c"])
})
async function asyncIterToArray<T> (iter: AsyncIterable<T>){
const result = [];
for await(const i of iter) {
result.push(i);
}
return result;
}
Deno.test("ctor throws", () => {
assertThrows(() => new Path(""))
assertThrows(() => new Path(" "))
assertThrows(() => new Path(" \n "))
assertThrows(() => new Path(" / "))
})
Deno.test({
name: "dirname",
ignore: Deno.build.os != "windows",
fn() {
const p = new Path("Y:\\")
assertEquals(p.string, "Y:\\")
assertEquals(p.parent().string, "Y:\\")
assertEquals(p.parent().parent().parent().string, "Y:\\")
const q = new Path("\\\\bar\\foo\\baz")
assertEquals(q.string, "\\\\bar\\foo\\baz")
assertEquals(q.parent().string, "\\\\bar\\foo")
assertEquals(q.parent().parent().parent().string, "\\\\bar\\foo") // the first path after the hostname is actually a root
}
})
Deno.test("join roots", () => {
if (Deno.build.os == "windows") {
assertEquals(new Path("C:\\foo").join("D:\\bar").string, "D:\\bar")
assertEquals(new Path("C:").join("D:\\bar\baz").string, "D:\\bar\baz")
assertEquals(new Path("c:\\foo\bar").join("\\\\bar\\baz").string, "\\\\bar\\baz")
} else {
assertEquals(new Path("/foo").join("/bar").string, "/bar")
}
})