Skip to content

Commit

Permalink
fix(scaffold-git): properly handle git response when no remotes have …
Browse files Browse the repository at this point in the history
…yet been defined to list
  • Loading branch information
travi committed May 14, 2023
1 parent 52c5ce5 commit 266d9ba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
14 changes: 13 additions & 1 deletion src/vcs/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
16 changes: 12 additions & 4 deletions src/vcs/git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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: {}});

Expand All @@ -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}});

Expand Down
6 changes: 4 additions & 2 deletions test/integration/features/step_definitions/vcs/git-steps.mjs
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 () {
Expand Down

0 comments on commit 266d9ba

Please sign in to comment.