From bbba857517d6ca01f13ea1a7c2f999c1141fb5b5 Mon Sep 17 00:00:00 2001 From: Jacob Wejendorp Date: Sat, 30 Jun 2018 02:48:59 +0200 Subject: [PATCH] fix(release): more reliable branch detection Fixes dry-run issue on detached head checkouts. --- package.json | 1 + src/__mocks__/env-ci.js | 1 + src/scripts/__tests__/travis-after-success.js | 16 ++++++---------- src/scripts/travis-after-success.js | 17 +++++++---------- 4 files changed, 15 insertions(+), 20 deletions(-) create mode 100644 src/__mocks__/env-ci.js diff --git a/package.json b/package.json index 82893c5..e429a8d 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "cross-env": "^5.0.5", "cross-spawn": "^6.0.4", "doctoc": "^1.3.0", + "env-ci": "^2.1.1", "eslint": "^4.7.2", "eslint-config-tradeshift": "^5.0.0", "eslint-plugin-jest": "^21.17.0", diff --git a/src/__mocks__/env-ci.js b/src/__mocks__/env-ci.js new file mode 100644 index 0000000..6ee5856 --- /dev/null +++ b/src/__mocks__/env-ci.js @@ -0,0 +1 @@ +module.exports = jest.fn(); diff --git a/src/scripts/__tests__/travis-after-success.js b/src/scripts/__tests__/travis-after-success.js index 2170a07..6093135 100644 --- a/src/scripts/__tests__/travis-after-success.js +++ b/src/scripts/__tests__/travis-after-success.js @@ -11,6 +11,8 @@ cases( ({ version = '0.0.0-semantically-released', hasCoverageDir = true, branch = 'master' }) => { // beforeEach const { sync: crossSpawnSyncMock } = require('cross-spawn'); + const envCiMock = require('env-ci'); + const utils = require('../../utils'); utils.resolveBin = (modName, { executable = modName } = {}) => executable; const originalExit = process.exit; @@ -18,21 +20,15 @@ cases( // tests crossSpawnSyncMock.mockClear(); - crossSpawnSyncMock.mockReturnValue({ - stdout: { - toString() { - return branch; - } - } - }); + envCiMock.mockReturnValue({ branch }); if (version) { utils.pkg.version = version; } utils.hasFile = filename => filename === 'coverage' && hasCoverageDir; require('../travis-after-success'); - expect(crossSpawnSyncMock).toHaveBeenCalledTimes(2); - const [, secondCall] = crossSpawnSyncMock.mock.calls; - const [script, calledArgs] = secondCall; + expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1); + const [firstCall] = crossSpawnSyncMock.mock.calls; + const [script, calledArgs] = firstCall; expect([script, ...calledArgs].join(' ')).toMatchSnapshot(); // afterEach diff --git a/src/scripts/travis-after-success.js b/src/scripts/travis-after-success.js index d72d69c..6b013f3 100644 --- a/src/scripts/travis-after-success.js +++ b/src/scripts/travis-after-success.js @@ -1,4 +1,4 @@ - +const envCi = require('env-ci'); const spawn = require('cross-spawn'); const { resolveBin, getConcurrentlyArgs, hasFile, fromRoot, pkg } = require('../utils'); @@ -7,15 +7,10 @@ const autorelease = pkg.version === '0.0.0-semantically-released'; // We don't care about the TravisCI validation const releaseConfig = hasFile('release.config.js') ? require(fromRoot('release.config.js')) : {}; const releaseBranch = releaseConfig.branch || 'master'; -const currentBranch = spawn - .sync('git', ['rev-parse', '--abbrev-ref', 'HEAD']) - .stdout.toString() - .trim(); +const { branch } = envCi(); // Run a dry-run release when not on master -const dryRun = currentBranch !== releaseBranch; -const releaseFlags = ['--no-ci', dryRun ? '--dry-run' : ''] - .filter(Boolean) - .join(' '); +const dryRun = branch !== releaseBranch; +const releaseFlags = ['--no-ci', dryRun ? '--dry-run' : ''].filter(Boolean).join(' '); const result = spawn.sync( resolveBin('concurrently'), @@ -25,7 +20,9 @@ const result = spawn.sync( ? "echo installing codecov && npx -p codecov -c 'echo running codecov && codecov'" : null, release: autorelease - ? `echo installing semantic-release && npx -p semantic-release@15 -c 'echo running semantic-release ${dryRun ? 'dry run' : ''} && semantic-release ${releaseFlags}'` + ? `echo installing semantic-release && npx -p semantic-release@15 -c 'echo running semantic-release ${ + dryRun ? 'dry run' : '' + } && semantic-release ${releaseFlags}'` : null }, { killOthers: false }