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
238 lines (226 loc) · 7.46 KB
/
Copy pathutils.test.ts
File metadata and controls
238 lines (226 loc) · 7.46 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
import {GitData} from "../src/git-data.js";
import {Utils} from "../src/utils.js";
import {initSpawnSpyReject, initSpawnSpy} from "./mocks/utils.mock.js";
import {isSshDirFound} from "./utils.js";
describe("remoteFileExist", () => {
describe("protocol: git", () => {
const ref = "0.3.1";
const domain = "gitlab.com";
const projectPath = "components/go";
const port = "22";
const cwd = ".";
test("exists", async () => {
const file = "templates/test.yml";
// NOTE: Only mocks git archive command if `~/.ssh` ssh dir is not found because we have no setup ssh keys in ci env
if (!isSshDirFound()) {
const spyGitArchive = {
cmdArgs: `git archive --remote=ssh://git@${domain}:${port}/${projectPath}.git ${ref} ${file}`.split(" "),
returnValue: {output: ""},
};
initSpawnSpy([spyGitArchive]);
}
const fileExist = await Utils.remoteFileExist(cwd, file, ref, domain, projectPath, "git", port);
expect(fileExist).toBe(true);
});
test("don't exists", async () => {
const file = "templates/potato.yml";
// NOTE: Only mocks git archive command if `~/.ssh` ssh dir is not found because we have no setup ssh keys in ci env
if (!isSshDirFound()) {
const spyGitArchive = {
cmdArgs: `git archive --remote=ssh://git@${domain}:${port}/${projectPath}.git ${ref} ${file}`.split(" "),
rejection: {
stderr: `fatal: sent error to the client: git upload-archive: archiver died with error
remote: fatal: pathspec 'templates/potato.yml' did not match any files
remote: git upload-archive: archiver died with error`,
},
};
initSpawnSpyReject([spyGitArchive]);
}
const fileExist = await Utils.remoteFileExist(cwd, file, ref, domain, projectPath, "git", port);
expect(fileExist).toBe(false);
});
});
});
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 = import.meta.jest.spyOn(GitData, "changedFiles");
spy.mockReturnValue(t.input);
expect(Utils.evaluateRuleChanges("origin/master", t.pattern, ".")).toBe(t.hasChanges);
});
});
});
describe("isSubPath where process.cwd() have been mocked to return /home/user/gitlab-ci-local", () => {
beforeAll(() => {
const spy = import.meta.jest.spyOn(process, "cwd");
spy.mockReturnValue("/home/user/gitlab-ci-local");
});
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);
});
});
});