feat: pass cwd and env context to plugins

- Allow to run semantic-release (via API) from anywhere passing the current working directory.
- Allows to simplify the tests and to run them in parallel in both the core and plugins.
This commit is contained in:
Pierre Vanduynslager
2018-07-17 00:42:04 -04:00
parent 12e4155cd3
commit a94e08de9a
32 changed files with 1361 additions and 1332 deletions
+199 -246
View File
@@ -1,7 +1,6 @@
import test from 'ava';
import proxyquire from 'proxyquire';
import {spy, stub} from 'sinon';
import clearModule from 'clear-module';
import AggregateError from 'aggregate-error';
import SemanticReleaseError from '@semantic-release/error';
import {COMMIT_NAME, COMMIT_EMAIL} from '../lib/definitions/constants';
@@ -16,51 +15,31 @@ import {
gitShallowClone,
} from './helpers/git-utils';
// Save the current process.env
const envBackup = Object.assign({}, process.env);
// Save the current working diretory
const cwd = process.cwd();
const requireNoCache = proxyquire.noPreserveCache();
const pluginNoop = require.resolve('./fixtures/plugin-noop');
test.beforeEach(t => {
clearModule('../lib/hide-sensitive');
// Delete environment variables that could have been set on the machine running the tests
delete process.env.GIT_CREDENTIALS;
delete process.env.GH_TOKEN;
delete process.env.GITHUB_TOKEN;
delete process.env.GL_TOKEN;
delete process.env.GITLAB_TOKEN;
// Stub the logger functions
t.context.log = spy();
t.context.error = spy();
t.context.logger = {log: t.context.log, error: t.context.error};
t.context.stdout = stub(process.stdout, 'write');
t.context.stderr = stub(process.stderr, 'write');
t.context.stdout = spy();
t.context.stderr = spy();
t.context.logger = {log: t.context.log, error: t.context.error, stdout: t.context.stdout, stderr: t.context.stderr};
});
test.afterEach.always(t => {
// Restore process.env
process.env = envBackup;
// Restore the current working directory
process.chdir(cwd);
t.context.stdout.restore();
t.context.stderr.restore();
});
test.serial('Plugins are called with expected values', async t => {
test('Plugins are called with expected values', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
let commits = await gitCommits(['First']);
let commits = await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
commits = (await gitCommits(['Second'])).concat(commits);
await gitPush();
commits = (await gitCommits(['Second'], {cwd})).concat(commits);
await gitPush(repositoryUrl, 'master', {cwd});
const lastRelease = {version: '1.0.0', gitHead: commits[commits.length - 1].hash, gitTag: 'v1.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead(), gitTag: 'v2.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'v2.0.0'};
const notes1 = 'Release notes 1';
const notes2 = 'Release notes 2';
const notes3 = 'Release notes 3';
@@ -75,7 +54,7 @@ test.serial('Plugins are called with expected values', async t => {
const prepare = stub().resolves();
const publish1 = stub().resolves(release1);
const success = stub().resolves();
const env = {...process.env};
const config = {branch: 'master', repositoryUrl, globalOpt: 'global', tagFormat: `v\${version}`};
const options = {
...config,
@@ -88,17 +67,22 @@ test.serial('Plugins are called with expected values', async t => {
success,
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
t.truthy(await semanticRelease(options));
t.truthy(await semanticRelease(options, {cwd, extendEnv: false, env}));
t.is(verifyConditions1.callCount, 1);
t.deepEqual(verifyConditions1.args[0][0], config);
t.deepEqual(verifyConditions1.args[0][1], {options, logger: t.context.logger});
t.deepEqual(verifyConditions1.args[0][1].cwd, cwd);
t.deepEqual(verifyConditions1.args[0][1].options, options);
t.deepEqual(verifyConditions1.args[0][1].logger, t.context.logger);
t.is(verifyConditions2.callCount, 1);
t.deepEqual(verifyConditions2.args[0][1], {options, logger: t.context.logger});
t.deepEqual(verifyConditions2.args[0][0], config);
t.deepEqual(verifyConditions2.args[0][1].cwd, cwd);
t.deepEqual(verifyConditions2.args[0][1].options, options);
t.deepEqual(verifyConditions2.args[0][1].logger, t.context.logger);
t.is(analyzeCommits.callCount, 1);
t.deepEqual(analyzeCommits.args[0][0], config);
@@ -176,24 +160,24 @@ test.serial('Plugins are called with expected values', async t => {
]);
// Verify the tag has been created on the local and remote repo and reference the gitHead
t.is(await gitTagHead(nextRelease.gitTag), nextRelease.gitHead);
t.is(await gitRemoteTagHead(repositoryUrl, nextRelease.gitTag), nextRelease.gitHead);
t.is(await gitTagHead(nextRelease.gitTag, {cwd}), nextRelease.gitHead);
t.is(await gitRemoteTagHead(repositoryUrl, nextRelease.gitTag, {cwd}), nextRelease.gitHead);
// Verify the author/commiter name and email hve been set
t.is(process.env.GIT_AUTHOR_NAME, COMMIT_NAME);
t.is(process.env.GIT_AUTHOR_EMAIL, COMMIT_EMAIL);
t.is(process.env.GIT_COMMITTER_NAME, COMMIT_NAME);
t.is(process.env.GIT_COMMITTER_EMAIL, COMMIT_EMAIL);
// Verify the author/commiter name and email have been set
t.is(env.GIT_AUTHOR_NAME, COMMIT_NAME);
t.is(env.GIT_AUTHOR_EMAIL, COMMIT_EMAIL);
t.is(env.GIT_COMMITTER_NAME, COMMIT_NAME);
t.is(env.GIT_COMMITTER_EMAIL, COMMIT_EMAIL);
});
test.serial('Use custom tag format', async t => {
const repositoryUrl = await gitRepo(true);
await gitCommits(['First']);
await gitTagVersion('test-1.0.0');
await gitCommits(['Second']);
await gitPush();
test('Use custom tag format', async t => {
const {cwd, repositoryUrl} = await gitRepo(true);
await gitCommits(['First'], {cwd});
await gitTagVersion('test-1.0.0', undefined, {cwd});
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead(), gitTag: 'test-2.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'test-2.0.0'};
const notes = 'Release notes';
const config = {branch: 'master', repositoryUrl, globalOpt: 'global', tagFormat: `test-\${version}`};
const options = {
@@ -208,38 +192,37 @@ test.serial('Use custom tag format', async t => {
fail: stub().resolves(),
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
t.truthy(await semanticRelease(options));
t.truthy(await semanticRelease(options, {cwd, env: {}}));
// Verify the tag has been created on the local and remote repo and reference the gitHead
t.is(await gitTagHead(nextRelease.gitTag), nextRelease.gitHead);
t.is(await gitRemoteTagHead(repositoryUrl, nextRelease.gitTag), nextRelease.gitHead);
t.is(await gitTagHead(nextRelease.gitTag, {cwd}), nextRelease.gitHead);
t.is(await gitRemoteTagHead(repositoryUrl, nextRelease.gitTag, {cwd}), nextRelease.gitHead);
});
test.serial('Use new gitHead, and recreate release notes if a prepare plugin create a commit', async t => {
test('Use new gitHead, and recreate release notes if a prepare plugin create a commit', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
let commits = await gitCommits(['First']);
let commits = await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
commits = (await gitCommits(['Second'])).concat(commits);
await gitPush();
commits = (await gitCommits(['Second'], {cwd})).concat(commits);
await gitPush(repositoryUrl, 'master', {cwd});
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead(), gitTag: 'v2.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'v2.0.0'};
const notes = 'Release notes';
const generateNotes = stub().resolves(notes);
const prepare1 = stub().callsFake(async () => {
commits = (await gitCommits(['Third'])).concat(commits);
commits = (await gitCommits(['Third'], {cwd})).concat(commits);
});
const prepare2 = stub().resolves();
const publish = stub().resolves();
const options = {
branch: 'master',
repositoryUrl,
@@ -253,19 +236,19 @@ test.serial('Use new gitHead, and recreate release notes if a prepare plugin cre
fail: stub().resolves(),
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
t.truthy(await semanticRelease(options));
t.truthy(await semanticRelease(options, {cwd, env: {}}));
t.is(generateNotes.callCount, 2);
t.deepEqual(generateNotes.args[0][1].nextRelease, nextRelease);
t.is(prepare1.callCount, 1);
t.deepEqual(prepare1.args[0][1].nextRelease, {...nextRelease, notes});
nextRelease.gitHead = await getGitHead();
nextRelease.gitHead = await getGitHead({cwd});
t.deepEqual(generateNotes.args[1][1].nextRelease, {...nextRelease, notes});
t.is(prepare2.callCount, 1);
@@ -275,22 +258,22 @@ test.serial('Use new gitHead, and recreate release notes if a prepare plugin cre
t.deepEqual(publish.args[0][1].nextRelease, {...nextRelease, notes});
// Verify the tag has been created on the local and remote repo and reference the last gitHead
t.is(await gitTagHead(nextRelease.gitTag), commits[0].hash);
t.is(await gitRemoteTagHead(repositoryUrl, nextRelease.gitTag), commits[0].hash);
t.is(await gitTagHead(nextRelease.gitTag, {cwd}), commits[0].hash);
t.is(await gitRemoteTagHead(repositoryUrl, nextRelease.gitTag, {cwd}), commits[0].hash);
});
test.serial('Call all "success" plugins even if one errors out', async t => {
test('Call all "success" plugins even if one errors out', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
await gitCommits(['First']);
await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
await gitCommits(['Second']);
await gitPush();
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead(), gitTag: 'v2.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'v2.0.0'};
const notes = 'Release notes';
const verifyConditions1 = stub().resolves();
const verifyConditions2 = stub().resolves();
@@ -300,7 +283,6 @@ test.serial('Call all "success" plugins even if one errors out', async t => {
const publish = stub().resolves(release);
const success1 = stub().rejects();
const success2 = stub().resolves();
const config = {branch: 'master', repositoryUrl, globalOpt: 'global', tagFormat: `v\${version}`};
const options = {
...config,
@@ -312,27 +294,26 @@ test.serial('Call all "success" plugins even if one errors out', async t => {
success: [success1, success2],
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
await t.throws(semanticRelease(options));
await t.throws(semanticRelease(options, {cwd, env: {}}));
t.is(success1.callCount, 1);
t.deepEqual(success1.args[0][0], config);
t.deepEqual(success1.args[0][1].releases, [{...release, ...nextRelease, notes, pluginName: '[Function: proxy]'}]);
t.is(success2.callCount, 1);
t.deepEqual(success2.args[0][1].releases, [{...release, ...nextRelease, notes, pluginName: '[Function: proxy]'}]);
});
test.serial('Log all "verifyConditions" errors', async t => {
test('Log all "verifyConditions" errors', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
await gitCommits(['First']);
await gitPush();
await gitCommits(['First'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const error1 = new Error('error 1');
const error2 = new SemanticReleaseError('error 2', 'ERR2');
@@ -345,11 +326,11 @@ test.serial('Log all "verifyConditions" errors', async t => {
fail,
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
const errors = [...(await t.throws(semanticRelease(options)))];
const errors = [...(await t.throws(semanticRelease(options, {cwd, env: {}})))];
t.deepEqual(errors, [error1, error2, error3]);
t.deepEqual(t.context.log.args[t.context.log.args.length - 2], ['%s error 2', 'ERR2']);
@@ -366,16 +347,16 @@ test.serial('Log all "verifyConditions" errors', async t => {
t.deepEqual(fail.args[0][1].errors, [error2, error3]);
});
test.serial('Log all "verifyRelease" errors', async t => {
test('Log all "verifyRelease" errors', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
await gitCommits(['First']);
await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
await gitCommits(['Second']);
await gitPush();
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const error1 = new SemanticReleaseError('error 1', 'ERR1');
const error2 = new SemanticReleaseError('error 2', 'ERR2');
@@ -389,11 +370,11 @@ test.serial('Log all "verifyRelease" errors', async t => {
fail,
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
const errors = [...(await t.throws(semanticRelease(options)))];
const errors = [...(await t.throws(semanticRelease(options, {cwd, env: {}})))];
t.deepEqual(errors, [error1, error2]);
t.deepEqual(t.context.log.args[t.context.log.args.length - 2], ['%s error 1', 'ERR1']);
@@ -403,18 +384,18 @@ test.serial('Log all "verifyRelease" errors', async t => {
t.deepEqual(fail.args[0][1].errors, [error1, error2]);
});
test.serial('Dry-run skips publish and success', async t => {
test('Dry-run skips publish and success', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
await gitCommits(['First']);
await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
await gitCommits(['Second']);
await gitPush();
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead(), gitTag: 'v2.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'v2.0.0'};
const notes = 'Release notes';
const verifyConditions = stub().resolves();
@@ -437,11 +418,11 @@ test.serial('Dry-run skips publish and success', async t => {
success,
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
t.truthy(await semanticRelease(options));
t.truthy(await semanticRelease(options, {cwd, env: {}}));
t.not(t.context.log.args[0][0], 'This run was not triggered in a known CI environment, running in dry-run mode.');
t.is(verifyConditions.callCount, 1);
@@ -452,16 +433,16 @@ test.serial('Dry-run skips publish and success', async t => {
t.is(success.callCount, 0);
});
test.serial('Dry-run skips fail', async t => {
test('Dry-run skips fail', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
await gitCommits(['First']);
await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
await gitCommits(['Second']);
await gitPush();
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const error1 = new SemanticReleaseError('error 1', 'ERR1');
const error2 = new SemanticReleaseError('error 2', 'ERR2');
@@ -475,11 +456,11 @@ test.serial('Dry-run skips fail', async t => {
fail,
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
const errors = [...(await t.throws(semanticRelease(options)))];
const errors = [...(await t.throws(semanticRelease(options, {cwd, env: {}})))];
t.deepEqual(errors, [error1, error2]);
t.deepEqual(t.context.log.args[t.context.log.args.length - 2], ['%s error 1', 'ERR1']);
@@ -487,18 +468,18 @@ test.serial('Dry-run skips fail', async t => {
t.is(fail.callCount, 0);
});
test.serial('Force a dry-run if not on a CI and "noCi" is not explicitly set', async t => {
test('Force a dry-run if not on a CI and "noCi" is not explicitly set', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
await gitCommits(['First']);
await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
await gitCommits(['Second']);
await gitPush();
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead(), gitTag: 'v2.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'v2.0.0'};
const notes = 'Release notes';
const verifyConditions = stub().resolves();
@@ -522,11 +503,11 @@ test.serial('Force a dry-run if not on a CI and "noCi" is not explicitly set', a
fail: stub().resolves(),
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: false, branch: 'master'}),
});
t.truthy(await semanticRelease(options));
t.truthy(await semanticRelease(options, {cwd, env: {}}));
t.is(t.context.log.args[1][0], 'This run was not triggered in a known CI environment, running in dry-run mode.');
t.is(verifyConditions.callCount, 1);
@@ -539,16 +520,16 @@ test.serial('Force a dry-run if not on a CI and "noCi" is not explicitly set', a
test.serial('Dry-run does not print changelog if "generateNotes" return "undefined"', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
await gitCommits(['First']);
await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
await gitCommits(['Second']);
await gitPush();
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead(), gitTag: 'v2.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'v2.0.0'};
const analyzeCommits = stub().resolves(nextRelease.type);
const generateNotes = stub().resolves();
@@ -565,27 +546,27 @@ test.serial('Dry-run does not print changelog if "generateNotes" return "undefin
success: false,
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
t.truthy(await semanticRelease(options));
t.truthy(await semanticRelease(options, {cwd, env: {}}));
t.deepEqual(t.context.log.args[t.context.log.args.length - 1], ['Release note for version %s:\n', '2.0.0']);
});
test.serial('Allow local releases with "noCi" option', async t => {
test('Allow local releases with "noCi" option', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
await gitCommits(['First']);
await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
await gitCommits(['Second']);
await gitPush();
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead(), gitTag: 'v2.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'v2.0.0'};
const notes = 'Release notes';
const verifyConditions = stub().resolves();
@@ -609,11 +590,11 @@ test.serial('Allow local releases with "noCi" option', async t => {
fail: stub().resolves(),
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: false, branch: 'master', isPr: true}),
});
t.truthy(await semanticRelease(options));
t.truthy(await semanticRelease(options, {cwd, env: {}}));
t.not(t.context.log.args[0][0], 'This run was not triggered in a known CI environment, running in dry-run mode.');
t.not(
@@ -628,19 +609,19 @@ test.serial('Allow local releases with "noCi" option', async t => {
t.is(success.callCount, 1);
});
test.serial('Accept "undefined" value returned by the "generateNotes" plugins', async t => {
test('Accept "undefined" value returned by the "generateNotes" plugins', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
let commits = await gitCommits(['First']);
let commits = await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
commits = (await gitCommits(['Second'])).concat(commits);
await gitPush();
commits = (await gitCommits(['Second'], {cwd})).concat(commits);
await gitPush(repositoryUrl, 'master', {cwd});
const lastRelease = {version: '1.0.0', gitHead: commits[commits.length - 1].hash, gitTag: 'v1.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead(), gitTag: 'v2.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'v2.0.0'};
const analyzeCommits = stub().resolves(nextRelease.type);
const verifyRelease = stub().resolves();
const generateNotes1 = stub().resolves();
@@ -661,11 +642,11 @@ test.serial('Accept "undefined" value returned by the "generateNotes" plugins',
fail: stub().resolves(),
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
t.truthy(await semanticRelease(options));
t.truthy(await semanticRelease(options, {cwd, env: {}}));
t.is(analyzeCommits.callCount, 1);
t.deepEqual(analyzeCommits.args[0][1].lastRelease, lastRelease);
@@ -684,50 +665,49 @@ test.serial('Accept "undefined" value returned by the "generateNotes" plugins',
t.is(publish.args[0][1].nextRelease.notes, notes2);
});
test.serial('Returns falsy value if triggered by a PR', async t => {
test('Returns falsy value if triggered by a PR', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: true}),
});
t.falsy(await semanticRelease({repositoryUrl}));
t.falsy(await semanticRelease({cwd, repositoryUrl}, {cwd, env: {}}));
t.is(
t.context.log.args[t.context.log.args.length - 1][0],
"This run was triggered by a pull request and therefore a new version won't be published."
);
});
test.serial('Returns falsy value if triggered on an outdated clone', async t => {
test('Returns falsy value if triggered on an outdated clone', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const repoDir = process.cwd();
let {cwd, repositoryUrl} = await gitRepo(true);
const repoDir = cwd;
// Add commits to the master branch
await gitCommits(['First']);
await gitCommits(['Second']);
await gitPush();
await gitShallowClone(repositoryUrl);
await gitCommits(['Third']);
await gitPush();
process.chdir(repoDir);
await gitCommits(['First'], {cwd});
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
cwd = await gitShallowClone(repositoryUrl);
await gitCommits(['Third'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
t.falsy(await semanticRelease({repositoryUrl}));
t.falsy(await semanticRelease({repositoryUrl}, {cwd: repoDir, env: {}}));
t.deepEqual(t.context.log.args[t.context.log.args.length - 1], [
"The local branch %s is behind the remote one, therefore a new version won't be published.",
'master',
]);
});
test.serial('Returns falsy value if not running from the configured branch', async t => {
test('Returns falsy value if not running from the configured branch', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
const options = {
branch: 'master',
repositoryUrl,
@@ -741,24 +721,24 @@ test.serial('Returns falsy value if not running from the configured branch', asy
fail: stub().resolves(),
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'other-branch', isPr: false}),
});
t.falsy(await semanticRelease(options));
t.falsy(await semanticRelease(options, {cwd, env: {}}));
t.is(
t.context.log.args[1][0],
'This test run was triggered on the branch other-branch, while semantic-release is configured to only publish from master, therefore a new version wont be published.'
);
});
test.serial('Returns falsy value if there is no relevant changes', async t => {
test('Returns falsy value if there is no relevant changes', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
await gitCommits(['First']);
await gitPush();
await gitCommits(['First'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const analyzeCommits = stub().resolves();
const verifyRelease = stub().resolves();
@@ -778,12 +758,12 @@ test.serial('Returns falsy value if there is no relevant changes', async t => {
fail: stub().resolves(),
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
t.falsy(await semanticRelease(options));
t.falsy(await semanticRelease(options, {cwd, env: {}}));
t.is(analyzeCommits.callCount, 1);
t.is(verifyRelease.callCount, 0);
t.is(generateNotes.callCount, 0);
@@ -794,21 +774,24 @@ test.serial('Returns falsy value if there is no relevant changes', async t => {
);
});
test.serial('Exclude commits with [skip release] or [release skip] from analysis', async t => {
test('Exclude commits with [skip release] or [release skip] from analysis', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
const commits = await gitCommits([
'Test commit',
'Test commit [skip release]',
'Test commit [release skip]',
'Test commit [Release Skip]',
'Test commit [Skip Release]',
'Test commit [skip release]',
'Test commit\n\n commit body\n[skip release]',
'Test commit\n\n commit body\n[release skip]',
]);
await gitPush();
const commits = await gitCommits(
[
'Test commit',
'Test commit [skip release]',
'Test commit [release skip]',
'Test commit [Release Skip]',
'Test commit [Skip Release]',
'Test commit [skip release]',
'Test commit\n\n commit body\n[skip release]',
'Test commit\n\n commit body\n[release skip]',
],
{cwd}
);
await gitPush(repositoryUrl, 'master', {cwd});
const analyzeCommits = stub().resolves();
const config = {branch: 'master', repositoryUrl, globalOpt: 'global'};
const options = {
@@ -823,47 +806,19 @@ test.serial('Exclude commits with [skip release] or [release skip] from analysis
fail: stub().resolves(),
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
await semanticRelease(options);
await semanticRelease(options, {cwd, env: {}});
t.is(analyzeCommits.callCount, 1);
t.is(analyzeCommits.args[0][1].commits.length, 2);
t.deepEqual(analyzeCommits.args[0][1].commits[0], commits[commits.length - 1]);
});
test.serial('Hide sensitive environment variable values from the logs', async t => {
process.env.MY_TOKEN = 'secret token';
const repositoryUrl = await gitRepo(true);
const options = {
branch: 'master',
repositoryUrl,
verifyConditions: async (pluginConfig, {logger}) => {
console.log(`Console: The token ${process.env.MY_TOKEN} is invalid`);
logger.log(`Log: The token ${process.env.MY_TOKEN} is invalid`);
logger.error(`Error: The token ${process.env.MY_TOKEN} is invalid`);
throw new Error(`Invalid token ${process.env.MY_TOKEN}`);
},
};
const semanticRelease = proxyquire('..', {
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
await t.throws(semanticRelease(options));
t.regex(t.context.stdout.args[t.context.stdout.args.length - 2][0], /Console: The token \[secure\] is invalid/);
t.regex(t.context.stdout.args[t.context.stdout.args.length - 1][0], /Log: The token \[secure\] is invalid/);
t.regex(t.context.stderr.args[0][0], /Error: The token \[secure\] is invalid/);
t.regex(t.context.stderr.args[1][0], /Invalid token \[secure\]/);
});
test.serial('Log both plugins errors and errors thrown by "fail" plugin', async t => {
process.env.MY_TOKEN = 'secret token';
const repositoryUrl = await gitRepo(true);
test('Log both plugins errors and errors thrown by "fail" plugin', async t => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginError = new SemanticReleaseError('Plugin error', 'ERR');
const failError1 = new Error('Fail error 1');
const failError2 = new Error('Fail error 2');
@@ -874,21 +829,20 @@ test.serial('Log both plugins errors and errors thrown by "fail" plugin', async
verifyConditions: stub().rejects(pluginError),
fail: [stub().rejects(failError1), stub().rejects(failError2)],
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
await t.throws(semanticRelease(options));
await t.throws(semanticRelease(options, {cwd, env: {}}));
t.is(t.context.error.args[t.context.error.args.length - 2][1], failError1);
t.is(t.context.error.args[t.context.error.args.length - 1][1], failError2);
t.deepEqual(t.context.log.args[t.context.log.args.length - 1], ['%s Plugin error', 'ERR']);
});
test.serial('Call "fail" only if a plugin returns a SemanticReleaseError', async t => {
process.env.MY_TOKEN = 'secret token';
const repositoryUrl = await gitRepo(true);
test('Call "fail" only if a plugin returns a SemanticReleaseError', async t => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginError = new Error('Plugin error');
const fail = stub().resolves();
@@ -898,42 +852,42 @@ test.serial('Call "fail" only if a plugin returns a SemanticReleaseError', async
verifyConditions: stub().rejects(pluginError),
fail,
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
await t.throws(semanticRelease(options));
await t.throws(semanticRelease(options, {cwd, env: {}}));
t.true(fail.notCalled);
t.is(t.context.error.args[t.context.error.args.length - 1][1], pluginError);
});
test.serial('Throw SemanticReleaseError if repositoryUrl is not set and cannot be found from repo config', async t => {
test('Throw SemanticReleaseError if repositoryUrl is not set and cannot be found from repo config', async t => {
// Create a git repository, set the current working directory at the root of the repo
await gitRepo();
const {cwd} = await gitRepo();
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
const errors = [...(await t.throws(semanticRelease()))];
const errors = [...(await t.throws(semanticRelease({}, {cwd, env: {}})))];
// Verify error code and type
t.is(errors[0].code, 'ENOREPOURL');
t.is(errors[0].name, 'SemanticReleaseError');
});
test.serial('Throw an Error if plugin returns an unexpected value', async t => {
test('Throw an Error if plugin returns an unexpected value', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repositoryUrl = await gitRepo(true);
const {cwd, repositoryUrl} = await gitRepo(true);
// Add commits to the master branch
await gitCommits(['First']);
await gitCommits(['First'], {cwd});
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
await gitTagVersion('v1.0.0', undefined, {cwd});
// Add new commits to the master branch
await gitCommits(['Second']);
await gitPush();
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
const verifyConditions = stub().resolves();
const analyzeCommits = stub().resolves('string');
@@ -947,24 +901,23 @@ test.serial('Throw an Error if plugin returns an unexpected value', async t => {
fail: stub().resolves(),
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
const error = await t.throws(semanticRelease(options), Error);
const error = await t.throws(semanticRelease(options, {cwd, env: {}}), Error);
t.regex(error.details, /string/);
});
test.serial('Get all commits including the ones not in the shallow clone', async t => {
const repositoryUrl = await gitRepo(true);
await gitTagVersion('v1.0.0');
await gitCommits(['First', 'Second', 'Third']);
await gitPush(repositoryUrl, 'master');
test('Get all commits including the ones not in the shallow clone', async t => {
let {cwd, repositoryUrl} = await gitRepo(true);
await gitTagVersion('v1.0.0', undefined, {cwd});
await gitCommits(['First', 'Second', 'Third'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
await gitShallowClone(repositoryUrl);
cwd = await gitShallowClone(repositoryUrl);
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead(), gitTag: 'v2.0.0'};
const nextRelease = {type: 'major', version: '2.0.0', gitHead: await getGitHead({cwd}), gitTag: 'v2.0.0'};
const notes = 'Release notes';
const analyzeCommits = stub().resolves(nextRelease.type);
@@ -981,11 +934,11 @@ test.serial('Get all commits including the ones not in the shallow clone', async
fail: stub().resolves(),
};
const semanticRelease = proxyquire('..', {
const semanticRelease = requireNoCache('..', {
'./lib/logger': t.context.logger,
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
});
t.truthy(await semanticRelease(options));
t.truthy(await semanticRelease(options, {cwd, env: {}}));
t.is(analyzeCommits.args[0][1].commits.length, 3);
});