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 52856b0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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(branch => `\`${branch}\``).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 52856b0

Please sign in to comment.