forked from firecow/gitlab-ci-local
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.test.ts
More file actions
331 lines (311 loc) · 11.3 KB
/
Copy pathutils.test.ts
File metadata and controls
331 lines (311 loc) · 11.3 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import {vi} from "vitest";
import {GitData} from "../src/git-data.js";
import {Utils} from "../src/utils.js";
describe("evaluateRuleChanges", () => {
const tests = [
{
description: "should be case sensitive",
input: ["Foo"],
pattern: ["foo"],
hasChanges: false,
},
{
description: "should support wildcard",
input: ["foo"],
pattern: ["*"],
hasChanges: true,
},
{
description: "should support wildcard glob",
input: ["README.md"],
pattern: ["*.md"],
hasChanges: true,
},
{
description: "should support globstar",
input: ["aaa/bc/foo"],
pattern: ["**/foo"],
hasChanges: true,
},
{
description: "should support brace expansion",
input: ["src/foo.rb"],
pattern: ["src/*.{rb,py,sh}"],
hasChanges: true,
},
{
description: "should not support negation",
input: ["README.md"],
pattern: ["!.*"],
hasChanges: false,
},
{
description: "should not support extended glob",
input: ["a.js"],
pattern: ["+(a|b).js"],
hasChanges: false,
},
{
description: "should treat . as literal (false)",
input: ["fizz"],
pattern: ["...."],
hasChanges: false,
},
{
description: "should treat . as literal (true)",
input: ["...."],
pattern: ["...."],
hasChanges: true,
},
{
description: "should not support posix character class",
input: ["a"],
pattern: ["[[:alpha:]]"],
hasChanges: false,
},
{
description: "wildcard should match filename starting with '.'",
input: [".profile"],
pattern: ["*"],
hasChanges: true,
},
{
description: "should match entire string",
input: ["cat"],
pattern: ["cat"],
hasChanges: true,
},
{
description: "should not match partial string",
input: ["category"],
pattern: ["cat"],
hasChanges: false,
},
{
description: "should support FNM_EXTGLOB",
input: ["cats"],
pattern: ["c{at,ub}s"],
hasChanges: true,
},
{
description: "'?' should match only one character (truthy)",
input: ["cat"],
pattern: ["c?t"],
hasChanges: true,
},
{
description: "'?' should match only one character (falsy)",
input: ["cat"],
pattern: ["c??t"],
hasChanges: false,
},
{
description: "'*' should match 0 or more characters",
input: ["cats"],
pattern: ["c*"],
hasChanges: true,
},
{
description: "should support inclusive bracket expansion",
input: ["cat"],
pattern: ["ca[a-z]"],
hasChanges: true,
},
{
description: "should support exclusive bracket expansion (falsy)",
input: ["cat"],
pattern: ["ca[^t]"],
hasChanges: false,
},
{
description: "should support exclusive bracket expansion (truthy)",
input: ["caa"],
pattern: ["ca[^t]"],
hasChanges: true,
},
];
tests.forEach((t) => {
test.concurrent(`${t.description} \t\t [input: ${t.input} pattern: ${t.pattern} hasChanges: ${t.hasChanges}]`, () => {
const spy = vi.spyOn(GitData, "changedFiles");
spy.mockReturnValue(t.input);
expect(Utils.evaluateRuleChanges("origin/master", t.pattern, ".")).toBe(t.hasChanges);
spy.mockRestore();
});
});
});
describe("isSubPath where process.cwd() have been mocked to return /home/user/gitlab-ci-local", () => {
let cwdSpy: ReturnType<typeof vi.spyOn>;
beforeAll(() => {
cwdSpy = vi.spyOn(process, "cwd");
cwdSpy.mockReturnValue("/home/user/gitlab-ci-local");
});
afterAll(() => {
cwdSpy.mockRestore();
});
const tests: {
input: [string, string, string?];
expected: boolean;
}[] = [
{
input: ["/tmp", "foo"],
expected: false,
},
{
input: ["../bar", "foo"],
expected: false,
},
{
input: ["../gitlab-ci-local", "."],
expected: true,
},
{
input: ["../gitlab-ci-local", "/home/user/gitlab-ci-local"],
expected: true,
},
{
input: ["../gitlab-ci-local", "/gitlab-ci-local"],
expected: false,
},
{
input: ["../////gitlab-ci-local", "."],
expected: true,
},
{
input: ["cache/*/foo", "cache"],
expected: true,
},
{
input: ["cache", "cache/*/foo"],
expected: false,
},
{
input: ["key-files", "/home/user/gitlab-ci-local", "/home/user/gitlab-ci-local"],
expected: true,
},
];
tests.forEach(({input, expected}) => {
test(`isSubpath("${input[0]}", "${input[1]}") => ${expected}`, () => {
expect(Utils.isSubpath(...input)).toBe(expected);
});
});
});
describe("getAllServiceAliases", () => {
const tests = [
{
input: "nginx",
expected: ["nginx"],
},
{
input: "library/nginx",
expected: ["library-nginx", "library__nginx"],
},
{
input: "docker.io/library/nginx",
expected: ["docker.io-library-nginx", "docker.io__library__nginx"],
},
{
input: "registry-1.docker.io/library/nginx",
expected: ["registry-1.docker.io-library-nginx", "registry-1.docker.io__library__nginx"],
},
{
input: "registry-1.docker.io:443/library/nginx",
expected: ["registry-1.docker.io-library-nginx", "registry-1.docker.io__library__nginx"],
},
];
const suffixes = [
"",
":1.29.7",
":1.29.7@sha256:e7257f1ef28ba17cf7c248cb8ccf6f0c6e0228ab9c315c152f9c203cd34cf6d1",
"@sha256:e7257f1ef28ba17cf7c248cb8ccf6f0c6e0228ab9c315c152f9c203cd34cf6d1",
];
tests.forEach(({input, expected}) => {
suffixes.forEach((suffix) => {
const serviceName = `${input}${suffix}`;
test.concurrent(`${serviceName}`, () => {
const service = {
name: serviceName,
entrypoint: null,
command: null,
alias: null,
variables: {},
};
const aliases = Utils.getAllServiceAliases(service);
expect([...aliases]).toEqual(expected);
});
});
});
test.concurrent("should include custom alias when provided", () => {
const service = {
name: "docker.io/library/nginx:1.29.7",
entrypoint: null,
command: null,
alias: "my-nginx",
variables: {},
};
const aliases = Utils.getAllServiceAliases(service);
expect([...aliases]).toEqual(["my-nginx", "docker.io-library-nginx", "docker.io__library__nginx"]);
});
});
describe("getServiceAlias", () => {
const base = {entrypoint: null, command: null, variables: {}};
test.concurrent("returns - variant when no custom alias", () => {
expect(Utils.getServiceAlias({...base, name: "library/nginx", alias: null})).toBe("library-nginx");
});
test.concurrent("returns custom alias when provided", () => {
expect(Utils.getServiceAlias({...base, name: "library/nginx", alias: "my-nginx"})).toBe("my-nginx");
});
});
describe("safeDockerString", () => {
it("should return encoded name unchanged when within limit", () => {
const result = Utils.safeDockerString("short-job-name");
expect(result).toBe("short-job-name");
});
it("should encode non-alphanumeric characters", () => {
const result = Utils.safeDockerString("job/name");
expect(result).toContain("Lw"); // '/' encodes to base64url
});
it("should truncate and hash when encoded name exceeds MAX_FILENAME_LENGTH", () => {
const longName = "my-group/common/python-unit-test: [my-app-controller,My app controller to be used as reference for development teams,python311,controller,common,controller/setup.py,controller/setup_c.py,controller/setup_n.py,controller/tests/**/*,controller/coverage/*,controller/build/**/*,controller/coverage/coverage-unit.xml,75,true]";
const result = Utils.safeDockerString(longName);
expect(result.length).toBeLessThanOrEqual(Utils.MAX_FILENAME_LENGTH);
});
it("should produce deterministic output for the same input", () => {
const longName = "a".repeat(50) + "/" + "b".repeat(200);
const result1 = Utils.safeDockerString(longName);
const result2 = Utils.safeDockerString(longName);
expect(result1).toBe(result2);
});
it("should produce different output for different long inputs", () => {
const name1 = "job: [" + "a".repeat(300) + "]";
const name2 = "job: [" + "b".repeat(300) + "]";
const result1 = Utils.safeDockerString(name1);
const result2 = Utils.safeDockerString(name2);
expect(result1).not.toBe(result2);
});
it("should handle extremely long job names (1000+ chars)", () => {
const extremeName = "group/subgroup/job: [" + "x".repeat(2000) + "]";
const result = Utils.safeDockerString(extremeName);
expect(result.length).toBeLessThanOrEqual(Utils.MAX_FILENAME_LENGTH);
expect(result.length).toBeGreaterThan(16); // has prefix + hash
});
it("should keep volume name within NAME_MAX=255 (worst-case suffix)", () => {
const longName = "my-group/common/python-unit-test: [" + "a/b/c,".repeat(100) + "]";
const safeJobName = Utils.safeDockerString(longName);
const worstCaseVolume = `gcl-${safeJobName}-999999-build`;
expect(worstCaseVolume.length).toBeLessThanOrEqual(255);
});
it("should not hash names that are exactly at the limit", () => {
// Create a name whose encoded form is exactly MAX_FILENAME_LENGTH
const name = "a".repeat(Utils.MAX_FILENAME_LENGTH);
const result = Utils.safeDockerString(name);
expect(result).toBe(name); // all alphanumeric, no encoding, no hash
});
it("should hash names whose encoded form is one char over the limit", () => {
// 'a' stays as 'a', '/' encodes to 'Lw' (2 chars)
// Build a string that encodes to exactly MAX_FILENAME_LENGTH + 1
const name = "a".repeat(Utils.MAX_FILENAME_LENGTH - 1) + "/"; // '/' -> 'Lw' = +2, total = MAX+1
const result = Utils.safeDockerString(name);
expect(result.length).toBeLessThanOrEqual(Utils.MAX_FILENAME_LENGTH);
expect(result).toContain("-"); // has hash separator
});
});