Skip to content

Commit

Permalink
major: prepared for semantic-release >= 11
Browse files Browse the repository at this point in the history
  • Loading branch information
AVVS committed Jan 4, 2018
1 parent 61965ce commit 889cc6d
Show file tree
Hide file tree
Showing 4 changed files with 2,870 additions and 1,121 deletions.
26 changes: 13 additions & 13 deletions index.js
Expand Up @@ -2,42 +2,42 @@
// https://semaphoreci.com/docs/available-environment-variables.html

const semver = require('semver')
const SRError = require('@semantic-release/error')
const SemanticReleaseError = require('@semantic-release/error')

module.exports = function (pluginConfig, config, cb) {
const env = config.env
const options = config.options
module.exports = async function (pluginConfig = {}, config = {}) {
const { env } = process
const { options = {} } = config

if (env.SEMAPHORE !== 'true') {
return cb(new SRError(
throw new SemanticReleaseError(
'semantic-release didn’t run on Semaphore and therefore a new version won’t be published.\n' +
'You can customize this behavior using "verifyConditions" plugins: git.io/sr-plugins',
'ENOSEMAPHORE'
))
)
}

if (env.hasOwnProperty('PULL_REQUEST_NUMBER')) {
return cb(new SRError(
throw new SemanticReleaseError(
'This test run was triggered by a pull request and therefore a new version won’t be published.',
'EPULLREQUEST'
))
)
}

if (options.branch === env.BRANCH_NAME) return cb(null)
if (options.branch === env.BRANCH_NAME) return null

if (semver.valid(env.BRANCH_NAME)) {
return cb(new SRError(
throw new SemanticReleaseError(
'This test run was triggered by a git tag that was created by semantic-release itself.\n' +
'Everything is okay. For log output of the actual publishing process look at the build that ran before this one.',
'EGITTAG'
))
)
}

return cb(new SRError(
throw new SemanticReleaseError(
'This test run was triggered on the branch ' + env.BRANCH_NAME +
', while semantic-release is configured to only publish from ' +
options.branch + '.\n' +
'You can customize this behavior using the "branch" option: git.io/sr-options',
'EBRANCHMISMATCH'
))
)
}
27 changes: 14 additions & 13 deletions package.json
@@ -1,23 +1,23 @@
{
"name": "@makeomatic/condition-semaphore",
"version": "0.0.0-development",
"description": "make sure only the right builds on semaphore get to publish",
"author": "Vitaly Aminev <v@makeomatic.ca> (https://makeomatic.ca)",
"bugs": {
"url": "https://github.com/makeomatic/condition-semaphore/issues"
},
"dependencies": {
"@semantic-release/error": "^1.0.0",
"@semantic-release/error": "^2.1.0",
"semver": "^5.1.0"
},
"devDependencies": {
"coveralls": "^2.11.2",
"github-post-release": "^1.12.1",
"ava": "^0.24.0",
"coveralls": "^3.0.0",
"nyc": "^11.0.3",
"pre-git": "^3.15.0",
"semantic-release": "^6.3.6",
"simple-commit-message": "^3.3.1",
"standard": "^10.0.2",
"tap": "^10.7.0"
"pre-git": "^3.17.0",
"semantic-release": "^11.0.2",
"simple-commit-message": "^4.0.0",
"standard": "^10.0.2"
},
"files": [
"index.js"
Expand All @@ -37,7 +37,9 @@
"branch": "master",
"verifyConditions": "./index.js",
"analyzeCommits": "simple-commit-message",
"generateNotes": "github-post-release"
"generateNotes": {
"config": "simple-commit-message"
}
},
"repository": {
"type": "git",
Expand All @@ -47,8 +49,8 @@
"coverage": "nyc report",
"coverage:upload": "npm run -s coverage -- --reporter=text-lcov | coveralls",
"pretest": "standard",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"test": "nyc tap --no-cov test.js",
"semantic-release": "semantic-release",
"test": "nyc ava -v --no-cov test.js",
"commit": "simple-commit-message"
},
"config": {
Expand All @@ -60,6 +62,5 @@
"post-checkout": [],
"post-merge": []
}
},
"version": "1.0.1"
}
}
148 changes: 63 additions & 85 deletions test.js
@@ -1,86 +1,64 @@
var test = require('tap').test
var SRError = require('@semantic-release/error')

