|
| 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 | +}); |
0 commit comments