/
code-owners.spec.ts
84 lines (82 loc) · 3.06 KB
/
code-owners.spec.ts
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
import { mock } from 'jest-mock-extended';
import { fs, platform } from '../../../test/util';
import { Pr } from '../../platform';
import { codeOwnersForPr } from './code-owners';
jest.mock('../../util/fs');
describe('workers/pr/code-owners', () => {
describe('codeOwnersForPr', () => {
let pr: Pr;
beforeEach(() => {
jest.resetAllMocks();
pr = mock<Pr>();
});
it('returns global code owner', async () => {
fs.readLocalFile.mockResolvedValueOnce(['* @jimmy'].join('\n'));
platform.getPrFiles.mockResolvedValueOnce(['README.md']);
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual(['@jimmy']);
});
it('returns more specific code owners', async () => {
fs.readLocalFile.mockResolvedValueOnce(
['* @jimmy', 'package.json @john @maria'].join('\n')
);
platform.getPrFiles.mockResolvedValueOnce(['package.json']);
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual(['@john', '@maria']);
});
it('ignores comments and leading/trailing whitespace', async () => {
fs.readLocalFile.mockResolvedValueOnce(
[
'# comment line',
' \t ',
' * @jimmy ',
' # comment line with leading whitespace',
' package.json @john @maria ',
].join('\n')
);
platform.getPrFiles.mockResolvedValueOnce(['package.json']);
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual(['@john', '@maria']);
});
it('returns empty array when no code owners set', async () => {
fs.readLocalFile.mockResolvedValueOnce(null);
platform.getPrFiles.mockResolvedValueOnce(['package.json']);
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual([]);
});
it('returns empty array when no code owners match', async () => {
fs.readLocalFile.mockResolvedValueOnce(
['package-lock.json @mike'].join('\n')
);
platform.getPrFiles.mockResolvedValueOnce(['yarn.lock']);
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual([]);
});
it('returns empty array when error occurs', async () => {
fs.readLocalFile.mockImplementationOnce((_, __) => {
throw new Error();
});
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual([]);
});
const codeOwnerFilePaths = [
'CODEOWNERS',
'.github/CODEOWNERS',
'.gitlab/CODEOWNERS',
'docs/CODEOWNERS',
];
codeOwnerFilePaths.forEach((codeOwnerFilePath) => {
it(`detects code owner file at '${codeOwnerFilePath}'`, async () => {
fs.readLocalFile.mockImplementation((path, _) => {
if (path === codeOwnerFilePath) {
return Promise.resolve(['* @mike'].join('\n'));
}
return Promise.resolve(null);
});
platform.getPrFiles.mockResolvedValueOnce(['README.md']);
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual(['@mike']);
});
});
});
});