| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {CodeReviewSystem} from 'isl/src/types'; |
| 9 | import path from 'path'; |
| 10 | import {mockLogger} from 'shared/testUtils'; |
| 11 | import {COMMIT_END_MARK, findMaxCommonPathPrefix, parseCommitInfoOutput} from '../templates'; |
| 12 | |
| 13 | const MOCK_GITHUB_REVIEW_SYSTEM: CodeReviewSystem = { |
| 14 | type: 'github', |
| 15 | repo: 'eden', |
| 16 | hostname: 'github.com', |
| 17 | owner: 'facebook', |
| 18 | }; |
| 19 | |
| 20 | describe('template parsing', () => { |
| 21 | it('parses normal commits', () => { |
| 22 | expect( |
| 23 | parseCommitInfoOutput( |
| 24 | mockLogger, |
| 25 | `\ |
| 26 | 77fdcef8759fb65da46a7a6431310829f12cef5b |
| 27 | Commit A |
| 28 | Author <author@example.com> |
| 29 | 2024-04-24 14:16:24 -0700 |
| 30 | draft |
| 31 | |
| 32 | |
| 33 | 3f41d88ab69446763404eccd0f3e579352ba2753\x00 |
| 34 | |
| 35 | |
| 36 | sapling/addons/isl/README.md |
| 37 | 1 |
| 38 | |
| 39 | |
| 40 | 1 |
| 41 | false |
| 42 | | |
| 43 | Commit A |
| 44 | |
| 45 | Summary: |
| 46 | this is my summary |
| 47 | ${COMMIT_END_MARK} |
| 48 | e4594714fb9b3410a0ef4affc955f9d76d61c8a7 |
| 49 | Commit B |
| 50 | Author <author@example.com> |
| 51 | 2024-04-24 12:19:08 -0700 |
| 52 | draft |
| 53 | |
| 54 | |
| 55 | 2934650733c9181bdf64b7d00f5e0c7ca93d7ed7\x00 |
| 56 | |
| 57 | |
| 58 | sapling/addons/isl/README.md\x00sapling/addons/isl/package.json |
| 59 | 1 |
| 60 | |
| 61 | 9637166dabea9ac50ccb93902f3f41df4d8a15c4, |
| 62 | |
| 63 | false |
| 64 | | |
| 65 | Commit B |
| 66 | `, |
| 67 | MOCK_GITHUB_REVIEW_SYSTEM, |
| 68 | null, |
| 69 | ), |
| 70 | ).toEqual([ |
| 71 | { |
| 72 | author: 'Author <author@example.com>', |
| 73 | bookmarks: [], |
| 74 | closestPredecessors: [], |
| 75 | date: new Date('2024-04-24T21:16:24.000Z'), |
| 76 | description: 'Summary:\nthis is my summary', |
| 77 | diffId: '1', |
| 78 | filePathsSample: ['sapling/addons/isl/README.md'], |
| 79 | hash: '77fdcef8759fb65da46a7a6431310829f12cef5b', |
| 80 | isDot: false, |
| 81 | isFollower: false, |
| 82 | parents: ['3f41d88ab69446763404eccd0f3e579352ba2753'], |
| 83 | grandparents: [], |
| 84 | phase: 'draft', |
| 85 | remoteBookmarks: [], |
| 86 | stableCommitMetadata: undefined, |
| 87 | successorInfo: undefined, |
| 88 | title: 'Commit A', |
| 89 | totalFileCount: 1, |
| 90 | maxCommonPathPrefix: 'sapling/addons/isl/', |
| 91 | }, |
| 92 | { |
| 93 | author: 'Author <author@example.com>', |
| 94 | bookmarks: [], |
| 95 | closestPredecessors: ['9637166dabea9ac50ccb93902f3f41df4d8a15c4'], |
| 96 | date: new Date('2024-04-24T19:19:08.000Z'), |
| 97 | description: '', |
| 98 | diffId: undefined, |
| 99 | filePathsSample: ['sapling/addons/isl/README.md', 'sapling/addons/isl/package.json'], |
| 100 | hash: 'e4594714fb9b3410a0ef4affc955f9d76d61c8a7', |
| 101 | isDot: false, |
| 102 | isFollower: false, |
| 103 | parents: ['2934650733c9181bdf64b7d00f5e0c7ca93d7ed7'], |
| 104 | grandparents: [], |
| 105 | phase: 'draft', |
| 106 | remoteBookmarks: [], |
| 107 | stableCommitMetadata: undefined, |
| 108 | successorInfo: undefined, |
| 109 | title: 'Commit B', |
| 110 | totalFileCount: 1, |
| 111 | maxCommonPathPrefix: 'sapling/addons/isl/', |
| 112 | }, |
| 113 | ]); |
| 114 | }); |
| 115 | |
| 116 | it('handles commits with no title+description ', () => { |
| 117 | expect( |
| 118 | parseCommitInfoOutput( |
| 119 | mockLogger, |
| 120 | `\ |
| 121 | 77fdcef8759fb65da46a7a6431310829f12cef5b |
| 122 | |
| 123 | Author <author@example.com> |
| 124 | 2024-04-24 14:16:24 -0700 |
| 125 | draft |
| 126 | |
| 127 | |
| 128 | 3f41d88ab69446763404eccd0f3e579352ba2753\x00 |
| 129 | |
| 130 | |
| 131 | sapling/addons/isl/README.md |
| 132 | 1 |
| 133 | |
| 134 | |
| 135 | 1 |
| 136 | false |
| 137 | | |
| 138 | ${COMMIT_END_MARK} |
| 139 | `, |
| 140 | MOCK_GITHUB_REVIEW_SYSTEM, |
| 141 | null, |
| 142 | ), |
| 143 | ).toEqual([ |
| 144 | { |
| 145 | author: 'Author <author@example.com>', |
| 146 | bookmarks: [], |
| 147 | closestPredecessors: [], |
| 148 | date: new Date('2024-04-24T21:16:24.000Z'), |
| 149 | description: '', |
| 150 | diffId: '1', |
| 151 | filePathsSample: ['sapling/addons/isl/README.md'], |
| 152 | hash: '77fdcef8759fb65da46a7a6431310829f12cef5b', |
| 153 | isDot: false, |
| 154 | isFollower: false, |
| 155 | parents: ['3f41d88ab69446763404eccd0f3e579352ba2753'], |
| 156 | grandparents: [], |
| 157 | phase: 'draft', |
| 158 | remoteBookmarks: [], |
| 159 | stableCommitMetadata: undefined, |
| 160 | successorInfo: undefined, |
| 161 | title: '', |
| 162 | totalFileCount: 1, |
| 163 | maxCommonPathPrefix: 'sapling/addons/isl/', |
| 164 | }, |
| 165 | ]); |
| 166 | }); |
| 167 | }); |
| 168 | |
| 169 | describe('max common path prefix', () => { |
| 170 | it('extracts common prefix', () => { |
| 171 | expect(findMaxCommonPathPrefix(['a/b/c', 'a/b/d', 'a/b/d/e'])).toEqual('a/b/'); |
| 172 | expect( |
| 173 | findMaxCommonPathPrefix(['a/b/c1', 'a/b/c2', 'a/b/d/e/f', 'a/b/d/e/f/g/h/i', 'a/b/q']), |
| 174 | ).toEqual('a/b/'); |
| 175 | expect( |
| 176 | findMaxCommonPathPrefix([ |
| 177 | 'addons/isl/src/file.ts', |
| 178 | 'addons/isl/README.md', |
| 179 | 'addons/isl/src/another.ts', |
| 180 | ]), |
| 181 | ).toEqual('addons/isl/'); |
| 182 | }); |
| 183 | it('handles root as common', () => { |
| 184 | expect(findMaxCommonPathPrefix(['a/b/c', 'd/e/f'])).toEqual(''); |
| 185 | expect(findMaxCommonPathPrefix(['www/README', 'fbcode/foo'])).toEqual(''); |
| 186 | expect(findMaxCommonPathPrefix(['subdir/some/file', 'toplevel'])).toEqual(''); |
| 187 | }); |
| 188 | it('acts on full dir names', () => { |
| 189 | expect(findMaxCommonPathPrefix(['a/foo1/a', 'a/foo2/b', 'a/foo3/c'])).toEqual( |
| 190 | 'a/', // not a/foo |
| 191 | ); |
| 192 | expect(findMaxCommonPathPrefix(['foo/bananaspoon', 'foo/banana'])).toEqual( |
| 193 | 'foo/', // not foo/banana |
| 194 | ); |
| 195 | expect(findMaxCommonPathPrefix(['foo/banana', 'foo/bananaspoon'])).toEqual( |
| 196 | 'foo/', // not foo/banana |
| 197 | ); |
| 198 | }); |
| 199 | describe('windows', () => { |
| 200 | const oldSep = path.sep; |
| 201 | beforeEach(() => { |
| 202 | (path.sep as string) = '\\'; |
| 203 | }); |
| 204 | afterEach(() => { |
| 205 | (path.sep as string) = oldSep; |
| 206 | }); |
| 207 | |
| 208 | it('handles windows paths', () => { |
| 209 | expect( |
| 210 | findMaxCommonPathPrefix([ |
| 211 | 'addons\\isl\\src\\file.ts', |
| 212 | 'addons\\isl\\README.md', |
| 213 | 'addons\\isl\\src\\another.ts', |
| 214 | ]), |
| 215 | ).toEqual('addons\\isl\\'); |
| 216 | }); |
| 217 | }); |
| 218 | }); |
| 219 | |