forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglob.test.ts
More file actions
164 lines (121 loc) · 5.89 KB
/
glob.test.ts
File metadata and controls
164 lines (121 loc) · 5.89 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
import { describe, test, expect } from "bun:test"
import path from "path"
import fs from "fs/promises"
import { Glob } from "../../src/util/glob"
import { tmpdir } from "../fixture/fixture"
describe("Glob", () => {
describe("scan()", () => {
test("finds files matching pattern", async () => {
await using tmp = await tmpdir()
await fs.writeFile(path.join(tmp.path, "a.txt"), "", "utf-8")
await fs.writeFile(path.join(tmp.path, "b.txt"), "", "utf-8")
await fs.writeFile(path.join(tmp.path, "c.md"), "", "utf-8")
const results = await Glob.scan("*.txt", { cwd: tmp.path })
expect(results.sort()).toEqual(["a.txt", "b.txt"])
})
test("returns absolute paths when absolute option is true", async () => {
await using tmp = await tmpdir()
await fs.writeFile(path.join(tmp.path, "file.txt"), "", "utf-8")
const results = await Glob.scan("*.txt", { cwd: tmp.path, absolute: true })
expect(results[0]).toBe(path.join(tmp.path, "file.txt"))
})
test("excludes directories by default", async () => {
await using tmp = await tmpdir()
await fs.mkdir(path.join(tmp.path, "subdir"))
await fs.writeFile(path.join(tmp.path, "file.txt"), "", "utf-8")
const results = await Glob.scan("*", { cwd: tmp.path })
expect(results).toEqual(["file.txt"])
})
test("excludes directories when include is 'file'", async () => {
await using tmp = await tmpdir()
await fs.mkdir(path.join(tmp.path, "subdir"))
await fs.writeFile(path.join(tmp.path, "file.txt"), "", "utf-8")
const results = await Glob.scan("*", { cwd: tmp.path, include: "file" })
expect(results).toEqual(["file.txt"])
})
test("includes directories when include is 'all'", async () => {
await using tmp = await tmpdir()
await fs.mkdir(path.join(tmp.path, "subdir"))
await fs.writeFile(path.join(tmp.path, "file.txt"), "", "utf-8")
const results = await Glob.scan("*", { cwd: tmp.path, include: "all" })
expect(results.sort()).toEqual(["file.txt", "subdir"])
})
test("handles nested patterns", async () => {
await using tmp = await tmpdir()
await fs.mkdir(path.join(tmp.path, "nested"), { recursive: true })
await fs.writeFile(path.join(tmp.path, "nested", "deep.txt"), "", "utf-8")
const results = await Glob.scan("**/*.txt", { cwd: tmp.path })
expect(results).toEqual([path.join("nested", "deep.txt")])
})
test("returns empty array for no matches", async () => {
await using tmp = await tmpdir()
const results = await Glob.scan("*.nonexistent", { cwd: tmp.path })
expect(results).toEqual([])
})
test("does not follow symlinks by default", async () => {
await using tmp = await tmpdir()
await fs.mkdir(path.join(tmp.path, "realdir"))
await fs.writeFile(path.join(tmp.path, "realdir", "file.txt"), "", "utf-8")
await fs.symlink(path.join(tmp.path, "realdir"), path.join(tmp.path, "linkdir"))
const results = await Glob.scan("**/*.txt", { cwd: tmp.path })
expect(results).toEqual([path.join("realdir", "file.txt")])
})
test("follows symlinks when symlink option is true", async () => {
await using tmp = await tmpdir()
await fs.mkdir(path.join(tmp.path, "realdir"))
await fs.writeFile(path.join(tmp.path, "realdir", "file.txt"), "", "utf-8")
await fs.symlink(path.join(tmp.path, "realdir"), path.join(tmp.path, "linkdir"))
const results = await Glob.scan("**/*.txt", { cwd: tmp.path, symlink: true })
expect(results.sort()).toEqual([path.join("linkdir", "file.txt"), path.join("realdir", "file.txt")])
})
test("includes dotfiles when dot option is true", async () => {
await using tmp = await tmpdir()
await fs.writeFile(path.join(tmp.path, ".hidden"), "", "utf-8")
await fs.writeFile(path.join(tmp.path, "visible"), "", "utf-8")
const results = await Glob.scan("*", { cwd: tmp.path, dot: true })
expect(results.sort()).toEqual([".hidden", "visible"])
})
test("excludes dotfiles when dot option is false", async () => {
await using tmp = await tmpdir()
await fs.writeFile(path.join(tmp.path, ".hidden"), "", "utf-8")
await fs.writeFile(path.join(tmp.path, "visible"), "", "utf-8")
const results = await Glob.scan("*", { cwd: tmp.path, dot: false })
expect(results).toEqual(["visible"])
})
})
describe("scanSync()", () => {
test("finds files matching pattern synchronously", async () => {
await using tmp = await tmpdir()
await fs.writeFile(path.join(tmp.path, "a.txt"), "", "utf-8")
await fs.writeFile(path.join(tmp.path, "b.txt"), "", "utf-8")
const results = Glob.scanSync("*.txt", { cwd: tmp.path })
expect(results.sort()).toEqual(["a.txt", "b.txt"])
})
test("respects options", async () => {
await using tmp = await tmpdir()
await fs.mkdir(path.join(tmp.path, "subdir"))
await fs.writeFile(path.join(tmp.path, "file.txt"), "", "utf-8")
const results = Glob.scanSync("*", { cwd: tmp.path, include: "all" })
expect(results.sort()).toEqual(["file.txt", "subdir"])
})
})
describe("match()", () => {
test("matches simple patterns", () => {
expect(Glob.match("*.txt", "file.txt")).toBe(true)
expect(Glob.match("*.txt", "file.js")).toBe(false)
})
test("matches directory patterns", () => {
expect(Glob.match("**/*.js", "src/index.js")).toBe(true)
expect(Glob.match("**/*.js", "src/index.ts")).toBe(false)
})
test("matches dot files", () => {
expect(Glob.match(".*", ".gitignore")).toBe(true)
expect(Glob.match("**/*.md", ".github/README.md")).toBe(true)
})
test("matches brace expansion", () => {
expect(Glob.match("*.{js,ts}", "file.js")).toBe(true)
expect(Glob.match("*.{js,ts}", "file.ts")).toBe(true)
expect(Glob.match("*.{js,ts}", "file.py")).toBe(false)
})
})
})