|
| 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 | +}) |
0 commit comments