Skip to content

Commit e83af69

Browse files
rjmunrotarmolov
authored andcommitted
Use existence of backup as marker for installation (#54)
* 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
1 parent 9fd9355 commit e83af69

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

lib/git-hooks.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ module.exports = {
4747
throw new Error('git-hooks already installed');
4848
}
4949

50-
if (fsHelpers.exists(hooksPath)) {
51-
fs.renameSync(hooksPath, hooksOldPath);
50+
if (!fsHelpers.exists(hooksPath)) {
51+
// Standard hooks folder has been removed - make an empty placeholder one
52+
fsHelpers.makeDir(hooksPath);
5253
}
54+
fs.renameSync(hooksPath, hooksOldPath);
5355

5456
var hookTemplate = fs.readFileSync(__dirname + '/' + HOOKS_TEMPLATE_FILE_NAME);
5557
var pathToGitHooks = __dirname;
@@ -86,13 +88,11 @@ module.exports = {
8688
var hooksPath = path.resolve(gitPath, HOOKS_DIRNAME);
8789
var hooksOldPath = path.resolve(gitPath, HOOKS_OLD_DIRNAME);
8890

89-
if (!fsHelpers.exists(hooksPath)) {
90-
throw new Error('git-hooks is not installed');
91-
}
92-
9391
if (fsHelpers.exists(hooksOldPath)) {
9492
fsHelpers.removeDir(hooksPath);
9593
fs.renameSync(hooksOldPath, hooksPath);
94+
} else {
95+
throw new Error('git-hooks is not installed');
9696
}
9797
},
9898

tests/uninstall.test.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
require('chai').should();
2+
var exec = require('child_process').exec;
23
var gitHooks = require('../lib/git-hooks');
34
var fsHelpers = require('../lib/fs-helpers');
45

5-
var SANDBOX_PATH = __dirname + '/tmp-sandbox/';
6+
var SANDBOX_PATH = '/tmp/tmp-sandbox/';
67
var GIT_ROOT = SANDBOX_PATH + '.git/';
78
var GIT_HOOKS = GIT_ROOT + 'hooks';
89
var GIT_HOOKS_OLD = GIT_ROOT + 'hooks.old';
910

1011
describe('--uninstall', function () {
11-
beforeEach(function () {
12-
fsHelpers.makeDir(GIT_ROOT);
12+
beforeEach(function (done) {
13+
fsHelpers.makeDir(SANDBOX_PATH);
14+
exec('git init', {cwd: SANDBOX_PATH}, function (err) {
15+
if (err) {
16+
throw err;
17+
}
18+
done();
19+
});
1320
});
1421

1522
afterEach(function () {
@@ -39,19 +46,18 @@ describe('--uninstall', function () {
3946
});
4047

4148
describe('when backup is absent', function () {
42-
beforeEach(function () {
43-
fsHelpers.makeDir(GIT_HOOKS);
44-
});
4549

4650
it('should not remove hooks directory', function () {
47-
gitHooks.uninstall(SANDBOX_PATH);
51+
var fn = function () {
52+
gitHooks.uninstall(SANDBOX_PATH);
53+
};
54+
fn.should.throw(Error);
4855
fsHelpers.exists(GIT_HOOKS).should.be.true;
4956
});
5057
});
5158

5259
describe('when backup exists', function () {
5360
beforeEach(function () {
54-
fsHelpers.makeDir(GIT_HOOKS);
5561
fsHelpers.makeDir(GIT_HOOKS_OLD);
5662
});
5763

0 commit comments

Comments
 (0)