Skip to content

Commit 1df7285

Browse files
committed
testing branch code
1 parent 659d43c commit 1df7285

File tree

3 files changed

+139
-2
lines changed

3 files changed

+139
-2
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@
8383
"nsp": "2.8.1",
8484
"pre-git": "3.15.3",
8585
"prettier-standard": "7.0.3",
86+
"ramda": "0.25.0",
8687
"semantic-release": "8.2.0",
8788
"simple-commit-message": "3.3.2",
88-
"standard": "10.0.3"
89+
"standard": "10.0.3",
90+
"stub-spawn-once": "2.3.0"
8991
},
9092
"dependencies": {
9193
"chdir-promise": "0.6.2",

src/utils-spec.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict'
2+
3+
const la = require('lazy-ass')
4+
const R = require('ramda')
5+
const { stubSpawnShellOnce } = require('stub-spawn-once')
6+
7+
/* eslint-env mocha */
8+
describe('utils', () => {
9+
describe('getBranch', () => {
10+
const { getBranch } = require('./utils')
11+
12+
context('CI environment', () => {
13+
const initialEnv = R.clone(process.env)
14+
15+
beforeEach(() => {
16+
process.env.TRAVIS_BRANCH = 'travis-branch'
17+
process.env.CI_BRANCH = 'ci-branch'
18+
})
19+
20+
afterEach(() => {
21+
process.env = R.clone(initialEnv)
22+
})
23+
24+
it('finds travis branch', () =>
25+
getBranch().then(branch =>
26+
la(branch === 'travis-branch', 'wrong branch', branch)
27+
))
28+
})
29+
30+
context('local environment', () => {
31+
const initialEnv = R.clone(process.env)
32+
33+
beforeEach(() => {
34+
// remove all possible CI branch variables
35+
delete process.env.CIRCLE_BRANCH
36+
delete process.env.TRAVIS_BRANCH
37+
delete process.env.BUILDKITE_BRANCH
38+
delete process.env.CI_BRANCH
39+
})
40+
41+
afterEach(() => {
42+
process.env = R.clone(initialEnv)
43+
})
44+
45+
it('uses git to determine branch', () => {
46+
const cmd = 'git rev-parse --abbrev-ref HEAD'
47+
stubSpawnShellOnce(cmd, 0, 'mock-test-branch', '')
48+
return getBranch().then(branch =>
49+
la(branch === 'mock-test-branch', 'wrong branch from git', branch)
50+
)
51+
})
52+
53+
it('returns empty string on failure', () => {
54+
const cmd = 'git rev-parse --abbrev-ref HEAD'
55+
stubSpawnShellOnce(cmd, 1, '', 'nope')
56+
return getBranch().then(branch =>
57+
la(branch === '', 'wrong empty branch from git', branch)
58+
)
59+
})
60+
61+
it('returns empty string on HEAD', () => {
62+
const cmd = 'git rev-parse --abbrev-ref HEAD'
63+
stubSpawnShellOnce(cmd, 0, 'HEAD', '')
64+
return getBranch().then(branch =>
65+
la(branch === '', 'wrong HEAD branch from git', branch)
66+
)
67+
})
68+
})
69+
})
70+
71+
describe('firstFoundValue', () => {
72+
const { firstFoundValue } = require('./utils')
73+
74+
const env = {
75+
a: 1,
76+
b: 2,
77+
c: 3
78+
}
79+
80+
it('finds first value', () => {
81+
const found = firstFoundValue(['a', 'b'], env)
82+
la(found === 1, found)
83+
})
84+
85+
it('finds second value', () => {
86+
const found = firstFoundValue(['z', 'a', 'b'], env)
87+
la(found === 1, found)
88+
})
89+
90+
it('finds nothing', () => {
91+
const found = firstFoundValue(['z', 'x'], env)
92+
la(found === null, found)
93+
})
94+
})
95+
})

src/utils.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const execa = require('execa')
33
const debug = require('debug')('commit-info')
44
const la = require('lazy-ass')
55
const is = require('check-more-types')
6+
const Promise = require('bluebird')
67

78
const prop = name => object => object[name]
89
const emptyString = () => ''
@@ -22,4 +23,43 @@ const runGitCommand = (pathToRepo, gitCommand) => {
2223
.catch(emptyString)
2324
}
2425

25-
module.exports = { runGitCommand }
26+
/*
27+
"gift" module returns "" for detached checkouts
28+
and our current command returns "HEAD"
29+
so we must imitate gift's behavior
30+
31+
example:
32+
git checkout <commit sha>
33+
get git branch returns "HEAD"
34+
*/
35+
const checkIfDetached = branch => (branch === 'HEAD' ? '' : branch)
36+
37+
function getGitBranch (pathToRepo) {
38+
return runGitCommand(pathToRepo, 'git rev-parse --abbrev-ref HEAD')
39+
.then(checkIfDetached)
40+
.catch(emptyString)
41+
}
42+
43+
function firstFoundValue (keys, object = process.env) {
44+
const found = keys.find(key => {
45+
return key in object
46+
})
47+
return found ? object[found] : null
48+
}
49+
50+
function getBranch (pathToRepo) {
51+
pathToRepo = pathToRepo || process.cwd()
52+
const ciNames = [
53+
'CIRCLE_BRANCH',
54+
'TRAVIS_BRANCH',
55+
'BUILDKITE_BRANCH',
56+
'CI_BRANCH'
57+
]
58+
const ciBranch = firstFoundValue(ciNames, process.env)
59+
if (ciBranch) {
60+
return Promise.resolve(ciBranch)
61+
}
62+
return getGitBranch(pathToRepo)
63+
}
64+
65+
module.exports = { runGitCommand, firstFoundValue, getBranch }

0 commit comments

Comments
 (0)