From 6a1945efa5e41a934146ba3c823590fc69426d0c Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Thu, 26 Apr 2018 14:19:20 +0100 Subject: [PATCH 1/2] Use existence of backup as marker for installation * Always make a backup when installing * Always check for backup when uninstalling --- lib/git-hooks.js | 12 ++++++------ tests/uninstall.test.js | 15 ++++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/git-hooks.js b/lib/git-hooks.js index f7f911c..37c55f1 100644 --- a/lib/git-hooks.js +++ b/lib/git-hooks.js @@ -47,9 +47,11 @@ module.exports = { throw new Error('git-hooks already installed'); } - if (fsHelpers.exists(hooksPath)) { - fs.renameSync(hooksPath, hooksOldPath); + if (!fsHelpers.exists(hooksPath)) { + // Standard hooks folder has been removed - make an empty placeholder one + fsHelpers.makeDir(hooksPath); } + fs.renameSync(hooksPath, hooksOldPath); var hookTemplate = fs.readFileSync(__dirname + '/' + HOOKS_TEMPLATE_FILE_NAME); var pathToGitHooks = __dirname; @@ -86,13 +88,11 @@ module.exports = { var hooksPath = path.resolve(gitPath, HOOKS_DIRNAME); var hooksOldPath = path.resolve(gitPath, HOOKS_OLD_DIRNAME); - if (!fsHelpers.exists(hooksPath)) { - throw new Error('git-hooks is not installed'); - } - if (fsHelpers.exists(hooksOldPath)) { fsHelpers.removeDir(hooksPath); fs.renameSync(hooksOldPath, hooksPath); + } else { + throw new Error('git-hooks is not installed'); } }, diff --git a/tests/uninstall.test.js b/tests/uninstall.test.js index f15673e..5856ba7 100644 --- a/tests/uninstall.test.js +++ b/tests/uninstall.test.js @@ -1,15 +1,17 @@ require('chai').should(); +var execSync = require('child_process').execSync; var gitHooks = require('../lib/git-hooks'); var fsHelpers = require('../lib/fs-helpers'); -var SANDBOX_PATH = __dirname + '/tmp-sandbox/'; +var SANDBOX_PATH = '/tmp/tmp-sandbox/'; var GIT_ROOT = SANDBOX_PATH + '.git/'; var GIT_HOOKS = GIT_ROOT + 'hooks'; var GIT_HOOKS_OLD = GIT_ROOT + 'hooks.old'; describe('--uninstall', function () { beforeEach(function () { - fsHelpers.makeDir(GIT_ROOT); + fsHelpers.makeDir(SANDBOX_PATH); + execSync('git init', {cwd: SANDBOX_PATH}); }); afterEach(function () { @@ -39,19 +41,18 @@ describe('--uninstall', function () { }); describe('when backup is absent', function () { - beforeEach(function () { - fsHelpers.makeDir(GIT_HOOKS); - }); it('should not remove hooks directory', function () { - gitHooks.uninstall(SANDBOX_PATH); + var fn = function () { + gitHooks.uninstall(SANDBOX_PATH); + }; + fn.should.throw(Error); fsHelpers.exists(GIT_HOOKS).should.be.true; }); }); describe('when backup exists', function () { beforeEach(function () { - fsHelpers.makeDir(GIT_HOOKS); fsHelpers.makeDir(GIT_HOOKS_OLD); }); From 2597d0e06e0a0514236ca2e3c96f875055e614f8 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Tue, 1 May 2018 18:14:05 +0100 Subject: [PATCH 2/2] Fix tests for node 0.10 which has no execSync --- tests/uninstall.test.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/uninstall.test.js b/tests/uninstall.test.js index 5856ba7..56fa421 100644 --- a/tests/uninstall.test.js +++ b/tests/uninstall.test.js @@ -1,5 +1,5 @@ require('chai').should(); -var execSync = require('child_process').execSync; +var exec = require('child_process').exec; var gitHooks = require('../lib/git-hooks'); var fsHelpers = require('../lib/fs-helpers'); @@ -9,9 +9,14 @@ var GIT_HOOKS = GIT_ROOT + 'hooks'; var GIT_HOOKS_OLD = GIT_ROOT + 'hooks.old'; describe('--uninstall', function () { - beforeEach(function () { + beforeEach(function (done) { fsHelpers.makeDir(SANDBOX_PATH); - execSync('git init', {cwd: SANDBOX_PATH}); + exec('git init', {cwd: SANDBOX_PATH}, function (err) { + if (err) { + throw err; + } + done(); + }); }); afterEach(function () {