From 266d9baece4c6d6696b2b4c7bd2e86f6d89f052d Mon Sep 17 00:00:00 2001 From: Matt Travi Date: Sat, 13 May 2023 23:46:32 -0500 Subject: [PATCH] fix(scaffold-git): properly handle git response when no remotes have yet been defined to list --- src/vcs/git.js | 14 +++++++++++++- src/vcs/git.test.js | 16 ++++++++++++---- .../features/step_definitions/vcs/git-steps.mjs | 6 ++++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/vcs/git.js b/src/vcs/git.js index 531a8c5b..ae16f6b6 100644 --- a/src/vcs/git.js +++ b/src/vcs/git.js @@ -22,9 +22,21 @@ function generateConfigFiles(projectRoot, ignore) { ].filter(Boolean)); } +async function getExistingRemotes(git) { + try { + return await git.listRemote(); + } catch (e) { + if ('fatal: No remote configured to list refs from.' === e.message) { + return []; + } + + throw e; + } +} + async function defineRemoteOrigin(projectRoot, origin) { const git = simpleGit(projectRoot); - const existingRemotes = await git.listRemote(); + const existingRemotes = await getExistingRemotes(git); if (existingRemotes.includes('origin')) { warn('The `origin` remote is already defined for this repository'); diff --git a/src/vcs/git.test.js b/src/vcs/git.test.js index d194ad7f..0534daf4 100644 --- a/src/vcs/git.test.js +++ b/src/vcs/git.test.js @@ -85,11 +85,10 @@ describe('git', () => { }); describe('scaffold', () => { - beforeEach(() => { - listRemote.mockResolvedValue(any.listOf(any.word)); - }); - it('should scaffold the git repo', async () => { + // listRemote.mockRejectedValue(new simpleGit.GitError(null, 'fatal: No remote configured to list refs from.')); + listRemote.mockRejectedValue(new Error('fatal: No remote configured to list refs from.')); + const results = await scaffold({projectRoot, origin: {}}); expect(fs.writeFile).toHaveBeenCalledWith(`${projectRoot}/.gitattributes`, '* text=auto'); @@ -99,9 +98,17 @@ describe('git', () => { expect(results.nextSteps).toEqual([{summary: 'Commit scaffolded files'}]); }); + it('throws git errors that are not a lack of defined remotes', async () => { + const error = new Error(any.sentence()); + listRemote.mockRejectedValue(error); + + await expect(scaffold({projectRoot, origin: {}})).rejects.toThrow(error); + }); + it('should create the ignore file when patterns are defined', async () => { const directories = any.listOf(any.string); const files = any.listOf(any.string); + listRemote.mockResolvedValue(any.listOf(any.word)); await scaffold({projectRoot, ignore: {directories, files}, origin: {}}); @@ -115,6 +122,7 @@ describe('git', () => { const sshUrl = any.url(); // const branch = any.simpleObject(); // gitBranch.lookup.withArgs(repository, 'master', gitBranch.BRANCH.LOCAL).resolves(branch); + listRemote.mockResolvedValue(any.listOf(any.word)); const results = await scaffold({projectRoot, origin: {sshUrl}}); diff --git a/test/integration/features/step_definitions/vcs/git-steps.mjs b/test/integration/features/step_definitions/vcs/git-steps.mjs index e86e351d..94895b41 100644 --- a/test/integration/features/step_definitions/vcs/git-steps.mjs +++ b/test/integration/features/step_definitions/vcs/git-steps.mjs @@ -1,7 +1,8 @@ import {promises as fs} from 'node:fs'; -import {fileExists} from '@form8ion/core'; +import {GitError} from 'simple-git'; import makeDir from 'make-dir'; +import {fileExists} from '@form8ion/core'; import {Given, Then} from '@cucumber/cucumber'; import {assert} from 'chai'; @@ -18,7 +19,8 @@ Given(/^the project should be versioned in git$/, async function () { td.when(this.git.simpleGit(process.cwd())).thenReturn(simpleGitInstance); td.when(simpleGitInstance.checkIsRepo('root')).thenResolve(false); - td.when(simpleGitInstance.listRemote()).thenResolve([]); + td.when(simpleGitInstance.listRemote()) + .thenReject(new GitError(null, 'fatal: No remote configured to list refs from.')); }); Given(/^the project should not be versioned in git$/, async function () {