Skip to content

Commit

Permalink
Set default release branch to main or master
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim committed Jul 21, 2020
1 parent b4bef72 commit 81007df
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 5 additions & 3 deletions source/git-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(br => `\`${br}\``).join('/')} branch. Use --any-branch to publish anyway, or set a different release branch using --branch.`);
}
};

Expand Down
17 changes: 15 additions & 2 deletions test/git-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand All @@ -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()));
});

Expand Down

0 comments on commit 81007df

Please sign in to comment.