Skip to content

Commit

Permalink
Use existence of backup as marker for installation (#54)
Browse files Browse the repository at this point in the history
* Use existence of backup as marker for installation

* Always make a backup when installing
* Always check for backup when uninstalling

* Fix tests for node 0.10 which has no execSync
  • Loading branch information
rjmunro authored and tarmolov committed May 2, 2018
1 parent 9fd9355 commit e83af69
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
12 changes: 6 additions & 6 deletions lib/git-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
},

Expand Down
22 changes: 14 additions & 8 deletions tests/uninstall.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
require('chai').should();
var exec = require('child_process').exec;
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);
beforeEach(function (done) {
fsHelpers.makeDir(SANDBOX_PATH);
exec('git init', {cwd: SANDBOX_PATH}, function (err) {
if (err) {
throw err;
}
done();
});
});

afterEach(function () {
Expand Down Expand Up @@ -39,19 +46,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);
});

Expand Down

0 comments on commit e83af69

Please sign in to comment.