From 52856b06966898f7a6adc5e7aee3bc50e4e24e94 Mon Sep 17 00:00:00 2001 From: Munif Tanjim Date: Tue, 21 Jul 2020 20:17:21 +0600 Subject: [PATCH] Set default release branch to main or master --- readme.md | 2 +- source/git-util.js | 8 +++++--- test/git-tasks.js | 17 +++++++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 7f89ddd1..abf9b638 100644 --- a/readme.md +++ b/readme.md @@ -7,7 +7,7 @@ ## Why - [Interactive UI](#interactive-ui) -- Ensures you are publishing from your release branch (`master` by default) +- Ensures you are publishing from your release branch (`main` and `master` by default) - Ensures the working directory is clean and that there are no unpulled changes - Reinstalls dependencies to ensure your project works with the latest dependency tree - Ensures your Node.js and npm versions are supported by the project and its dependencies diff --git a/source/git-util.js b/source/git-util.js index c5118845..888b98de 100644 --- a/source/git-util.js +++ b/source/git-util.js @@ -38,9 +38,11 @@ exports.currentBranch = async () => { return stdout; }; -exports.verifyCurrentBranchIsReleaseBranch = async (releaseBranch = 'master') => { - if (await exports.currentBranch() !== releaseBranch) { - throw new Error(`Not on \`${releaseBranch}\` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.`); +exports.verifyCurrentBranchIsReleaseBranch = async releaseBranch => { + const allowedBranches = releaseBranch ? [releaseBranch] : ['main', 'master']; + const currentBranch = await exports.currentBranch(); + if (!allowedBranches.includes(currentBranch)) { + throw new Error(`Not on ${allowedBranches.map(branch => `\`${branch}\``).join('/')} branch. Use --any-branch to publish anyway, or set a different release branch using --branch.`); } }; diff --git a/test/git-tasks.js b/test/git-tasks.js index 6153af35..3fd73534 100644 --- a/test/git-tasks.js +++ b/test/git-tasks.js @@ -24,6 +24,19 @@ test.beforeEach(() => { execaStub.resetStub(); }); +test.serial('should fail when release branch is not specified, current branch is not main/master and publishing from any branch not permitted', async t => { + execaStub.createStub([ + { + command: 'git symbolic-ref --short HEAD', + exitCode: 0, + stdout: 'feature' + } + ]); + await t.throwsAsync(run(testedModule({})), + {message: 'Not on `main`/`master` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.'}); + t.true(SilentRenderer.tasks.some(task => task.title === 'Check current branch' && task.hasFailed())); +}); + test.serial('should fail when current branch is not the specified release branch and publishing from any branch not permitted', async t => { execaStub.createStub([ { @@ -32,8 +45,8 @@ test.serial('should fail when current branch is not the specified release branch stdout: 'feature' } ]); - await t.throwsAsync(run(testedModule({branch: 'main'})), - {message: 'Not on `main` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.'}); + await t.throwsAsync(run(testedModule({branch: 'release'})), + {message: 'Not on `release` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.'}); t.true(SilentRenderer.tasks.some(task => task.title === 'Check current branch' && task.hasFailed())); });