5.5 KB219 lines
Blame
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
8import type {CodeReviewSystem} from 'isl/src/types';
9import path from 'path';
10import {mockLogger} from 'shared/testUtils';
11import {COMMIT_END_MARK, findMaxCommonPathPrefix, parseCommitInfoOutput} from '../templates';
12
13const MOCK_GITHUB_REVIEW_SYSTEM: CodeReviewSystem = {
14 type: 'github',
15 repo: 'eden',
16 hostname: 'github.com',
17 owner: 'facebook',
18};
19
20describe('template parsing', () => {
21 it('parses normal commits', () => {
22 expect(
23 parseCommitInfoOutput(
24 mockLogger,
25 `\
2677fdcef8759fb65da46a7a6431310829f12cef5b
27Commit A
28Author <author@example.com>
292024-04-24 14:16:24 -0700
30draft
31
32
333f41d88ab69446763404eccd0f3e579352ba2753\x00
34
35
36sapling/addons/isl/README.md
371
38
39
401
41false
42|
43Commit A
44
45Summary:
46this is my summary
47${COMMIT_END_MARK}
48e4594714fb9b3410a0ef4affc955f9d76d61c8a7
49Commit B
50Author <author@example.com>
512024-04-24 12:19:08 -0700
52draft
53
54
552934650733c9181bdf64b7d00f5e0c7ca93d7ed7\x00
56
57
58sapling/addons/isl/README.md\x00sapling/addons/isl/package.json
591
60
619637166dabea9ac50ccb93902f3f41df4d8a15c4,
62
63false
64|
65Commit 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 `\
12177fdcef8759fb65da46a7a6431310829f12cef5b
122
123Author <author@example.com>
1242024-04-24 14:16:24 -0700
125draft
126
127
1283f41d88ab69446763404eccd0f3e579352ba2753\x00
129
130
131sapling/addons/isl/README.md
1321
133
134
1351
136false
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
169describe('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