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