Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use existence of backup as marker for installation #54

Merged
merged 2 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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