addons/isl-server/src/__tests__/templates.test.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {CodeReviewSystem} from 'isl/src/types';
b69ab319import path from 'path';
b69ab3110import {mockLogger} from 'shared/testUtils';
b69ab3111import {COMMIT_END_MARK, findMaxCommonPathPrefix, parseCommitInfoOutput} from '../templates';
b69ab3112
b69ab3113const MOCK_GITHUB_REVIEW_SYSTEM: CodeReviewSystem = {
b69ab3114 type: 'github',
b69ab3115 repo: 'eden',
b69ab3116 hostname: 'github.com',
b69ab3117 owner: 'facebook',
b69ab3118};
b69ab3119
b69ab3120describe('template parsing', () => {
b69ab3121 it('parses normal commits', () => {
b69ab3122 expect(
b69ab3123 parseCommitInfoOutput(
b69ab3124 mockLogger,
b69ab3125 `\
b69ab312677fdcef8759fb65da46a7a6431310829f12cef5b
b69ab3127Commit A
b69ab3128Author <author@example.com>
b69ab31292024-04-24 14:16:24 -0700
b69ab3130draft
b69ab3131
b69ab3132
b69ab31333f41d88ab69446763404eccd0f3e579352ba2753\x00
b69ab3134
b69ab3135
b69ab3136sapling/addons/isl/README.md
b69ab31371
b69ab3138
b69ab3139
b69ab31401
b69ab3141false
b69ab3142|
b69ab3143Commit A
b69ab3144
b69ab3145Summary:
b69ab3146this is my summary
b69ab3147${COMMIT_END_MARK}
b69ab3148e4594714fb9b3410a0ef4affc955f9d76d61c8a7
b69ab3149Commit B
b69ab3150Author <author@example.com>
b69ab31512024-04-24 12:19:08 -0700
b69ab3152draft
b69ab3153
b69ab3154
b69ab31552934650733c9181bdf64b7d00f5e0c7ca93d7ed7\x00
b69ab3156
b69ab3157
b69ab3158sapling/addons/isl/README.md\x00sapling/addons/isl/package.json
b69ab31591
b69ab3160
b69ab31619637166dabea9ac50ccb93902f3f41df4d8a15c4,
b69ab3162
b69ab3163false
b69ab3164|
b69ab3165Commit B
b69ab3166`,
b69ab3167 MOCK_GITHUB_REVIEW_SYSTEM,
b69ab3168 null,
b69ab3169 ),
b69ab3170 ).toEqual([
b69ab3171 {
b69ab3172 author: 'Author <author@example.com>',
b69ab3173 bookmarks: [],
b69ab3174 closestPredecessors: [],
b69ab3175 date: new Date('2024-04-24T21:16:24.000Z'),
b69ab3176 description: 'Summary:\nthis is my summary',
b69ab3177 diffId: '1',
b69ab3178 filePathsSample: ['sapling/addons/isl/README.md'],
b69ab3179 hash: '77fdcef8759fb65da46a7a6431310829f12cef5b',
b69ab3180 isDot: false,
b69ab3181 isFollower: false,
b69ab3182 parents: ['3f41d88ab69446763404eccd0f3e579352ba2753'],
b69ab3183 grandparents: [],
b69ab3184 phase: 'draft',
b69ab3185 remoteBookmarks: [],
b69ab3186 stableCommitMetadata: undefined,
b69ab3187 successorInfo: undefined,
b69ab3188 title: 'Commit A',
b69ab3189 totalFileCount: 1,
b69ab3190 maxCommonPathPrefix: 'sapling/addons/isl/',
b69ab3191 },
b69ab3192 {
b69ab3193 author: 'Author <author@example.com>',
b69ab3194 bookmarks: [],
b69ab3195 closestPredecessors: ['9637166dabea9ac50ccb93902f3f41df4d8a15c4'],
b69ab3196 date: new Date('2024-04-24T19:19:08.000Z'),
b69ab3197 description: '',
b69ab3198 diffId: undefined,
b69ab3199 filePathsSample: ['sapling/addons/isl/README.md', 'sapling/addons/isl/package.json'],
b69ab31100 hash: 'e4594714fb9b3410a0ef4affc955f9d76d61c8a7',
b69ab31101 isDot: false,
b69ab31102 isFollower: false,
b69ab31103 parents: ['2934650733c9181bdf64b7d00f5e0c7ca93d7ed7'],
b69ab31104 grandparents: [],
b69ab31105 phase: 'draft',
b69ab31106 remoteBookmarks: [],
b69ab31107 stableCommitMetadata: undefined,
b69ab31108 successorInfo: undefined,
b69ab31109 title: 'Commit B',
b69ab31110 totalFileCount: 1,
b69ab31111 maxCommonPathPrefix: 'sapling/addons/isl/',
b69ab31112 },
b69ab31113 ]);
b69ab31114 });
b69ab31115
b69ab31116 it('handles commits with no title+description ', () => {
b69ab31117 expect(
b69ab31118 parseCommitInfoOutput(
b69ab31119 mockLogger,
b69ab31120 `\
b69ab3112177fdcef8759fb65da46a7a6431310829f12cef5b
b69ab31122
b69ab31123Author <author@example.com>
b69ab311242024-04-24 14:16:24 -0700
b69ab31125draft
b69ab31126
b69ab31127
b69ab311283f41d88ab69446763404eccd0f3e579352ba2753\x00
b69ab31129
b69ab31130
b69ab31131sapling/addons/isl/README.md
b69ab311321
b69ab31133
b69ab31134
b69ab311351
b69ab31136false
b69ab31137|
b69ab31138${COMMIT_END_MARK}
b69ab31139`,
b69ab31140 MOCK_GITHUB_REVIEW_SYSTEM,
b69ab31141 null,
b69ab31142 ),
b69ab31143 ).toEqual([
b69ab31144 {
b69ab31145 author: 'Author <author@example.com>',
b69ab31146 bookmarks: [],
b69ab31147 closestPredecessors: [],
b69ab31148 date: new Date('2024-04-24T21:16:24.000Z'),
b69ab31149 description: '',
b69ab31150 diffId: '1',
b69ab31151 filePathsSample: ['sapling/addons/isl/README.md'],
b69ab31152 hash: '77fdcef8759fb65da46a7a6431310829f12cef5b',
b69ab31153 isDot: false,
b69ab31154 isFollower: false,
b69ab31155 parents: ['3f41d88ab69446763404eccd0f3e579352ba2753'],
b69ab31156 grandparents: [],
b69ab31157 phase: 'draft',
b69ab31158 remoteBookmarks: [],
b69ab31159 stableCommitMetadata: undefined,
b69ab31160 successorInfo: undefined,
b69ab31161 title: '',
b69ab31162 totalFileCount: 1,
b69ab31163 maxCommonPathPrefix: 'sapling/addons/isl/',
b69ab31164 },
b69ab31165 ]);
b69ab31166 });
b69ab31167});
b69ab31168
b69ab31169describe('max common path prefix', () => {
b69ab31170 it('extracts common prefix', () => {
b69ab31171 expect(findMaxCommonPathPrefix(['a/b/c', 'a/b/d', 'a/b/d/e'])).toEqual('a/b/');
b69ab31172 expect(
b69ab31173 findMaxCommonPathPrefix(['a/b/c1', 'a/b/c2', 'a/b/d/e/f', 'a/b/d/e/f/g/h/i', 'a/b/q']),
b69ab31174 ).toEqual('a/b/');
b69ab31175 expect(
b69ab31176 findMaxCommonPathPrefix([
b69ab31177 'addons/isl/src/file.ts',
b69ab31178 'addons/isl/README.md',
b69ab31179 'addons/isl/src/another.ts',
b69ab31180 ]),
b69ab31181 ).toEqual('addons/isl/');
b69ab31182 });
b69ab31183 it('handles root as common', () => {
b69ab31184 expect(findMaxCommonPathPrefix(['a/b/c', 'd/e/f'])).toEqual('');
b69ab31185 expect(findMaxCommonPathPrefix(['www/README', 'fbcode/foo'])).toEqual('');
b69ab31186 expect(findMaxCommonPathPrefix(['subdir/some/file', 'toplevel'])).toEqual('');
b69ab31187 });
b69ab31188 it('acts on full dir names', () => {
b69ab31189 expect(findMaxCommonPathPrefix(['a/foo1/a', 'a/foo2/b', 'a/foo3/c'])).toEqual(
b69ab31190 'a/', // not a/foo
b69ab31191 );
b69ab31192 expect(findMaxCommonPathPrefix(['foo/bananaspoon', 'foo/banana'])).toEqual(
b69ab31193 'foo/', // not foo/banana
b69ab31194 );
b69ab31195 expect(findMaxCommonPathPrefix(['foo/banana', 'foo/bananaspoon'])).toEqual(
b69ab31196 'foo/', // not foo/banana
b69ab31197 );
b69ab31198 });
b69ab31199 describe('windows', () => {
b69ab31200 const oldSep = path.sep;
b69ab31201 beforeEach(() => {
b69ab31202 (path.sep as string) = '\\';
b69ab31203 });
b69ab31204 afterEach(() => {
b69ab31205 (path.sep as string) = oldSep;
b69ab31206 });
b69ab31207
b69ab31208 it('handles windows paths', () => {
b69ab31209 expect(
b69ab31210 findMaxCommonPathPrefix([
b69ab31211 'addons\\isl\\src\\file.ts',
b69ab31212 'addons\\isl\\README.md',
b69ab31213 'addons\\isl\\src\\another.ts',
b69ab31214 ]),
b69ab31215 ).toEqual('addons\\isl\\');
b69ab31216 });
b69ab31217 });
b69ab31218});