From 26d52fd4973ec0846156c3ab97dc7863c68a6f72 Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Wed, 6 Feb 2019 09:58:06 -0400 Subject: [PATCH 1/3] test(core): added init-git tests to cover known cases --- .../api/core/test/slow/init_git_spec_slow.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 packages/api/core/test/slow/init_git_spec_slow.ts diff --git a/packages/api/core/test/slow/init_git_spec_slow.ts b/packages/api/core/test/slow/init_git_spec_slow.ts new file mode 100644 index 0000000000..1e07609108 --- /dev/null +++ b/packages/api/core/test/slow/init_git_spec_slow.ts @@ -0,0 +1,46 @@ +import { execSync } from 'child_process'; +import fs from 'fs-extra'; +import os from 'os'; +import path from 'path'; + +import { expect } from 'chai'; + +import initGit from '../../src/api/init-scripts/init-git'; + +let dir: string; +let dirID = Date.now(); + +const ensureTestDirIsNonexistent = async () => { + dir = path.resolve(os.tmpdir(), `electron-forge-git-test-${dirID}`); + dirID += 1; + await fs.remove(dir); +}; + +describe('init-git', () => { + beforeEach(async () => { + await ensureTestDirIsNonexistent(); + await fs.mkdir(dir); + }); + + it('creates Git repository when run inside non-Git directory', async () => { + await initGit(dir); + const gitDir = path.join(dir, '.git'); + expect(await fs.pathExists(gitDir), 'the .git directory inside the folder').to.equal(true); + }); + + it('skips when run at root of Git repository', async () => { + await execSync('git init', { cwd: dir }); + + const gitDir = path.join(dir, '.git'); + const config = path.join(gitDir, 'config'); + const statBefore = await fs.lstat(config); + const before = statBefore.mtimeMs; + + await initGit(dir); + + const statAfter = await fs.lstat(config); + const after = statAfter.mtimeMs; + + expect(after, 'the config file in the repository').to.equal(before); + }); +}); From fef94bafbae876895455681c9eddd321ea3e13af Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Wed, 6 Feb 2019 09:59:54 -0400 Subject: [PATCH 2/3] test(core): add failing test for init-git catches case where init-git is run within a subdirectory of a known repository and forge should not create a nested .git directory --- .../api/core/test/slow/init_git_spec_slow.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/api/core/test/slow/init_git_spec_slow.ts b/packages/api/core/test/slow/init_git_spec_slow.ts index 1e07609108..f078a46d71 100644 --- a/packages/api/core/test/slow/init_git_spec_slow.ts +++ b/packages/api/core/test/slow/init_git_spec_slow.ts @@ -43,4 +43,26 @@ describe('init-git', () => { expect(after, 'the config file in the repository').to.equal(before); }); + + it('skips when run in subdirectory of Git repository', async () => { + await execSync('git init', { cwd: dir }); + + const gitDir = path.join(dir, '.git'); + const config = path.join(gitDir, 'config'); + const statBefore = await fs.lstat(config); + const before = statBefore.mtimeMs; + + const subdir = path.join(dir, 'some', 'other', 'folder'); + const innerGitDir = path.join(subdir, '.git'); + + await fs.mkdirp(subdir); + + await initGit(subdir); + + const statAfter = await fs.lstat(config); + const after = statAfter.mtimeMs; + + expect(after, 'the config file in the repository').to.equal(before); + expect(await fs.pathExists(innerGitDir), 'a nested .git directory inside the repository').to.equal(false); + }); }); From 5ab3ab5fcf1e701cf27d1359f54fdfff624a3a1c Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Wed, 6 Feb 2019 10:00:35 -0400 Subject: [PATCH 3/3] fix(core): do not initialize when in an existing git repository --- .../api/core/src/api/init-scripts/init-git.ts | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/api/core/src/api/init-scripts/init-git.ts b/packages/api/core/src/api/init-scripts/init-git.ts index 2d3c26fdc4..11a582b98f 100644 --- a/packages/api/core/src/api/init-scripts/init-git.ts +++ b/packages/api/core/src/api/init-scripts/init-git.ts @@ -1,24 +1,28 @@ import { asyncOra } from '@electron-forge/async-ora'; import { exec } from 'child_process'; import debug from 'debug'; -import fs from 'fs-extra'; -import path from 'path'; const d = debug('electron-forge:init:git'); export default async (dir: string) => { await asyncOra('Initializing Git Repository', async () => { - await new Promise(async (resolve, reject) => { - if (await fs.pathExists(path.resolve(dir, '.git'))) { - d('.git directory already exists, skipping git initialization'); - return resolve(); - } - d('executing "git init" in directory:', dir); - exec('git init', { + await new Promise((resolve, reject) => { + exec('git rev-parse --show-toplevel', { cwd: dir, }, (err) => { - if (err) return reject(err); - resolve(); + if (err) { + // not run within a Git repository + d('executing "git init" in directory:', dir); + exec('git init', { + cwd: dir, + }, (err) => { + if (err) return reject(err); + resolve(); + }); + } else { + d('.git directory already exists, skipping git initialization'); + resolve(); + } }); }); });