Skip to content

Commit f7e9bd5

Browse files
rjmunrotarmolov
authored andcommitted
Test with real git dir (#58)
* Test with real git dir New code fails with fake .git dir as it doesn't guess, it asks git. * Use tmp package to get temporary folder name * Fix typo in uninstall test It was running the install, not uninstall command. * Add test case for git repo not having a hooks folder
1 parent 5951059 commit f7e9bd5

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"istanbul": "0.4.5",
5353
"jscs": "2.11.0",
5454
"jshint": "2.9.5",
55-
"mocha": "5.1.1"
55+
"mocha": "5.1.1",
56+
"tmp": "0.0.33"
5657
},
5758
"license": "MIT"
5859
}

tests/install.test.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
require('chai').should();
2+
var execSync = require('child_process').execSync;
23
var gitHooks = require('../lib/git-hooks');
34
var fsHelpers = require('../lib/fs-helpers');
5+
var tmp = require('tmp');
46

5-
var SANDBOX_PATH = __dirname + '/tmp-sandbox/';
7+
var tmpDir = tmp.dirSync();
8+
9+
var SANDBOX_PATH = tmpDir.name + '/git-hooks-sandbox/';
610
var GIT_ROOT = SANDBOX_PATH + '.git/';
711
var GIT_HOOKS = GIT_ROOT + 'hooks';
812
var GIT_HOOKS_OLD = GIT_ROOT + 'hooks.old';
913

1014
describe('--install', function () {
1115
beforeEach(function () {
12-
fsHelpers.makeDir(GIT_ROOT);
16+
fsHelpers.makeDir(SANDBOX_PATH);
17+
execSync('git init', {cwd: SANDBOX_PATH});
1318
});
1419

1520
afterEach(function () {
@@ -21,6 +26,17 @@ describe('--install', function () {
2126
fsHelpers.exists(GIT_HOOKS).should.be.true;
2227
});
2328

29+
describe('when git repo is missing its hooks folder', function () {
30+
beforeEach(function () {
31+
fsHelpers.removeDir(GIT_ROOT + '/hooks');
32+
});
33+
34+
it('should install hooks', function () {
35+
gitHooks.install(SANDBOX_PATH);
36+
fsHelpers.exists(GIT_HOOKS).should.be.true;
37+
});
38+
});
39+
2440
describe('when it is run not inside a git repo', function () {
2541
beforeEach(function () {
2642
fsHelpers.removeDir(GIT_ROOT);
@@ -35,10 +51,6 @@ describe('--install', function () {
3551
});
3652

3753
describe('when some hooks already exist', function () {
38-
beforeEach(function () {
39-
fsHelpers.makeDir(GIT_HOOKS);
40-
});
41-
4254
it('should backup hooks before installation', function () {
4355
gitHooks.install(SANDBOX_PATH);
4456
fsHelpers.exists(GIT_HOOKS_OLD).should.be.true;

tests/run.test.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
require('chai').should();
2+
var execSync = require('child_process').execSync;
23
var fs = require('fs');
34
var gitHooks = require('../lib/git-hooks');
45
var fsHelpers = require('../lib/fs-helpers');
6+
var tmp = require('tmp');
57

6-
var SANDBOX_PATH = __dirname + '/tmp-sandbox/';
8+
var tmpDir = tmp.dirSync();
9+
10+
var SANDBOX_PATH = tmpDir.name + '/git-hooks-sandbox/';
711
var GIT_ROOT = SANDBOX_PATH + '.git/';
812
var GIT_HOOKS = GIT_ROOT + 'hooks';
913
var PRECOMMIT_HOOK_PATH = GIT_HOOKS + '/pre-commit';
@@ -16,8 +20,8 @@ function createHook(path, content) {
1620

1721
describe('git-hook runner', function () {
1822
beforeEach(function () {
19-
fsHelpers.makeDir(GIT_ROOT);
20-
gitHooks.install(SANDBOX_PATH);
23+
fsHelpers.makeDir(SANDBOX_PATH);
24+
execSync('git init', {cwd: SANDBOX_PATH});
2125
});
2226

2327
afterEach(function () {

tests/uninstall.test.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ require('chai').should();
22
var execSync = require('child_process').execSync;
33
var gitHooks = require('../lib/git-hooks');
44
var fsHelpers = require('../lib/fs-helpers');
5+
var tmp = require('tmp');
56

6-
var SANDBOX_PATH = '/tmp/tmp-sandbox/';
7+
var tmpDir = tmp.dirSync();
8+
9+
var SANDBOX_PATH = tmpDir.name + '/git-hooks-sandbox/';
710
var GIT_ROOT = SANDBOX_PATH + '.git/';
811
var GIT_HOOKS = GIT_ROOT + 'hooks';
912
var GIT_HOOKS_OLD = GIT_ROOT + 'hooks.old';
@@ -34,7 +37,7 @@ describe('--uninstall', function () {
3437

3538
it('should throw an error', function () {
3639
var fn = function () {
37-
gitHooks.install(SANDBOX_PATH);
40+
gitHooks.uninstall(SANDBOX_PATH);
3841
};
3942
fn.should.throw(Error);
4043
});

0 commit comments

Comments
 (0)