From 3706ef85392157071358801971a3e6bebcf7d79d Mon Sep 17 00:00:00 2001 From: Louis Bompart Date: Tue, 17 Aug 2021 12:49:12 -0400 Subject: [PATCH] normalize git precon+UT https://coveord.atlassian.net/browse/CDX-478 --- .../lib/decorators/preconditions/git.spec.ts | 85 +++++++++++++++++++ .../src/lib/decorators/preconditions/git.ts | 5 +- 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 packages/cli/src/lib/decorators/preconditions/git.spec.ts diff --git a/packages/cli/src/lib/decorators/preconditions/git.spec.ts b/packages/cli/src/lib/decorators/preconditions/git.spec.ts new file mode 100644 index 0000000000..407c25159b --- /dev/null +++ b/packages/cli/src/lib/decorators/preconditions/git.spec.ts @@ -0,0 +1,85 @@ +jest.mock('../../utils/process'); + +import {dedent} from 'ts-dedent'; +import {constants} from 'os'; +import {mocked} from 'ts-jest/utils'; +import {spawnProcessOutput} from '../../utils/process'; +import {getFakeCommand} from './testsUtils/utils'; + +import {IsGitInstalled} from './git'; + +describe('IsGitInstalled', () => { + const mockedSpawnProcessOutput = mocked(spawnProcessOutput); + beforeEach(() => { + jest.resetAllMocks(); + }); + + describe('when git is not installed', () => { + beforeEach(() => { + mockedSpawnProcessOutput.mockResolvedValue({ + exitCode: constants.errno.ENOENT, + stderr: 'spawn git ENOENT', + stdout: '', + }); + }); + + it('should return false and warn', async () => { + const fakeCommand = getFakeCommand(); + + await expect(IsGitInstalled()(fakeCommand)).resolves.toBe(false); + expect(fakeCommand.warn).toHaveBeenCalledTimes(2); + expect(fakeCommand.warn).toHaveBeenNthCalledWith( + 1, + 'foo requires Git to run.' + ); + expect(fakeCommand.warn).toHaveBeenNthCalledWith( + 2, + dedent` + Please visit https://git-scm.com/book/en/v2/Getting-Started-Installing-Git for more detailed installation information. + ` + ); + }); + }); + + describe('when an unknown error happens while checking for git', () => { + beforeEach(() => { + mockedSpawnProcessOutput.mockResolvedValue({ + exitCode: 1, + stderr: 'some random error oh no', + stdout: '', + }); + }); + + it('should return false and warn', async () => { + const fakeCommand = getFakeCommand(); + + await expect(IsGitInstalled()(fakeCommand)).resolves.toBe(false); + expect(fakeCommand.warn).toHaveBeenCalledTimes(2); + expect(fakeCommand.warn).toHaveBeenCalledWith(dedent` + foo requires a valid Git installation to run. + An unknown error happened while running git --version. + some random error oh no + `); + expect(fakeCommand.warn).toHaveBeenCalledWith(dedent` + Please visit https://git-scm.com/book/en/v2/Getting-Started-Installing-Git for more detailed installation information. + `); + }); + }); + + describe('when git is installed', () => { + beforeEach(() => { + mockedSpawnProcessOutput.mockResolvedValue({ + exitCode: 0, + stderr: '', + stdout: '', + }); + }); + + it('should return true and not warn', async () => { + const fakeCommand = getFakeCommand(); + + await expect(IsGitInstalled()(fakeCommand)).resolves.toBe(true); + expect(fakeCommand.warn).toHaveBeenCalledTimes(0); + }); + }); +}); diff --git a/packages/cli/src/lib/decorators/preconditions/git.ts b/packages/cli/src/lib/decorators/preconditions/git.ts index 1efc5a9d1d..6e7ffc498c 100644 --- a/packages/cli/src/lib/decorators/preconditions/git.ts +++ b/packages/cli/src/lib/decorators/preconditions/git.ts @@ -1,7 +1,6 @@ import {getBinInstalledPrecondition} from './binPreconditionsFactory'; export const IsGitInstalled = getBinInstalledPrecondition('git', { - prettyName: 'git', - howToInstallBinText: - 'https://git-scm.com/book/en/v2/Getting-Started-Installing-Git', + prettyName: 'Git', + installLink: 'https://git-scm.com/book/en/v2/Getting-Started-Installing-Git', });