Skip to content

Commit

Permalink
test: added tests for illegal argument usage
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondill committed Feb 2, 2023
1 parent 834ea29 commit aaa38d1
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ test('run `cli --help` with `--version`', macro.testCLI, {
message: 'Should prioritize help over version.',
})

test('run `cli --help=value`', macro.testCLI, {
args: ['--help=value'],
message: 'Should report illegal argument and suggest help.',
})

test('run `cli --version=value`', macro.testCLI, {
args: ['--version=value'],
message: 'Should report illegal argument and suggest help.',
})

test('run `cli --unknown-option`', macro.testCLI, {
args: ['--unknown-option'],
message: 'Should report unknown option and suggest help.',
})

test('run `cli -u` with unknown option', macro.testCLI, {
args: ['-u'],
message: 'Should report unknown option and suggest help.',
})

test('run `cli` with no patterns', macro.testCLI, {
fixtures: [
{
Expand All @@ -87,6 +107,11 @@ test('run `cli --quiet` with no patterns', macro.testCLI, {
message: 'Should format package.json without message.',
})

test('run `cli --quiet=value`', macro.testCLI, {
args: ['--quiet=value'],
message: 'Should report illegal argument and suggest help.',
})

test('run `cli -q` with no patterns', macro.testCLI, {
fixtures: [
{
Expand All @@ -111,6 +136,11 @@ test('run `cli --check` with no patterns', macro.testCLI, {
message: 'Should not sort package.json',
})

test('run `cli --check=value`', macro.testCLI, {
args: ['--check=value'],
message: 'Should report illegal argument and suggest help.',
})

test('run `cli --check --quiet` with no patterns', macro.testCLI, {
fixtures: [
{
Expand Down Expand Up @@ -147,6 +177,18 @@ test('run `cli -c -q` with no patterns', macro.testCLI, {
message: 'Should support `-q` alias',
})

test('run `cli -cq` with no patterns', macro.testCLI, {
fixtures: [
{
file: 'package.json',
content: badJson,
expect: badJson,
},
],
args: ['-cq'],
message: 'Should support option aggregation',
})

test('run `cli` on 1 bad file', macro.testCLI, {
fixtures: [
{
Expand Down
136 changes: 136 additions & 0 deletions tests/snapshots/cli.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,78 @@ Generated by [AVA](https://avajs.dev).
},
}

## run `cli --help=value`

> Should report illegal argument and suggest help.
{
args: [
'--help=value',
],
fixtures: [],
result: {
errorCode: 2,
stderr: `Option '-h, --help' does not take an argument␊
Try 'sort-package-json --help' for more information.␊
`,
stdout: '',
},
}

## run `cli --version=value`

> Should report illegal argument and suggest help.
{
args: [
'--version=value',
],
fixtures: [],
result: {
errorCode: 2,
stderr: `Option '-v, --version' does not take an argument␊
Try 'sort-package-json --help' for more information.␊
`,
stdout: '',
},
}

## run `cli --unknown-option`

> Should report unknown option and suggest help.
{
args: [
'--unknown-option',
],
fixtures: [],
result: {
errorCode: 2,
stderr: `Unknown option '--unknown-option'. To specify a positional argument starting with a '-', place it at the end of the command after '--', as in '-- "--unknown-option"␊
Try 'sort-package-json --help' for more information.␊
`,
stdout: '',
},
}

## run `cli -u` with unknown option

> Should report unknown option and suggest help.
{
args: [
'-u',
],
fixtures: [],
result: {
errorCode: 2,
stderr: `Unknown option '-u'. To specify a positional argument starting with a '-', place it at the end of the command after '--', as in '-- "-u"␊
Try 'sort-package-json --help' for more information.␊
`,
stdout: '',
},
}

## run `cli` with no patterns

> Should format package.json.
Expand Down Expand Up @@ -265,6 +337,24 @@ Generated by [AVA](https://avajs.dev).
},
}

## run `cli --quiet=value`

> Should report illegal argument and suggest help.
{
args: [
'--quiet=value',
],
fixtures: [],
result: {
errorCode: 2,
stderr: `Option '-q, --quiet' does not take an argument␊
Try 'sort-package-json --help' for more information.␊
`,
stdout: '',
},
}

## run `cli -q` with no patterns

> Should support -q alias.
Expand Down Expand Up @@ -325,6 +415,24 @@ Generated by [AVA](https://avajs.dev).
},
}

## run `cli --check=value`

> Should report illegal argument and suggest help.
{
args: [
'--check=value',
],
fixtures: [],
result: {
errorCode: 2,
stderr: `Option '-c, --check' does not take an argument␊
Try 'sort-package-json --help' for more information.␊
`,
stdout: '',
},
}

## run `cli --check --quiet` with no patterns

> Should not sort package.json or report a message.
Expand Down Expand Up @@ -415,6 +523,34 @@ Generated by [AVA](https://avajs.dev).
},
}

## run `cli -cq` with no patterns

> Should support option aggregation
{
args: [
'-cq',
],
fixtures: [
{
expect: `{␊
"version": "1.0.0",␊
"name": "sort-package-json"␊
}`,
file: 'package.json',
original: `{␊
"version": "1.0.0",␊
"name": "sort-package-json"␊
}`,
},
],
result: {
errorCode: 1,
stderr: '',
stdout: '',
},
}

## run `cli` on 1 bad file

> Should format 1 file.
Expand Down
Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.

0 comments on commit aaa38d1

Please sign in to comment.