-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathglob-assets.test.js
213 lines (171 loc) · 6.73 KB
/
glob-assets.test.js
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
import path from "path";
import test from "ava";
import { copy, ensureDir } from "fs-extra";
import { isPlainObject, sortBy } from "lodash-es";
import tempy from "tempy";
import globAssets from "../lib/glob-assets.js";
const sortAssets = (assets) => sortBy(assets, (asset) => (isPlainObject(asset) ? asset.path : asset));
const fixtures = "test/fixtures/files";
test("Retrieve file from single path", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, ["upload.txt"]);
t.deepEqual(globbedAssets, ["upload.txt"]);
});
test("Retrieve multiple files from path", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, ["upload.txt", "upload_other.txt"]);
t.deepEqual(sortAssets(globbedAssets), sortAssets(["upload_other.txt", "upload.txt"]));
});
test("Include missing files as defined, using Object definition", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, ["upload.txt", { path: "miss*.txt", label: "Missing" }]);
t.deepEqual(sortAssets(globbedAssets), sortAssets(["upload.txt", { path: "miss*.txt", label: "Missing" }]));
});
test("Retrieve multiple files from Object", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [
{ path: "upload.txt", name: "upload_name", label: "Upload label" },
"upload_other.txt",
]);
t.deepEqual(
sortAssets(globbedAssets),
sortAssets([{ path: "upload.txt", name: "upload_name", label: "Upload label" }, "upload_other.txt"])
);
});
test("Retrieve multiple files without duplicates", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [
"upload_other.txt",
"upload.txt",
"upload_other.txt",
"upload.txt",
"upload.txt",
"upload_other.txt",
]);
t.deepEqual(sortAssets(globbedAssets), sortAssets(["upload_other.txt", "upload.txt"]));
});
test("Favor Object over String values when removing duplicates", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [
"upload_other.txt",
"upload.txt",
{ path: "upload.txt", name: "upload_name" },
"upload.txt",
{ path: "upload_other.txt", name: "upload_other_name", filepath: "/path/to/other" },
"upload.txt",
"upload_other.txt",
]);
t.deepEqual(
sortAssets(globbedAssets),
sortAssets([
{ path: "upload.txt", name: "upload_name" },
{ path: "upload_other.txt", name: "upload_other_name", filepath: "/path/to/other" },
])
);
});
test("Retrieve file from single glob", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, ["upload.*"]);
t.deepEqual(globbedAssets, ["upload.txt"]);
});
test("Retrieve multiple files from single glob", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, ["*.txt"]);
t.deepEqual(sortAssets(globbedAssets), sortAssets(["upload_other.txt", "upload.txt"]));
});
test("Accept glob array with one value", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [["*load.txt"], ["*_other.txt"]]);
t.deepEqual(sortAssets(globbedAssets), sortAssets(["upload_other.txt", "upload.txt"]));
});
test("Include globs that resolve to no files as defined", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [["upload.txt", "!upload.txt"]]);
t.deepEqual(sortAssets(globbedAssets), sortAssets(["!upload.txt", "upload.txt"]));
});
test("Accept glob array with one value for missing files", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [["*missing.txt"], ["*_other.txt"]]);
t.deepEqual(sortAssets(globbedAssets), sortAssets(["upload_other.txt", "*missing.txt"]));
});
test("Replace name by filename for Object that match multiple files", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [{ path: "*.txt", label: "Upload label" }]);
t.deepEqual(
sortAssets(globbedAssets),
sortAssets([
{ path: "upload.txt", label: "upload.txt" },
{ path: "upload_other.txt", label: "upload_other.txt" },
])
);
});
test("Ignore filepath for Object that match multiple files", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [{ path: "*.txt", filepath: "/path/to/file" }]);
t.deepEqual(
sortAssets(globbedAssets),
sortAssets([
{ path: "upload.txt", label: "upload.txt" },
{ path: "upload_other.txt", label: "upload_other.txt" },
])
);
});
test("Include dotfiles", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [".dot*"]);
t.deepEqual(globbedAssets, [".dotfile"]);
});
test("Ingnore single negated glob", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, ["!*.txt"]);
t.deepEqual(globbedAssets, []);
});
test("Ingnore single negated glob in Object", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [{ path: "!*.txt" }]);
t.deepEqual(globbedAssets, []);
});
test("Accept negated globs", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, [["*.txt", "!**/*_other.txt"]]);
t.deepEqual(globbedAssets, ["upload.txt"]);
});
test("Expand directories", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, path.resolve(cwd, "dir"));
const globbedAssets = await globAssets({ cwd }, [["dir"]]);
t.deepEqual(
sortAssets(globbedAssets),
sortAssets(["dir", "dir/upload_other.txt", "dir/upload.txt", "dir/.dotfile", "dir/file.css"])
);
});
test("Include empty directory as defined", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
await ensureDir(path.resolve(cwd, "empty"));
const globbedAssets = await globAssets({ cwd }, [["empty"]]);
t.deepEqual(globbedAssets, ["empty"]);
});
test("Deduplicate resulting files path", async (t) => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({ cwd }, ["./upload.txt", path.resolve(cwd, "upload.txt"), "upload.txt"]);
t.is(globbedAssets.length, 1);
});