Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit fbd5f60

Browse files
authored
Increase coverage (#47)
* adding coverage for logger * adding a jest mock * silencing logger during tests * adding tests for file
1 parent 15db91c commit fbd5f60

3 files changed

Lines changed: 122 additions & 0 deletions

File tree

src/lib/logger/logger.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { log } from './logger';
2+
3+
describe('log', () => {
4+
it('should log an error', () => {
5+
// Arrange
6+
const expectedMsg = 'expected message';
7+
const expectedErr = new Error('expect error');
8+
const spy = jest.spyOn(console, 'error').mockReturnValue(undefined);
9+
10+
// Act
11+
log.error(expectedMsg, expectedErr);
12+
13+
// Assert
14+
expect(spy).toHaveBeenCalledWith(expectedMsg, expectedErr);
15+
});
16+
17+
it('should log a warning', () => {
18+
// Arrange
19+
const expectedMsg = 'expected message';
20+
const expectedErr = new Error('expect error');
21+
const spy = jest.spyOn(console, 'warn').mockReturnValue(undefined);
22+
23+
// Act
24+
log.warn(expectedMsg, expectedErr);
25+
26+
// Assert
27+
expect(spy).toHaveBeenCalledWith(expectedMsg, expectedErr);
28+
});
29+
});

src/lib/ownership/file.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { getFileOwnership } from './file';
2+
3+
import { OwnershipEngine } from './lib/OwnershipEngine';
4+
5+
import { readDirRecursively } from './lib/readDirRecursively';
6+
import { readTrackedGitFiles } from './lib/readTrackedGitFiles';
7+
import { countLinesInFile } from './lib/countLinesInFile';
8+
import { OwnedFile } from './lib/OwnedFile';
9+
10+
jest.mock('./lib/OwnershipEngine');
11+
jest.mock('./lib/readDirRecursively');
12+
jest.mock('./lib/readTrackedGitFiles');
13+
jest.mock('./lib/countLinesInFile');
14+
15+
describe('file', () => {
16+
const mocks = {
17+
readDirRecursively: readDirRecursively as jest.Mock,
18+
readTrackedGitFiles: readTrackedGitFiles as jest.Mock,
19+
countLinesInFile: countLinesInFile as jest.Mock,
20+
};
21+
22+
beforeEach(() => {
23+
jest.resetAllMocks();
24+
mocks.readDirRecursively.mockResolvedValue([]);
25+
mocks.readTrackedGitFiles.mockResolvedValue([]);
26+
mocks.countLinesInFile.mockResolvedValue(0);
27+
});
28+
29+
describe('getFileOwnership', () => {
30+
it('should create an engine using the specified code ownersfile', async () => {
31+
// Arrange
32+
const expected = 'some/file';
33+
34+
// Act
35+
await getFileOwnership({ codeowners: expected, dir: '/', onlyGit: false });
36+
37+
// Assert
38+
expect(OwnershipEngine.FromCodeownersFile).toHaveBeenLastCalledWith(expected);
39+
});
40+
41+
it('should read files from the directory when onlyGit is not specified', async () => {
42+
// Arrange
43+
const expected = 'some/dir';
44+
45+
// Act
46+
await getFileOwnership({ codeowners: expected, dir: expected, onlyGit: false });
47+
48+
// Assert
49+
expect(mocks.readDirRecursively).toHaveBeenCalledWith(expected, ['.git']);
50+
});
51+
52+
it('should read files from git tracked files when onlyGit is specified', async () => {
53+
// Arrange
54+
const expected = 'some/dir';
55+
56+
// Act
57+
await getFileOwnership({ codeowners: expected, dir: expected, onlyGit: true });
58+
59+
// Assert
60+
expect(mocks.readTrackedGitFiles).toHaveBeenCalledWith(expected);
61+
});
62+
63+
it('should return owned files', async () => {
64+
// Arrange
65+
const expected = [
66+
new OwnedFile({ path: 'is/not-owned', owners: ['@some/owner'], lines: 0 }),
67+
new OwnedFile({ path: 'is/owned', owners: ['@some/owner'], lines: 0 }),
68+
];
69+
70+
const mockEngine = OwnershipEngine as any;
71+
mockEngine.FromCodeownersFile.mockImplementation(() => {
72+
return {
73+
...OwnershipEngine,
74+
calcFileOwnership: (path: string) => {
75+
const matching = expected.find(f => f.path === path);
76+
if (!matching) throw new Error('unexpected path');
77+
return matching.owners;
78+
},
79+
};
80+
});
81+
82+
mocks.readDirRecursively.mockResolvedValue(expected.map(f => f.path).reverse());
83+
84+
// Act
85+
const result = await getFileOwnership({ codeowners: 'some/file', dir: '/', onlyGit: false });
86+
87+
// Assert
88+
expect(result).toEqual(expected);
89+
});
90+
});
91+
});

src/lib/ownership/lib/OwnershipEngine.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as fs from 'fs';
33
import { OwnershipEngine } from './OwnershipEngine';
44
import { FileOwnershipMatcher } from '../types';
55

6+
jest.mock('../../logger');
7+
68
jest.mock('fs');
79
const readFileSyncMock = fs.readFileSync as jest.Mock;
810

0 commit comments

Comments
 (0)