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
|
const { mkdir, open } = Deno;
type FileInfo = Deno.FileInfo;
import { test } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
import { glob } from "./glob.ts";
import { join } from "./path.ts";
import { testWalk } from "./walk_test.ts";
import { walk, walkSync, WalkOptions } from "./walk.ts";
async function touch(path: string): Promise<void> {
await open(path, "w");
}
async function walkArray(
dirname: string = ".",
options: WalkOptions = {}
): Promise<Array<string>> {
const arr: string[] = [];
for await (const f of walk(dirname, { ...options })) {
arr.push(f.path.replace(/\\/g, "/"));
}
arr.sort();
const arr_sync = Array.from(walkSync(dirname, options), (f: FileInfo) =>
f.path.replace(/\\/g, "/")
).sort();
assertEquals(arr, arr_sync);
return arr;
}
test({
name: "glob: glob to regex",
fn() {
assertEquals(glob("unicorn.*") instanceof RegExp, true);
assertEquals(glob("unicorn.*").test("poney.ts"), false);
assertEquals(glob("unicorn.*").test("unicorn.py"), true);
assertEquals(glob("*.ts").test("poney.ts"), true);
assertEquals(glob("*.ts").test("unicorn.js"), false);
assertEquals(
glob(join("unicorn", "**", "cathedral.ts")).test(
join("unicorn", "in", "the", "cathedral.ts")
),
true
);
assertEquals(
glob(join("unicorn", "**", "cathedral.ts")).test(
join("unicorn", "in", "the", "kitchen.ts")
),
false
);
assertEquals(
glob(join("unicorn", "**", "bathroom.*")).test(
join("unicorn", "sleeping", "in", "bathroom.py")
),
true
);
assertEquals(
glob(join("unicorn", "!(sleeping)", "bathroom.ts"), {
extended: true
}).test(join("unicorn", "flying", "bathroom.ts")),
true
);
assertEquals(
glob(join("unicorn", "(!sleeping)", "bathroom.ts"), {
extended: true
}).test(join("unicorn", "sleeping", "bathroom.ts")),
false
);
}
});
testWalk(
async (d: string) => {
await mkdir(d + "/a");
await touch(d + "/a/x.ts");
},
async function globInWalk() {
const arr = await walkArray(".", { match: [glob("*.ts")] });
assertEquals(arr.length, 1);
assertEquals(arr[0], "./a/x.ts");
}
);
testWalk(
async (d: string) => {
await mkdir(d + "/a");
await mkdir(d + "/b");
await touch(d + "/a/x.ts");
await touch(d + "/b/z.ts");
await touch(d + "/b/z.js");
},
async function globInWalkWildcardFiles() {
const arr = await walkArray(".", { match: [glob("*.ts")] });
assertEquals(arr.length, 2);
assertEquals(arr[0], "./a/x.ts");
assertEquals(arr[1], "./b/z.ts");
}
);
testWalk(
async (d: string) => {
await mkdir(d + "/a");
await mkdir(d + "/a/yo");
await touch(d + "/a/yo/x.ts");
},
async function globInWalkFolderWildcard() {
const arr = await walkArray(".", {
match: [
glob(join("a", "**", "*.ts"), {
flags: "g",
globstar: true
})
]
});
assertEquals(arr.length, 1);
assertEquals(arr[0], "./a/yo/x.ts");
}
);
testWalk(
async (d: string) => {
await mkdir(d + "/a");
await mkdir(d + "/a/unicorn");
await mkdir(d + "/a/deno");
await mkdir(d + "/a/raptor");
await touch(d + "/a/raptor/x.ts");
await touch(d + "/a/deno/x.ts");
await touch(d + "/a/unicorn/x.ts");
},
async function globInWalkFolderExtended() {
const arr = await walkArray(".", {
match: [
glob(join("a", "+(raptor|deno)", "*.ts"), {
flags: "g",
extended: true
})
]
});
assertEquals(arr.length, 2);
assertEquals(arr[0], "./a/deno/x.ts");
assertEquals(arr[1], "./a/raptor/x.ts");
}
);
testWalk(
async (d: string) => {
await touch(d + "/x.ts");
await touch(d + "/x.js");
await touch(d + "/b.js");
},
async function globInWalkWildcardExtension() {
const arr = await walkArray(".", {
match: [glob("x.*", { flags: "g", globstar: true })]
});
console.log(arr);
assertEquals(arr.length, 2);
assertEquals(arr[0], "./x.js");
assertEquals(arr[1], "./x.ts");
}
);
|