Skip to content

Commit

Permalink
Add tests for dash-cased and camel-cased args
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Sep 3, 2019
1 parent 3d28d41 commit 47c58ae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ The above commands will start an interactive prompt. You can use the `arrow keys
| --pr-description | Pull request description suffix | | string |
| --pr-title | Pull request title pattern | | string |
| --pr | Pull request to backport | | number |
| --resetAuthor | Set yourself as commit author | | boolean |
| --reset-author | Set yourself as commit author | | boolean |
| --sha | Sha of commit to backport | | string |
| --upstream | Name of organization and repository | | string |
| --username | Github username | | string |
Expand Down
34 changes: 28 additions & 6 deletions src/options/cliArgs.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getOptionsFromCliArgs } from './cliArgs';
import { OptionsFromConfigFiles } from './config/config';

describe('getOptionsFromCliArgs', () => {
let res: ReturnType<typeof getOptionsFromCliArgs>;

beforeEach(async () => {
it('should return correct options', () => {
const configOptions = {
accessToken: 'myAccessToken',
all: false,
Expand Down Expand Up @@ -32,10 +31,8 @@ describe('getOptionsFromCliArgs', () => {
'sqren'
];

res = getOptionsFromCliArgs(configOptions, argv);
});
const res = getOptionsFromCliArgs(configOptions, argv);

it('should return correct options', () => {
expect(res).toEqual({
accessToken: 'myAccessToken',
all: true,
Expand All @@ -55,4 +52,29 @@ describe('getOptionsFromCliArgs', () => {
username: 'sqren'
});
});

it('should accept both camel-case and dashed-case and convert them to camel cased', () => {
const configOptions = {} as OptionsFromConfigFiles;
const argv = [
'--access-token',
'my access token',
'--apiHostname',
'my api hostname'
];

const res = getOptionsFromCliArgs(configOptions, argv);

expect(res.accessToken).toEqual('my access token');
expect(res.apiHostname).toEqual('my api hostname');
});

it('should accept aliases (--pr) but only return the full name (--pullNumber) in the result', () => {
const configOptions = {} as OptionsFromConfigFiles;
const argv = ['--pr', '1337'];

const res = getOptionsFromCliArgs(configOptions, argv);

expect(res.pullNumber).toEqual(1337);
expect('pr' in res).toEqual(false);
});
});
29 changes: 9 additions & 20 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export function getOptionsFromCliArgs(
argv: string[]
) {
const cliArgs = yargs(argv)
.parserConfiguration({
'strip-dashed': true,
'strip-aliased': true
})
.usage('$0 [args]')
.wrap(Math.max(100, Math.min(120, yargs.terminalWidth())))
.option('accessToken', {
Expand Down Expand Up @@ -121,28 +125,13 @@ export function getOptionsFromCliArgs(
.version()
.help().argv;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { $0, _, ...rest } = cliArgs;

return {
accessToken: cliArgs.accessToken,
all: cliArgs.all,
apiHostname: cliArgs.apiHostname,
author: cliArgs.author,
commitsCount: cliArgs.commitsCount,
...rest,
branchChoices: configOptions.branchChoices,
branches: cliArgs.branches,
editor: cliArgs.editor,
fork: cliArgs.fork,
gitHostname: cliArgs.gitHostname,
labels: cliArgs.labels,
multiple: cliArgs.multiple,
multipleBranches: cliArgs.multipleBranches || cliArgs.multiple,
multipleCommits: cliArgs.multipleCommits || cliArgs.multiple,
path: cliArgs.path,
prTitle: cliArgs.prTitle,
prDescription: cliArgs.prDescription,
pullNumber: cliArgs.pullNumber,
resetAuthor: cliArgs.resetAuthor,
sha: cliArgs.sha,
upstream: cliArgs.upstream,
username: cliArgs.username
multipleCommits: cliArgs.multipleCommits || cliArgs.multiple
};
}

0 comments on commit 47c58ae

Please sign in to comment.