var condition = require('./')

test('raise errors in semaphore environment', function (t) {
t.test('only runs on semaphore', function (tt) {
tt.plan(2)

condition({}, {env: {}}, function (err) {
tt.ok(err instanceof SRError)
tt.is(err.code, 'ENOSEMAPHORE')
})
})

t.test('not running on pull requests', function (tt) {
tt.plan(2)
condition({}, {
env: {
SEMAPHORE: 'true',
PULL_REQUEST_NUMBER: '1'
}
}, function (err) {
tt.ok(err instanceof SRError)
tt.is(err.code, 'EPULLREQUEST')
})
})

t.test('only running on specified branch', function (tt) {
tt.plan(5)

condition({}, {
env: {
SEMAPHORE: 'true',
BRANCH_NAME: 'master'
},
options: {
branch: 'master'
}
}, function (err) {
tt.is(err, null)
})

condition({}, {
env: {
SEMAPHORE: 'true',
BRANCH_NAME: 'notmaster'
},
options: {
branch: 'master'
}
}, function (err) {
tt.ok(err instanceof SRError)
tt.is(err.code, 'EBRANCHMISMATCH')
})

condition({}, {
env: {
SEMAPHORE: 'true',
BRANCH_NAME: 'master'
},
options: {
branch: 'foo'
}
}, function (err) {
tt.ok(err instanceof SRError)
tt.is(err.code, 'EBRANCHMISMATCH')
})
})

t.test('not running on tags', function (tt) {
tt.plan(2)
condition({}, {
env: {
SEMAPHORE: 'true',
BRANCH_NAME: 'v1.0.0'
},
options: {}
}, function (err) {
tt.ok(err instanceof SRError)
tt.is(err.code, 'EGITTAG')
})
})

t.end()
import test from 'ava'
import SemanticReleaseError from '@semantic-release/error'
import condition from './'

const envBackup = Object.assign({}, process.env)

test.beforeEach(() => {
// Delete env variables in case they are on the machine running the tests
delete process.env.SEMAPHORE
delete process.env.PULL_REQUEST_NUMBER
delete process.env.BRANCH_NAME
})

test.afterEach.always(() => {
// Restore process.env
process.env = envBackup
})

test.serial('only runs on semaphore', async (t) => {
const err = await t.throws(condition(), SemanticReleaseError)
t.is(err.code, 'ENOSEMAPHORE')
})

test.serial('not running on pull requests', async (t) => {
process.env.SEMAPHORE = 'true'
process.env.PULL_REQUEST_NUMBER = '1'

const err = await t.throws(condition(), SemanticReleaseError)
t.is(err.code, 'EPULLREQUEST')
})

test.serial('only running on specified branch', async (t) => {
let err
process.env.SEMAPHORE = 'true'
process.env.BRANCH_NAME = 'master'

await condition({}, { options: { branch: 'master' } })

process.env.BRANCH_NAME = 'notmaster'

err = await t.throws(
condition({}, { options: { branch: 'master' } }),
SemanticReleaseError
)

t.is(err.code, 'EBRANCHMISMATCH')

process.env.BRANCH_NAME = 'master'

err = await t.throws(
condition({}, { options: { branch: 'notmaster' } }),
SemanticReleaseError
)

t.is(err.code, 'EBRANCHMISMATCH')
})

test.serial('not running on tags', async (t) => {
process.env.SEMAPHORE = 'true'
process.env.BRANCH_NAME = 'v1.0.0'

const err = await t.throws(condition(), SemanticReleaseError)
t.is(err.code, 'EGITTAG')
})

0 comments on commit 889cc6d

Please sign in to comment.