Skip to content

Commit

Permalink
normalize git precon+UT
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-bompart committed Aug 17, 2021
1 parent 21f9ee6 commit 3706ef8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
85 changes: 85 additions & 0 deletions packages/cli/src/lib/decorators/preconditions/git.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
5 changes: 2 additions & 3 deletions packages/cli/src/lib/decorators/preconditions/git.ts
Original file line number Diff line number Diff line change
@@ -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',
});

0 comments on commit 3706ef8

Please sign in to comment.