Skip to content

Commit

Permalink
Merge pull request bump-sh#445 from paulRbr/add-breaking-flag
Browse files Browse the repository at this point in the history
diff: add a `--fail-on-breaking` flag to fail on breaking changes
  • Loading branch information
paulRbr authored Apr 13, 2023
2 parents 2d37c7d + 34ff97b commit e66cc02
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default class Diff extends Command {
branch: flags.branch(),
token: flags.token({ required: false }),
open: flags.open({ description: 'Open the visual diff in your browser' }),
'fail-on-breaking': flags.failOnBreaking(),
format: flags.format(),
expires: flags.expires(),
};
Expand Down Expand Up @@ -106,7 +107,12 @@ export default class Diff extends Command {
cli.action.stop();

if (diff) {
await this.displayCompareResult(diff, format, flags.open);
await this.displayCompareResult(
diff,
format,
flags.open,
flags['fail-on-breaking'],
);
} else {
await cli.log('No changes detected.');
}
Expand All @@ -118,6 +124,7 @@ export default class Diff extends Command {
result: DiffResponse,
format: string,
open: boolean,
failOnBreaking: boolean,
): Promise<void> {
if (format == 'text' && result.text) {
await cli.log(result.text);
Expand All @@ -134,5 +141,9 @@ export default class Diff extends Command {
if (open && result.public_url) {
await cli.open(result.public_url);
}

if (failOnBreaking && result.breaking) {
this.exit(1);
}
}
}
17 changes: 17 additions & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ const open = (options = {}): Parser.flags.IBooleanFlag<boolean> => {
});
};

const failOnBreaking = (options = {}): Parser.flags.IBooleanFlag<boolean> => {
return flags.boolean({
char: 'F',
description: 'Fail when diff contains a breaking change',
default: () => {
const envCi = process.env.CI;
if (envCi) {
return true;
} else {
return false;
}
},
...options,
});
};

const live = (options = {}): Parser.flags.IBooleanFlag<boolean> => {
return flags.boolean({
char: 'l',
Expand Down Expand Up @@ -108,6 +124,7 @@ export {
autoCreate,
dryRun,
open,
failOnBreaking,
live,
format,
expires,
Expand Down
33 changes: 33 additions & 0 deletions test/commands/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,39 @@ describe('diff subcommand', () => {
},
);

base
.env({ CI: '1' })
.nock('https://bump.sh', (api) => {
api
.post('/api/v1/diffs')
.once()
.reply(201, {
id: '321abc',
public_url: 'http://localhost/preview/321abc',
})
.get('/api/v1/diffs/321abc?formats[]=text')
.once()
.reply(202)
.get('/api/v1/diffs/321abc?formats[]=text')
.once()
.reply(200, { diff_summary: 'Updated: POST /versions', diff_breaking: true });
})
.stdout()
.stderr()
.command([
'diff',
'examples/valid/openapi.v3.json',
'examples/valid/openapi.v2.json',
])
.exit(1)
.it(
'asks for a public diff between the two files to Bump and exit 1 due to breaking change',
async ({ stdout, stderr }) => {
expect(stderr).to.match(/Comparing the two given definition files/);
expect(stdout).to.contain('Updated: POST /versions');
},
);

test
.nock('https://bump.sh', (api) => {
api
Expand Down

0 comments on commit e66cc02

Please sign in to comment.