-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathowners.test.js
More file actions
350 lines (299 loc) · 10.7 KB
/
owners.test.js
File metadata and controls
350 lines (299 loc) · 10.7 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
const { describe, it, before, after } = require("node:test");
const assert = require("node:assert/strict");
const fs = require("fs");
const os = require("os");
const path = require("path");
const {
parseOwnerTeams,
ownersMatch,
parseOwnersFile,
findOwners,
getMaintainers,
getOwnershipGroups,
} = require("./owners");
// --- ownersMatch ---
describe("ownersMatch", () => {
it("* matches everything", () => {
assert.ok(ownersMatch("*", "any/file/path.go"));
assert.ok(ownersMatch("*", "README.md"));
assert.ok(ownersMatch("*", ""));
});
it("/dir/ prefix matches files under that directory", () => {
assert.ok(ownersMatch("/cmd/pipelines/", "cmd/pipelines/foo.go"));
assert.ok(ownersMatch("/cmd/pipelines/", "cmd/pipelines/sub/bar.go"));
});
it("/dir/ does NOT match files in other directories", () => {
assert.ok(!ownersMatch("/cmd/pipelines/", "cmd/other/foo.go"));
assert.ok(!ownersMatch("/cmd/pipelines/", "cmd/pipeline/foo.go"));
assert.ok(!ownersMatch("/cmd/pipelines/", "bundle/pipelines/foo.go"));
});
it("exact file match", () => {
assert.ok(ownersMatch("/some/file.go", "some/file.go"));
assert.ok(!ownersMatch("/some/file.go", "some/other.go"));
assert.ok(!ownersMatch("/some/file.go", "some/file.go/extra"));
});
it("leading / is stripped for matching", () => {
assert.ok(ownersMatch("/bundle/", "bundle/config.go"));
assert.ok(ownersMatch("/README.md", "README.md"));
});
});
// --- parseOwnersFile ---
describe("parseOwnersFile", () => {
let tmpDir;
let ownersPath;
before(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "owners-test-"));
ownersPath = path.join(tmpDir, "OWNERS");
});
after(() => {
fs.rmSync(tmpDir, { recursive: true });
});
it("parses rules with owners", () => {
fs.writeFileSync(
ownersPath,
[
"* @alice @bob",
"/cmd/pipelines/ @carol",
].join("\n")
);
const rules = parseOwnersFile(ownersPath);
assert.equal(rules.length, 2);
assert.equal(rules[0].pattern, "*");
assert.deepEqual(rules[0].owners, ["alice", "bob"]);
assert.equal(rules[1].pattern, "/cmd/pipelines/");
assert.deepEqual(rules[1].owners, ["carol"]);
});
it("filters out team refs by default", () => {
fs.writeFileSync(
ownersPath,
"/cmd/apps/ @databricks/eng-apps-devex @alice\n"
);
const rules = parseOwnersFile(ownersPath);
assert.equal(rules.length, 1);
assert.deepEqual(rules[0].owners, ["alice"]);
});
it("includes team refs with includeTeams option", () => {
fs.writeFileSync(
ownersPath,
"/cmd/apps/ @databricks/eng-apps-devex @alice\n"
);
const rules = parseOwnersFile(ownersPath, { includeTeams: true });
assert.equal(rules.length, 1);
assert.deepEqual(rules[0].owners, ["databricks/eng-apps-devex", "alice"]);
});
it("skips comments and blank lines", () => {
fs.writeFileSync(
ownersPath,
[
"# This is a comment",
"",
" # indented comment",
"* @alice",
"",
"/cmd/ @bob",
].join("\n")
);
const rules = parseOwnersFile(ownersPath);
assert.equal(rules.length, 2);
});
it("strips @ prefix from owners", () => {
fs.writeFileSync(ownersPath, "* @alice @bob\n");
const rules = parseOwnersFile(ownersPath);
assert.deepEqual(rules[0].owners, ["alice", "bob"]);
});
it("skips lines with only a pattern and no owners", () => {
fs.writeFileSync(ownersPath, "/lonely/\n* @alice\n");
const rules = parseOwnersFile(ownersPath);
assert.equal(rules.length, 1);
assert.equal(rules[0].pattern, "*");
});
});
// --- parseOwnerTeams ---
describe("parseOwnerTeams", () => {
let tmpDir;
before(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "ownerteams-test-"));
});
after(() => {
fs.rmSync(tmpDir, { recursive: true });
});
it("parses team definitions", () => {
const teamsPath = path.join(tmpDir, "OWNERTEAMS");
fs.writeFileSync(teamsPath, "team:platform @alice @bob @carol\n");
const teams = parseOwnerTeams(teamsPath);
assert.equal(teams.size, 1);
assert.deepEqual(teams.get("team:platform"), ["alice", "bob", "carol"]);
});
it("parses multiple teams", () => {
const teamsPath = path.join(tmpDir, "OWNERTEAMS");
fs.writeFileSync(teamsPath, "team:platform @alice @bob\nteam:bundle @carol @dave\n");
const teams = parseOwnerTeams(teamsPath);
assert.equal(teams.size, 2);
assert.deepEqual(teams.get("team:platform"), ["alice", "bob"]);
assert.deepEqual(teams.get("team:bundle"), ["carol", "dave"]);
});
it("skips comments and blank lines", () => {
const teamsPath = path.join(tmpDir, "OWNERTEAMS");
fs.writeFileSync(teamsPath, "# comment\n\nteam:platform @alice\n");
const teams = parseOwnerTeams(teamsPath);
assert.equal(teams.size, 1);
});
it("returns empty map if file does not exist", () => {
const teams = parseOwnerTeams(path.join(tmpDir, "NONEXISTENT"));
assert.equal(teams.size, 0);
});
});
// --- parseOwnersFile with team aliases ---
describe("parseOwnersFile with OWNERTEAMS", () => {
let tmpDir;
let ownersPath;
let teamsPath;
before(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "owners-teams-test-"));
ownersPath = path.join(tmpDir, "OWNERS");
teamsPath = path.join(tmpDir, "OWNERTEAMS");
});
after(() => {
fs.rmSync(tmpDir, { recursive: true });
});
it("expands team aliases to members", () => {
fs.writeFileSync(teamsPath, "team:platform @alice @bob\n");
fs.writeFileSync(ownersPath, "/cmd/auth/ team:platform\n");
const rules = parseOwnersFile(ownersPath);
assert.equal(rules.length, 1);
assert.deepEqual(rules[0].owners, ["alice", "bob"]);
});
it("mixes team aliases with individual owners", () => {
fs.writeFileSync(teamsPath, "team:platform @alice @bob\n");
fs.writeFileSync(ownersPath, "/cmd/auth/ team:platform @carol\n");
const rules = parseOwnersFile(ownersPath);
assert.equal(rules.length, 1);
assert.deepEqual(rules[0].owners, ["alice", "bob", "carol"]);
});
it("unknown team alias is ignored", () => {
fs.writeFileSync(teamsPath, "team:platform @alice\n");
fs.writeFileSync(ownersPath, "/cmd/auth/ team:unknown @bob\n");
const rules = parseOwnersFile(ownersPath);
assert.deepEqual(rules[0].owners, ["bob"]);
});
it("works without OWNERTEAMS file", () => {
const tmpDir2 = fs.mkdtempSync(path.join(os.tmpdir(), "owners-noteams-"));
const ownersPath2 = path.join(tmpDir2, "OWNERS");
fs.writeFileSync(ownersPath2, "* @alice\n");
const rules = parseOwnersFile(ownersPath2);
assert.deepEqual(rules[0].owners, ["alice"]);
fs.rmSync(tmpDir2, { recursive: true });
});
});
// --- findOwners ---
describe("findOwners", () => {
const rules = [
{ pattern: "*", owners: ["maintainer1", "maintainer2"] },
{ pattern: "/cmd/pipelines/", owners: ["pipelinesOwner"] },
{ pattern: "/cmd/apps/", owners: ["appsOwner"] },
];
it("last match wins", () => {
const owners = findOwners("cmd/pipelines/foo.go", rules);
assert.deepEqual(owners, ["pipelinesOwner"]);
});
it("file matching only * returns catch-all owners", () => {
const owners = findOwners("README.md", rules);
assert.deepEqual(owners, ["maintainer1", "maintainer2"]);
});
it("file matching specific rule returns that rule's owners", () => {
const owners = findOwners("cmd/apps/main.go", rules);
assert.deepEqual(owners, ["appsOwner"]);
});
it("returns empty array when no rules match", () => {
const noWildcard = [{ pattern: "/cmd/pipelines/", owners: ["owner1"] }];
const owners = findOwners("bundle/config.go", noWildcard);
assert.deepEqual(owners, []);
});
});
// --- getMaintainers ---
describe("getMaintainers", () => {
it("returns owners from * rule", () => {
const rules = [
{ pattern: "*", owners: ["alice", "bob"] },
{ pattern: "/cmd/", owners: ["carol"] },
];
assert.deepEqual(getMaintainers(rules), ["alice", "bob"]);
});
it("returns empty array if no * rule", () => {
const rules = [{ pattern: "/cmd/", owners: ["carol"] }];
assert.deepEqual(getMaintainers(rules), []);
});
});
// --- getOwnershipGroups ---
describe("getOwnershipGroups", () => {
const rules = [
{ pattern: "*", owners: ["maintainer"] },
{ pattern: "/cmd/pipelines/", owners: ["pipelinesOwner"] },
{ pattern: "/cmd/apps/", owners: ["appsOwner"] },
{ pattern: "/bundle/", owners: ["bundleOwner"] },
];
it("single file matching one rule -> one group", () => {
const groups = getOwnershipGroups(["cmd/pipelines/foo.go"], rules);
assert.equal(groups.size, 1);
assert.ok(groups.has("/cmd/pipelines/"));
assert.deepEqual(groups.get("/cmd/pipelines/").owners, ["pipelinesOwner"]);
assert.deepEqual(groups.get("/cmd/pipelines/").files, ["cmd/pipelines/foo.go"]);
});
it("multiple files matching same rule -> grouped together", () => {
const groups = getOwnershipGroups(
["cmd/pipelines/foo.go", "cmd/pipelines/bar.go"],
rules
);
assert.equal(groups.size, 1);
assert.deepEqual(groups.get("/cmd/pipelines/").files, [
"cmd/pipelines/foo.go",
"cmd/pipelines/bar.go",
]);
});
it("files matching different rules -> separate groups", () => {
const groups = getOwnershipGroups(
["cmd/pipelines/foo.go", "cmd/apps/bar.go"],
rules
);
assert.equal(groups.size, 2);
assert.ok(groups.has("/cmd/pipelines/"));
assert.ok(groups.has("/cmd/apps/"));
});
it("file matching only * -> group with * key", () => {
const groups = getOwnershipGroups(["README.md"], rules);
assert.equal(groups.size, 1);
assert.ok(groups.has("*"));
assert.deepEqual(groups.get("*").owners, ["maintainer"]);
assert.deepEqual(groups.get("*").files, ["README.md"]);
});
it("file matching no rule -> skipped", () => {
const noWildcard = [{ pattern: "/cmd/pipelines/", owners: ["owner1"] }];
const groups = getOwnershipGroups(["unrelated/file.go"], noWildcard);
assert.equal(groups.size, 0);
});
it("cross-domain: /cmd/pipelines/ and /cmd/apps/ -> two groups", () => {
const groups = getOwnershipGroups(
[
"cmd/pipelines/a.go",
"cmd/pipelines/b.go",
"cmd/apps/c.go",
],
rules
);
assert.equal(groups.size, 2);
assert.deepEqual(groups.get("/cmd/pipelines/").files, [
"cmd/pipelines/a.go",
"cmd/pipelines/b.go",
]);
assert.deepEqual(groups.get("/cmd/apps/").files, ["cmd/apps/c.go"]);
});
it("mixed: domain files + *-only files -> both groups present", () => {
const groups = getOwnershipGroups(
["cmd/pipelines/a.go", "README.md"],
rules
);
assert.equal(groups.size, 2);
assert.ok(groups.has("/cmd/pipelines/"));
assert.ok(groups.has("*"));
});
});