|
| 1 | +import * as fs from 'fs'; |
| 2 | + |
1 | 3 | import { OwnershipEngine } from './OwnershipEngine'; |
2 | 4 | import { FileOwnershipMatcher } from './types'; |
3 | 5 |
|
| 6 | +jest.mock('fs'); |
| 7 | +const readFileSyncMock = fs.readFileSync as jest.Mock; |
| 8 | + |
4 | 9 | describe('OwnershipEngine', () => { |
| 10 | + afterEach(() => { |
| 11 | + jest.resetAllMocks(); |
| 12 | + }); |
| 13 | + |
5 | 14 | describe('calcFileOwnership', () => { |
6 | 15 | const createFileOwnershipMatcher = (path: string, owners: string[]): FileOwnershipMatcher => { |
7 | 16 | return { |
@@ -50,4 +59,81 @@ describe('OwnershipEngine', () => { |
50 | 59 | expect(result).not.toContainEqual(unexpectedOwner); |
51 | 60 | }); |
52 | 61 | }); |
| 62 | + |
| 63 | + describe('FromCodeownersFile', () => { |
| 64 | + it('should not throw when provided valid owners', () => { |
| 65 | + // Arrange |
| 66 | + const codeowners = 'some/path @global-owner1 @org/octocat docs@example.com'; |
| 67 | + |
| 68 | + readFileSyncMock.mockReturnValue(Buffer.from(codeowners)); |
| 69 | + |
| 70 | + // Assert |
| 71 | + expect(() => OwnershipEngine.FromCodeownersFile('some/codeowners/file')).not.toThrow(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should throw when provided an invalid owner', () => { |
| 75 | + // Arrange |
| 76 | + const rulePath = 'some/path'; |
| 77 | + const owner = '.not@valid-owner'; |
| 78 | + |
| 79 | + const expectedError = new Error(`${owner} is not a valid owner name in rule ${rulePath} ${owner}`); |
| 80 | + |
| 81 | + const codeowners = `${rulePath} ${owner}`; |
| 82 | + |
| 83 | + readFileSyncMock.mockReturnValue(Buffer.from(codeowners)); |
| 84 | + |
| 85 | + // Assert |
| 86 | + expect(() => OwnershipEngine.FromCodeownersFile('some/codeowners/file')) |
| 87 | + .toThrowError(expectedError); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should throw when provided an invalid github user as an owner', () => { |
| 91 | + // Arrange |
| 92 | + const rulePath = 'some/path'; |
| 93 | + const owner = 'invalid-owner'; |
| 94 | + |
| 95 | + const expectedError = new Error(`${owner} is not a valid owner name in rule ${rulePath} ${owner}`); |
| 96 | + |
| 97 | + const codeowners = `${rulePath} ${owner}`; |
| 98 | + |
| 99 | + readFileSyncMock.mockReturnValue(Buffer.from(codeowners)); |
| 100 | + |
| 101 | + // Assert |
| 102 | + expect(() => OwnershipEngine.FromCodeownersFile('some/codeowners/file')) |
| 103 | + .toThrowError(expectedError); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should throw when provided an invalid email address as an owner', () => { |
| 107 | + // Arrange |
| 108 | + const rulePath = 'some/path'; |
| 109 | + const owner = 'invalid-owner@nowhere'; |
| 110 | + |
| 111 | + const expectedError = new Error(`${owner} is not a valid owner name in rule ${rulePath} ${owner}`); |
| 112 | + |
| 113 | + const codeowners = `${rulePath} ${owner}`; |
| 114 | + |
| 115 | + readFileSyncMock.mockReturnValue(Buffer.from(codeowners)); |
| 116 | + |
| 117 | + // Assert |
| 118 | + expect(() => OwnershipEngine.FromCodeownersFile('some/codeowners/file')) |
| 119 | + .toThrowError(expectedError); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should throw when provided at least one invalid owner', () => { |
| 123 | + // Arrange |
| 124 | + const rulePath = 'some/path'; |
| 125 | + const valid = 'valid@owner.com'; |
| 126 | + const owner = '@invalid-owner*'; |
| 127 | + |
| 128 | + const expectedError = new Error(`${owner} is not a valid owner name in rule ${rulePath} ${valid} ${owner}`); |
| 129 | + |
| 130 | + const codeowners = `${rulePath} ${valid} ${owner}`; |
| 131 | + |
| 132 | + readFileSyncMock.mockReturnValue(Buffer.from(codeowners)); |
| 133 | + |
| 134 | + // Assert |
| 135 | + expect(() => OwnershipEngine.FromCodeownersFile('some/codeowners/file')) |
| 136 | + .toThrowError(expectedError); |
| 137 | + }); |
| 138 | + }); |
53 | 139 | }); |
0 commit comments