From 310477e5b92d0c664e9fb9f7281ee6277175fa3e Mon Sep 17 00:00:00 2001 From: Ben Monro Date: Fri, 24 May 2024 22:44:40 -0700 Subject: [PATCH] add tests for fetch sloc Summary: tsia Reviewed By: quark-zju Differential Revision: D57795331 fbshipit-source-id: 263f1e457bd0569e09b39beecd9d667ab4e2179e --- .../src/__tests__/Repository.test.ts | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/addons/isl-server/src/__tests__/Repository.test.ts b/addons/isl-server/src/__tests__/Repository.test.ts index 584da047edeee..44b8430b9e048 100644 --- a/addons/isl-server/src/__tests__/Repository.test.ts +++ b/addons/isl-server/src/__tests__/Repository.test.ts @@ -376,6 +376,78 @@ describe('Repository', () => { }); }); + describe('fetchSloc', () => { + const repoInfo: ValidatedRepoInfo = { + type: 'success', + command: 'sl', + dotdir: '/path/to/repo/.sl', + repoRoot: '/path/to/repo', + codeReviewSystem: {type: 'unknown'}, + pullRequestDomain: undefined, + }; + + const EXAMPLE_DIFFSTAT = ` +| 34 ++++++++++ +www/flib/intern/entity/diff/EntPhabricatorDiffSchema.php | 11 +++ +2 files changed, 45 insertions(+), 0 deletions(-)\n`; + + it('parses sloc', async () => { + const repo = new Repository(repoInfo, ctx); + + const execaSpy = mockExeca([[/^sl diff/, () => ({stdout: EXAMPLE_DIFFSTAT})]]); + const results = repo.fetchSignificantLinesOfCode(ctx, 'abcdef', ['generated.file']); + await expect(results).resolves.toEqual(45); + expect(execaSpy).toHaveBeenCalledWith( + 'sl', + expect.arrayContaining([ + 'diff', + '-B', + '-X', + '**__generated__**', + '-X', + '/path/to/repo/generated.file', + '-c', + 'abcdef', + ]), + expect.anything(), + ); + }); + + it('handles empty generated list', async () => { + const repo = new Repository(repoInfo, ctx); + const execaSpy = mockExeca([[/^sl diff/, () => ({stdout: EXAMPLE_DIFFSTAT})]]); + repo.fetchSignificantLinesOfCode(ctx, 'abcdef', []); + expect(execaSpy).toHaveBeenCalledWith( + 'sl', + expect.arrayContaining(['diff', '-B', '-X', '**__generated__**', '-c', 'abcdef']), + expect.anything(), + ); + }); + + it('handles multiple generated files', async () => { + const repo = new Repository(repoInfo, ctx); + const execaSpy = mockExeca([[/^sl diff/, () => ({stdout: EXAMPLE_DIFFSTAT})]]); + const generatedFiles = ['generated1.file', 'generated2.file']; + repo.fetchSignificantLinesOfCode(ctx, 'abcdef', generatedFiles); + expect(execaSpy).toHaveBeenCalledWith( + 'sl', + expect.arrayContaining([ + 'diff', + '-B', + '-X', + '**__generated__**', + '-X', + '/path/to/repo/generated1.file', + '-X', + '/path/to/repo/generated2.file', + '-c', + 'abcdef', + ]), + expect.anything(), + ); + }); + }); + describe('fetchSmartlogCommits', () => { const repoInfo: ValidatedRepoInfo = { type: 'success',