diff --git a/tests/index.js b/tests/index.js index 618a5e70..60b522b6 100644 --- a/tests/index.js +++ b/tests/index.js @@ -4,6 +4,7 @@ var test = require('tape') var createModule = require('./lib/create-module') +require('./scenarios/custom-analyzer')(test, createModule) require('./scenarios/ignore')(test, createModule) require('./scenarios/prepublish')(test, createModule) require('./scenarios/postpublish')(test, createModule) diff --git a/tests/lib/commit-to-version-test.js b/tests/lib/commit-to-version-test.js new file mode 100644 index 00000000..f087ddcc --- /dev/null +++ b/tests/lib/commit-to-version-test.js @@ -0,0 +1,37 @@ +'use strict' + +var fs = require('fs') +var exec = require('child_process').exec + +var nixt = require('nixt') + +module.exports = function (t, message, version, code, name, cwd) { + t.test(name, function (t) { + t.plan(3) + + nixt() + .cwd(cwd) + .env('CI', true) + .env('npm_config_registry', 'http://127.0.0.1:4873/') + .exec('git commit --allow-empty -m "' + message + '"') + .run('npm run prepublish') + .code(code) + .stdout(/> semantic-release pre\n\nDetermining new version\n/m) + .end(function (err) { + t.error(err, 'nixt') + + var pkg = JSON.parse(fs.readFileSync(cwd + '/package.json')) + t.is(pkg.version, version, 'version') + + if (code === 1) { + return t.error(null, 'no publish') + } + + exec('npm publish --ignore-scripts', {cwd: cwd}, function (err) { + setTimeout(function () { + t.error(err, 'publish') + }, 300) + }) + }) + }) +} diff --git a/tests/lib/custom-analyzer.js b/tests/lib/custom-analyzer.js new file mode 100644 index 00000000..cd548e70 --- /dev/null +++ b/tests/lib/custom-analyzer.js @@ -0,0 +1,20 @@ +'use strict' + +module.exports = function (commits) { + var type = null + + commits.every(function (commit) { + if (/FOO/.test(commit.message)) { + type = 'major' + return false + } + + if (/BAR/.test(commit.message)) type = 'minor' + + if (!type && /BAZ/.test(commit.message)) type = 'patch' + + return true + }) + + return type +} diff --git a/tests/scenarios/custom-analyzer.js b/tests/scenarios/custom-analyzer.js new file mode 100644 index 00000000..d571cb78 --- /dev/null +++ b/tests/scenarios/custom-analyzer.js @@ -0,0 +1,23 @@ +'use strict' + +var path = require('path') + +var efh = require('error-first-handler') + +var commitToVersionTest = require('../lib/commit-to-version-test') + +module.exports = function (test, createModule) { + createModule({ + release: { + analyzer: path.join(__dirname, '../lib/custom-analyzer') + } + }, efh()(function (name, cwd) { + test('custom-analyzer', function (t) { + commitToVersionTest(t, 'HO', '0.0.0', 1, 'abort publish w/o changes', cwd) + commitToVersionTest(t, 'BAZ', '1.0.0', 0, 'release 1.0.0 if unpublished', cwd) + commitToVersionTest(t, 'BAZ', '1.0.1', 0, 'bump patch for fix', cwd) + commitToVersionTest(t, 'BAR', '1.1.0', 0, 'bump minor for feature', cwd) + commitToVersionTest(t, 'FOO', '2.0.0', 0, 'bump major for breaking change', cwd) + }) + })) +} diff --git a/tests/scenarios/prepublish.js b/tests/scenarios/prepublish.js index 2fdd0e39..22b8e112 100644 --- a/tests/scenarios/prepublish.js +++ b/tests/scenarios/prepublish.js @@ -1,50 +1,17 @@ 'use strict' -var fs = require('fs') -var exec = require('child_process').exec - var efh = require('error-first-handler') -var nixt = require('nixt') + +var commitToVersionTest = require('../lib/commit-to-version-test') module.exports = function (test, createModule) { createModule(efh()(function (name, cwd) { test('prepublish', function (t) { - prepublishTest(t, 'refactor: change', '0.0.0', 1, 'abort publish w/o changes') - prepublishTest(t, 'fix: change', '1.0.0', 0, 'release 1.0.0 if unpublished') - prepublishTest(t, 'fix: change', '1.0.1', 0, 'bump patch for fix') - prepublishTest(t, 'feat: change', '1.1.0', 0, 'bump minor for feature') - prepublishTest(t, 'fix: BREAKING CHANGE: change', '2.0.0', 0, 'bump major for breaking change') + commitToVersionTest(t, 'refactor: change', '0.0.0', 1, 'abort publish w/o changes', cwd) + commitToVersionTest(t, 'fix: change', '1.0.0', 0, 'release 1.0.0 if unpublished', cwd) + commitToVersionTest(t, 'fix: change', '1.0.1', 0, 'bump patch for fix', cwd) + commitToVersionTest(t, 'feat: change', '1.1.0', 0, 'bump minor for feature', cwd) + commitToVersionTest(t, 'fix: BREAKING CHANGE: change', '2.0.0', 0, 'bump major for breaking change', cwd) }) - - function prepublishTest (t, message, version, code, name) { - t.test(name, function (t) { - t.plan(3) - - nixt() - .cwd(cwd) - .env('CI', true) - .env('npm_config_registry', 'http://127.0.0.1:4873/') - .exec('git commit --allow-empty -m "' + message + '"') - .run('npm run prepublish') - .code(code) - .stdout(/> semantic-release pre\n\nDetermining new version\n/m) - .end(function (err) { - t.error(err, 'nixt') - - var pkg = JSON.parse(fs.readFileSync(cwd + '/package.json')) - t.is(pkg.version, version, 'version') - - if (code === 1) { - return t.error(null, 'no publish') - } - - exec('npm publish --ignore-scripts', {cwd: cwd}, function (err) { - setTimeout(function () { - t.error(err, 'publish') - }, 300) - }) - }) - }) - } })) }