semantic-release/test/index.test.js
Pierre-Denis Vanduynslager e2a8a5cd32 feat: Refactor CLI to run with one command, improve logs, modularize, add tests
- Run with one command and do not rely on error exit codes to stop the process when a release is not necessary
- Break `index.js` in smaller modules in order to improve testability and simplify the code
- Add several missing unit and integration tests to reach 100% coverage
- Integration tests now test end to end, including publishing to Github (with http://www.mock-server.com on Docker)
- Use `tj/commander.js` to print an help message, verify and parse CLI arguments
- Semantic-release can now be called via Javascript API: `require('semantic-release')(options)`
- Remove npmlog dependency and add more log messages
- Logger is now passed to plugins
- Add debug logs with `visionmedia/debug`. `debug` is enabled for both semantic-release and plugins with `--debug`
- Use `kevva/npm-conf` in place of the deprecated `npm/npmconf`
- Pass lastRelease, nextRelease and commits to generate-notes plugin
- In dry-run mode, print the release note instead of publishing it to Github as draft, and skip the CI verifications
- The dry-run mode does not require npm and Github TOKEN to be set anymore and can be run locally

BREAKING CHANGE: Semantic-Release must now be executed with `semantic-release` instead of `semantic-release pre && npm publish && semantic-release post`.
BREAKING CHANGE: The `semantic-release` command now returns with exit code 0 on expected exception (no release has to be done, running on a PR, gitHead not found, other CI job failed etc...). It only returns with 1 when there is an unexpected error (code error in a plugin, plugin not found, git command cannot be run etc..).
BREAKING CHANGE: Calling the `semantic-release` command with unexpected argument(s) now exit with 1 and print an help message.
BREAKING CHANGE: Semantic-Release does not rely on `npmlog` anymore and the log level cannot be configured. Debug logs can be activated with CLI option `--debug` or with environment variable `DEBUG=semantic-release:*`
BREAKING CHANGE: The CLI options `--debug` doesn't enable the dry-run mode anymore but activate the debugs. The dry run mode is now set with the CLI command `--dry-run` or `-d`.
2017-10-27 18:07:13 -07:00

279 lines
10 KiB
JavaScript

import test from 'ava';
import proxyquire from 'proxyquire';
import {stub} from 'sinon';
import SemanticReleaseError from '@semantic-release/error';
const consoleLog = stub(console, 'log');
test.beforeEach(t => {
// Save the current process.env
t.context.env = Object.assign({}, process.env);
// Save the current working diretory
t.context.cwd = process.cwd();
});
test.afterEach.always(t => {
// Restore the current working directory
process.chdir(t.context.cwd);
// Restore process.env
process.env = Object.assign({}, t.context.env);
});
test.after.always(t => {
consoleLog.restore();
});
test('Plugins are called with expected values', async t => {
const env = {NPM_TOKEN: 'NPM_TOKEN'};
const pkgOptions = {branch: 'master'};
const cliOptions = {githubToken: 'GH_TOKEN'};
const options = Object.assign({}, pkgOptions, cliOptions);
const pkg = {name: 'available', release: options, repository: {url: 'http://github.com/whats/up.git'}};
const npm = {registry: 'http://test.registry.com'};
const lastRelease = {version: '1.0.0', gitHead: 'test_commit_head'};
const commitsLastRelease = {version: '1.0.0', gitHead: 'tag_head'};
const commits = [{hash: '1', message: 'fix: First fix'}, {hash: '2', message: 'feat: First feature'}];
const nextRelease = {type: 'major', version: '2.0.0'};
const notes = 'Release notes';
// Stub modules
const log = stub();
const error = stub();
const logger = {log, error};
const verifyAuth = stub().returns();
const publishNpm = stub().resolves();
const githubRelease = stub().resolves();
const getCommits = stub().resolves({commits, lastRelease: commitsLastRelease});
const getNextVersion = stub().returns(nextRelease.version);
// Stub plugins
const verifyConditions = stub().resolves();
const getLastRelease = stub().resolves(lastRelease);
const analyzeCommits = stub().resolves(nextRelease.type);
const verifyRelease = stub().resolves();
const generateNotes = stub().resolves(notes);
const getConfig = stub().resolves({
plugins: {getLastRelease, analyzeCommits, verifyRelease, verifyConditions, generateNotes},
env,
options,
pkg,
npm,
});
const semanticRelease = proxyquire('../src/index', {
'./lib/logger': logger,
'./lib/verify-auth': verifyAuth,
'./lib/get-config': getConfig,
'./lib/get-commits': getCommits,
'./lib/publish-npm': publishNpm,
'./lib/github-release': githubRelease,
'./lib/get-next-version': getNextVersion,
});
// Call the index module
await semanticRelease(cliOptions);
// Verify the sub-modules have been called with expected parameters
t.true(getConfig.calledOnce);
t.true(getConfig.calledWithExactly(cliOptions));
t.true(verifyAuth.calledOnce);
t.true(verifyAuth.calledWithExactly(options, env));
t.true(publishNpm.calledOnce);
t.true(publishNpm.calledWithExactly(pkg, npm, nextRelease));
t.true(githubRelease.calledOnce);
t.true(githubRelease.calledWithExactly(pkg, notes, nextRelease.version, options));
// Verify plugins have been called with expected parameters
t.true(verifyConditions.calledOnce);
t.true(verifyConditions.calledWithExactly({env, options, pkg, npm, logger}));
t.true(getLastRelease.calledOnce);
t.true(getLastRelease.calledWithExactly({env, options, pkg, npm, logger}));
t.true(analyzeCommits.calledOnce);
t.true(analyzeCommits.calledWithExactly({env, options, pkg, npm, logger, lastRelease: commitsLastRelease, commits}));
t.true(verifyRelease.calledOnce);
t.true(
verifyRelease.calledWithExactly({
env,
options,
pkg,
npm,
logger,
lastRelease: commitsLastRelease,
commits,
nextRelease,
})
);
t.true(generateNotes.calledOnce);
t.true(
generateNotes.calledWithExactly({
env,
options,
pkg,
npm,
logger,
lastRelease: commitsLastRelease,
commits,
nextRelease,
})
);
});
test('Dry-run skips verifyAuth, verifyConditions, publishNpm and githubRelease', async t => {
const env = {NPM_TOKEN: 'NPM_TOKEN'};
const pkgOptions = {branch: 'master'};
const cliOptions = {githubToken: 'GH_TOKEN', dryRun: true};
const options = Object.assign({}, pkgOptions, cliOptions);
const pkg = {name: 'available', release: options, repository: {url: 'http://github.com/whats/up.git'}};
const npm = {registry: 'http://test.registry.com'};
const lastRelease = {version: '1.0.0', gitHead: 'test_commit_head'};
const commitsLastRelease = {version: '1.0.0', gitHead: 'tag_head'};
const commits = [{hash: '1', message: 'fix: First fix'}, {hash: '2', message: 'feat: First feature'}];
const nextRelease = {type: 'major', version: '2.0.0'};
const notes = 'Release notes';
// Stub modules
const log = stub();
const error = stub();
const logger = {log, error};
const verifyAuth = stub().returns();
const publishNpm = stub().resolves();
const githubRelease = stub().resolves();
const getCommits = stub().resolves({commits, lastRelease: commitsLastRelease});
const getNextVersion = stub().returns(nextRelease.version);
// Stub plugins
const verifyConditions = stub().resolves();
const getLastRelease = stub().resolves(lastRelease);
const analyzeCommits = stub().resolves(nextRelease.type);
const verifyRelease = stub().resolves();
const generateNotes = stub().resolves(notes);
const getConfig = stub().resolves({
plugins: {getLastRelease, analyzeCommits, verifyRelease, verifyConditions, generateNotes},
env,
options,
pkg,
npm,
});
const semanticRelease = proxyquire('../src/index', {
'./lib/logger': logger,
'./lib/verify-auth': verifyAuth,
'./lib/get-config': getConfig,
'./lib/get-commits': getCommits,
'./lib/publish-npm': publishNpm,
'./lib/github-release': githubRelease,
'./lib/get-next-version': getNextVersion,
});
// Call the index module
await semanticRelease(cliOptions);
// Verify that publishNpm, githubRelease, verifyAuth, verifyConditions have not been called in a dry run
t.true(publishNpm.notCalled);
t.true(githubRelease.notCalled);
t.true(verifyAuth.notCalled);
t.true(verifyConditions.notCalled);
// Verify the release notes are logged
t.true(consoleLog.calledWithMatch(notes));
// Verify the sub-modules have been called with expected parameters
t.true(getConfig.calledOnce);
t.true(getConfig.calledWithExactly(cliOptions));
// Verify plugins have been called with expected parameters
t.true(getLastRelease.calledOnce);
t.true(getLastRelease.calledWithExactly({env, options, pkg, npm, logger}));
t.true(analyzeCommits.calledOnce);
t.true(analyzeCommits.calledWithExactly({env, options, pkg, npm, logger, lastRelease: commitsLastRelease, commits}));
t.true(verifyRelease.calledOnce);
t.true(
verifyRelease.calledWithExactly({
env,
options,
pkg,
npm,
logger,
lastRelease: commitsLastRelease,
commits,
nextRelease,
})
);
t.true(generateNotes.calledOnce);
t.true(
generateNotes.calledWithExactly({
env,
options,
pkg,
npm,
logger,
lastRelease: commitsLastRelease,
commits,
nextRelease,
})
);
});
test('Throw SemanticReleaseError if there is no release to be done', async t => {
const env = {NPM_TOKEN: 'NPM_TOKEN'};
const pkgOptions = {branch: 'master'};
const cliOptions = {githubToken: 'GH_TOKEN'};
const options = Object.assign({}, pkgOptions, cliOptions);
const pkg = {name: 'available', release: options, repository: {url: 'http://github.com/whats/up.git'}};
const npm = {registry: 'http://test.registry.com'};
const lastRelease = {version: '1.0.0', gitHead: 'test_commit_head'};
const commitsLastRelease = {version: '1.0.0', gitHead: 'tag_head'};
const commits = [{hash: '1', message: 'fix: First fix'}, {hash: '2', message: 'feat: First feature'}];
const nextRelease = {type: undefined};
// Stub modules
const log = stub();
const error = stub();
const logger = {log, error};
const verifyAuth = stub().returns();
const publishNpm = stub().resolves();
const githubRelease = stub().resolves();
const getCommits = stub().resolves({commits, lastRelease: commitsLastRelease});
const getNextVersion = stub().returns(null);
// Stub plugins
const verifyConditions = stub().resolves();
const getLastRelease = stub().resolves(lastRelease);
const analyzeCommits = stub().resolves(nextRelease.type);
const verifyRelease = stub().resolves();
const generateNotes = stub().resolves();
const getConfig = stub().resolves({
plugins: {getLastRelease, analyzeCommits, verifyRelease, verifyConditions, generateNotes},
env,
options,
pkg,
npm,
});
const semanticRelease = proxyquire('../src/index', {
'./lib/logger': logger,
'./lib/verify-auth': verifyAuth,
'./lib/get-config': getConfig,
'./lib/get-commits': getCommits,
'./lib/publish-npm': publishNpm,
'./lib/github-release': githubRelease,
'./lib/get-next-version': getNextVersion,
});
// Call the index module
const err = await t.throws(semanticRelease(cliOptions));
// Verify error code and type
t.is(err.code, 'ENOCHANGE');
t.true(err instanceof SemanticReleaseError);
// Verify the sub-modules have been called with expected parameters
t.true(getConfig.calledOnce);
t.true(getConfig.calledWithExactly(cliOptions));
t.true(verifyAuth.calledOnce);
t.true(verifyAuth.calledWithExactly(options, env));
// Verify plugins have been called with expected parameters
t.true(verifyConditions.calledOnce);
t.true(verifyConditions.calledWithExactly({env, options, pkg, npm, logger}));
t.true(getLastRelease.calledOnce);
t.true(getLastRelease.calledWithExactly({env, options, pkg, npm, logger}));
t.true(analyzeCommits.calledOnce);
t.true(analyzeCommits.calledWithExactly({env, options, pkg, npm, logger, lastRelease: commitsLastRelease, commits}));
// Verify that verifyRelease, publishNpm, generateNotes, githubRelease have not been called when no release is done
t.true(verifyRelease.notCalled);
t.true(generateNotes.notCalled);
t.true(publishNpm.notCalled);
t.true(githubRelease.notCalled);
});