UNPKG

1.98 kBJavaScriptView Raw
1var should = require('should');
2var fs = require('fs');
3var path = require('path');
4
5var detectLocalGit = require('../lib/detectLocalGit');
6
7var ORIGINAL_CWD = process.cwd();
8var TEST_DIR = path.resolve(__dirname);
9var TEMP_GIT_DIR = path.join(TEST_DIR, '.git');
10
11describe("detectLocalGit", function() {
12
13 before(function() {
14 _makeTempGitDir();
15 process.chdir(TEST_DIR);
16 });
17
18 after(function() {
19 _cleanTempGitDir();
20 process.chdir(ORIGINAL_CWD);
21 });
22
23 it('should get commit hash from packed-refs when refs/heads/master does not exist', function() {
24 var results = detectLocalGit();
25 should.exist(results);
26 (results).should.deepEqual({
27 git_commit: '0000000000000000ffffffffffffffffffffffff',
28 git_branch: 'master'
29 });
30 });
31
32});
33
34function _makeTempGitDir() {
35
36 _cleanTempGitDir();
37
38 var dir = TEMP_GIT_DIR;
39
40 fs.mkdirSync(dir);
41
42 var HEAD = path.join(dir, 'HEAD');
43 var packedRefs = path.join(dir, 'packed-refs');
44
45 fs.writeFileSync(HEAD, 'ref: refs/heads/master');
46 fs.writeFileSync(packedRefs, "" +
47"# pack-refs with: peeled fully-peeled\n" +
48"0000000000000000000000000000000000000000 refs/heads/other/ref\n" +
49"0000000000000000ffffffffffffffffffffffff refs/heads/master\n" +
50"ffffffffffffffffffffffffffffffffffffffff refs/remotes/origin/other\n");
51
52}
53
54function _cleanTempGitDir() {
55 _deleteFolderRecursive(TEMP_GIT_DIR);
56}
57
58function _deleteFolderRecursive(dir) {
59
60 if (!dir.match('node-coveralls/test')) {
61 throw new Error('Tried to clean a temp git directory that did not match path: node-coveralls/test');
62 }
63
64 if(fs.existsSync(dir)) {
65
66 fs.readdirSync(dir).forEach(function(file,index){
67 var curPath = path.join(dir, file);
68 if(fs.lstatSync(curPath).isDirectory()) { // recurse
69 _deleteFolderRecursive(curPath);
70 } else { // delete file
71 fs.unlinkSync(curPath);
72 }
73 });
74
75 fs.rmdirSync(dir);
76 }
77}