diff --git a/README.md b/README.md index 56a93314..8376d397 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,15 @@ $ sort-package-json "**/package.json" --check # 2 of 5 matched files are not sorted. ``` +#### `--quiet` flag + +In order to silence any successful output, you can run CLI with the `--quiet` flag (or `-q`). This will stop the CLI from outputting if it runs successfully, but will still display errors if they occur. Exit codes will not change. + +```bash +$ sort-package-json "**/package.json" --check --quiet +$ sort-package-json "**/package.json" --quiet +``` + ## API ### Install diff --git a/cli.js b/cli.js index 370afd26..c2369172 100755 --- a/cli.js +++ b/cli.js @@ -7,9 +7,15 @@ const isCheckFlag = (argument) => argument === '--check' || argument === '-c' const isHelpFlag = (argument) => argument === '--help' || argument === '-h' const isVersionFlag = (argument) => argument === '--version' || argument === '-v' +const isQuietFlag = (argument) => argument === '--quiet' || argument === '-q' const cliArguments = process.argv.slice(2) const isCheck = cliArguments.some(isCheckFlag) +const isQuiet = cliArguments.some(isQuietFlag) + +const stdout = isQuiet ? () => {} : console.log +const stderr = console.error + const isHelp = cliArguments.some(isHelpFlag) const isVersion = cliArguments.some(isVersionFlag) @@ -20,6 +26,7 @@ Sort npm package.json files. Default: ./package.json Strings passed as files are parsed as globs. -c, --check check if FILES are sorted + -q, --quiet don't output success messages -h, --help display this help and exit -v, --version display the version and exit `, @@ -35,7 +42,9 @@ if (isVersion) { process.exit(0) } -const patterns = cliArguments.filter((argument) => !isCheckFlag(argument)) +const patterns = cliArguments.filter( + (argument) => !isCheckFlag(argument) && !isQuietFlag(argument), +) if (!patterns.length) { patterns[0] = 'package.json' @@ -44,7 +53,7 @@ if (!patterns.length) { const files = globbySync(patterns) if (files.length === 0) { - console.log('No matching files.') + stderr('No matching files.') process.exit(1) } @@ -57,24 +66,24 @@ files.forEach((file) => { if (sorted !== packageJson) { if (isCheck) { notSortedFiles++ - console.log(file) + stdout(file) } else { fs.writeFileSync(file, sorted, 'utf8') - console.log(`${file} is sorted!`) + stdout(`${file} is sorted!`) } } }) if (isCheck) { - console.log() + stdout() if (notSortedFiles) { - console.log( + stdout( notSortedFiles === 1 ? `${notSortedFiles} of ${files.length} matched file is not sorted.` : `${notSortedFiles} of ${files.length} matched files are not sorted.`, ) } else { - console.log( + stdout( files.length === 1 ? `${files.length} matched file is sorted.` : `${files.length} matched files are sorted.`, diff --git a/tests/cli.js b/tests/cli.js index dff443c4..9f628246 100644 --- a/tests/cli.js +++ b/tests/cli.js @@ -18,6 +18,51 @@ test('cli', (t) => { ) }) +test('run `cli --help`', macro.testCLI, { + args: ['--help'], + message: 'Should report help menu.', +}) + +test('run `cli --help --quiet`', macro.testCLI, { + args: ['--help', '--quiet'], + message: 'Should report help menu overriding quiet.', +}) + +test('run `cli -h`', macro.testCLI, { + args: ['-h'], + message: 'Should support help alias.', +}) + +test('run `cli --help` with other arguments', macro.testCLI, { + args: ['NONE_EXISTS_FILE', '--help'], + message: 'Should prioritize help argument.', +}) + +test('run `cli --version`', macro.testCLI, { + args: ['--version'], + message: 'Should report version number.', +}) + +test('run `cli --version --quiet`', macro.testCLI, { + args: ['--version', '--quiet'], + message: 'Should report version overriding quiet.', +}) + +test('run `cli -v`', macro.testCLI, { + args: ['-v'], + message: 'Should support version alias.', +}) + +test('run `cli --version` with other arguments', macro.testCLI, { + args: ['NONE_EXISTS_FILE', '--version'], + message: 'Should prioritize version argument.', +}) + +test('run `cli --help` with `--version`', macro.testCLI, { + args: ['--version', '--help'], + message: 'Should prioritize help over version.', +}) + test('run `cli` with no patterns', macro.testCLI, { fixtures: [ { @@ -30,6 +75,30 @@ test('run `cli` with no patterns', macro.testCLI, { message: 'Should format package.json.', }) +test('run `cli --quiet` with no patterns', macro.testCLI, { + fixtures: [ + { + file: 'package.json', + content: badJson, + expect: goodJson, + }, + ], + args: ['--quiet'], + message: 'Should format package.json without message.', +}) + +test('run `cli -q` with no patterns', macro.testCLI, { + fixtures: [ + { + file: 'package.json', + content: badJson, + expect: goodJson, + }, + ], + args: ['-q'], + message: 'Should support -q alias.', +}) + test('run `cli --check` with no patterns', macro.testCLI, { fixtures: [ { @@ -39,7 +108,19 @@ test('run `cli --check` with no patterns', macro.testCLI, { }, ], args: ['--check'], - message: 'Should package.json is not sorted', + message: 'Should not sort package.json', +}) + +test('run `cli --check --quiet` with no patterns', macro.testCLI, { + fixtures: [ + { + file: 'package.json', + content: badJson, + expect: badJson, + }, + ], + args: ['--check', '--quiet'], + message: 'Should not sort package.json or report a message.', }) test('run `cli -c` with no patterns', macro.testCLI, { @@ -54,6 +135,18 @@ test('run `cli -c` with no patterns', macro.testCLI, { message: 'Should support `-c` alias', }) +test('run `cli -c -q` with no patterns', macro.testCLI, { + fixtures: [ + { + file: 'package.json', + content: badJson, + expect: badJson, + }, + ], + args: ['-c', '-q'], + message: 'Should support `-q` alias', +}) + test('run `cli` on 1 bad file', macro.testCLI, { fixtures: [ { @@ -66,6 +159,18 @@ test('run `cli` on 1 bad file', macro.testCLI, { message: 'Should format 1 file.', }) +test('run `cli --quiet` on 1 bad file', macro.testCLI, { + fixtures: [ + { + file: 'bad/package.json', + content: badJson, + expect: goodJson, + }, + ], + args: ['*/package.json', '--quiet'], + message: 'Should format 1 file without message.', +}) + test('run `cli --check` on 1 bad file', macro.testCLI, { fixtures: [ { @@ -78,6 +183,18 @@ test('run `cli --check` on 1 bad file', macro.testCLI, { message: 'Should report 1 file.', }) +test('run `cli --check --quiet` on 1 bad file', macro.testCLI, { + fixtures: [ + { + file: 'bad/package.json', + content: badJson, + expect: badJson, + }, + ], + args: ['*/package.json', '--check', '--quiet'], + message: 'Should exit code 1 without report.', +}) + test('run `cli` on 2 bad files', macro.testCLI, { fixtures: Array.from({ length: 2 }, (_, index) => ({ file: `bad-${index + 1}/package.json`, @@ -98,6 +215,26 @@ test('run `cli --check` on 2 bad files', macro.testCLI, { message: 'Should report 2 files.', }) +test('run `cli --quiet` on 2 bad files', macro.testCLI, { + fixtures: Array.from({ length: 2 }, (_, index) => ({ + file: `bad-${index + 1}/package.json`, + content: badJson, + expect: goodJson, + })), + args: ['*/package.json', '--quiet'], + message: 'Should format 2 files without messages.', +}) + +test('run `cli --check --quiet` on 2 bad files', macro.testCLI, { + fixtures: Array.from({ length: 2 }, (_, index) => ({ + file: `bad-${index + 1}/package.json`, + content: badJson, + expect: badJson, + })), + args: ['*/package.json', '--check', '--quiet'], + message: 'Should exit code 2.', +}) + test('run `cli` on 2 good files and 2 bad files', macro.testCLI, { fixtures: [ ...Array.from({ length: 2 }, (_, index) => ({ @@ -115,6 +252,23 @@ test('run `cli` on 2 good files and 2 bad files', macro.testCLI, { message: 'Should format 2 files.', }) +test('run `cli --quiet` on 2 good files and 2 bad files', macro.testCLI, { + fixtures: [ + ...Array.from({ length: 2 }, (_, index) => ({ + file: `bad-${index + 1}/package.json`, + content: badJson, + expect: goodJson, + })), + ...Array.from({ length: 2 }, (_, index) => ({ + file: `good-${index + 1}/package.json`, + content: goodJson, + expect: goodJson, + })), + ], + args: ['*/package.json', '--quiet'], + message: 'Should format 2 files.', +}) + test('run `cli --check` on 2 good files and 2 bad files', macro.testCLI, { fixtures: [ ...Array.from({ length: 2 }, (_, index) => ({ @@ -132,6 +286,27 @@ test('run `cli --check` on 2 good files and 2 bad files', macro.testCLI, { message: 'Should report 2 files.', }) +test( + 'run `cli --check --quiet` on 2 good files and 2 bad files', + macro.testCLI, + { + fixtures: [ + ...Array.from({ length: 2 }, (_, index) => ({ + file: `bad-${index + 1}/package.json`, + content: badJson, + expect: badJson, + })), + ...Array.from({ length: 2 }, (_, index) => ({ + file: `good-${index + 1}/package.json`, + content: goodJson, + expect: goodJson, + })), + ], + args: ['*/package.json', '--check', '--quiet'], + message: 'Should exit code 2.', + }, +) + test('run `cli` on none exists file', macro.testCLI, { fixtures: [ { @@ -144,6 +319,18 @@ test('run `cli` on none exists file', macro.testCLI, { message: 'Should report no files matching.', }) +test('run `cli --quiet` on none exists file', macro.testCLI, { + fixtures: [ + { + file: 'package.json', + content: badJson, + expect: badJson, + }, + ], + args: ['NONE_EXISTS_FILE', '--quiet'], + message: 'Should report no files matching.', +}) + test('run `cli --check` on none exists file', macro.testCLI, { fixtures: [ { @@ -156,6 +343,18 @@ test('run `cli --check` on none exists file', macro.testCLI, { message: 'Should report no files matching.', }) +test('run `cli --check --quiet` on none exists file', macro.testCLI, { + fixtures: [ + { + file: 'package.json', + content: badJson, + expect: badJson, + }, + ], + args: ['NONE_EXISTS_FILE', '--check', '--quiet'], + message: 'Should report no files matching regardless of quiet.', +}) + test('run `cli` on duplicate patterns', macro.testCLI, { fixtures: [ { @@ -210,3 +409,32 @@ test('run `cli --check` on duplicate patterns', macro.testCLI, { ], message: 'Should not list `bad-1/package.json` more than once.', }) + +test('run `cli --check --quiet` on duplicate patterns', macro.testCLI, { + fixtures: [ + { + file: 'bad-1/package.json', + content: badJson, + expect: badJson, + }, + { + file: 'good-1/package.json', + content: goodJson, + expect: goodJson, + }, + { + file: 'good-2/package.json', + content: goodJson, + expect: goodJson, + }, + ], + args: [ + 'bad-1/package.json', + 'bad-1/package.json', + 'bad-*/package.json', + '*/package.json', + '--check', + '--quiet', + ], + message: 'Should not count `bad-1/package.json` more than once. Exit code 1', +}) diff --git a/tests/snapshots/cli.js.md b/tests/snapshots/cli.js.md index 9e4bf27f..25b467ce 100644 --- a/tests/snapshots/cli.js.md +++ b/tests/snapshots/cli.js.md @@ -4,6 +4,204 @@ The actual snapshot is saved in `cli.js.snap`. Generated by [AVA](https://avajs.dev). +## run `cli --help` + +> Should report help menu. + + { + args: [ + '--help', + ], + fixtures: [], + result: { + errorCode: null, + stderr: '', + stdout: `Usage: sort-package-json [OPTION...] [FILE...]␊ + Sort npm package.json files. Default: ./package.json␊ + Strings passed as files are parsed as globs.␊ + ␊ + -c, --check check if FILES are sorted␊ + -q, --quiet don't output success messages␊ + -h, --help display this help and exit␊ + -v, --version display the version and exit␊ + ␊ + `, + }, + } + +## run `cli --help --quiet` + +> Should report help menu overriding quiet. + + { + args: [ + '--help', + '--quiet', + ], + fixtures: [], + result: { + errorCode: null, + stderr: '', + stdout: `Usage: sort-package-json [OPTION...] [FILE...]␊ + Sort npm package.json files. Default: ./package.json␊ + Strings passed as files are parsed as globs.␊ + ␊ + -c, --check check if FILES are sorted␊ + -q, --quiet don't output success messages␊ + -h, --help display this help and exit␊ + -v, --version display the version and exit␊ + ␊ + `, + }, + } + +## run `cli -h` + +> Should support help alias. + + { + args: [ + '-h', + ], + fixtures: [], + result: { + errorCode: null, + stderr: '', + stdout: `Usage: sort-package-json [OPTION...] [FILE...]␊ + Sort npm package.json files. Default: ./package.json␊ + Strings passed as files are parsed as globs.␊ + ␊ + -c, --check check if FILES are sorted␊ + -q, --quiet don't output success messages␊ + -h, --help display this help and exit␊ + -v, --version display the version and exit␊ + ␊ + `, + }, + } + +## run `cli --help` with other arguments + +> Should prioritize help argument. + + { + args: [ + 'NONE_EXISTS_FILE', + '--help', + ], + fixtures: [], + result: { + errorCode: null, + stderr: '', + stdout: `Usage: sort-package-json [OPTION...] [FILE...]␊ + Sort npm package.json files. Default: ./package.json␊ + Strings passed as files are parsed as globs.␊ + ␊ + -c, --check check if FILES are sorted␊ + -q, --quiet don't output success messages␊ + -h, --help display this help and exit␊ + -v, --version display the version and exit␊ + ␊ + `, + }, + } + +## run `cli --version` + +> Should report version number. + + { + args: [ + '--version', + ], + fixtures: [], + result: { + errorCode: null, + stderr: '', + stdout: `sort-package-json 0.0.0-development␊ + `, + }, + } + +## run `cli --version --quiet` + +> Should report version overriding quiet. + + { + args: [ + '--version', + '--quiet', + ], + fixtures: [], + result: { + errorCode: null, + stderr: '', + stdout: `sort-package-json 0.0.0-development␊ + `, + }, + } + +## run `cli -v` + +> Should support version alias. + + { + args: [ + '-v', + ], + fixtures: [], + result: { + errorCode: null, + stderr: '', + stdout: `sort-package-json 0.0.0-development␊ + `, + }, + } + +## run `cli --version` with other arguments + +> Should prioritize version argument. + + { + args: [ + 'NONE_EXISTS_FILE', + '--version', + ], + fixtures: [], + result: { + errorCode: null, + stderr: '', + stdout: `sort-package-json 0.0.0-development␊ + `, + }, + } + +## run `cli --help` with `--version` + +> Should prioritize help over version. + + { + args: [ + '--version', + '--help', + ], + fixtures: [], + result: { + errorCode: null, + stderr: '', + stdout: `Usage: sort-package-json [OPTION...] [FILE...]␊ + Sort npm package.json files. Default: ./package.json␊ + Strings passed as files are parsed as globs.␊ + ␊ + -c, --check check if FILES are sorted␊ + -q, --quiet don't output success messages␊ + -h, --help display this help and exit␊ + -v, --version display the version and exit␊ + ␊ + `, + }, + } + ## run `cli` with no patterns > Should format package.json. @@ -31,9 +229,65 @@ Generated by [AVA](https://avajs.dev). }, } +## run `cli --quiet` with no patterns + +> Should format package.json without message. + + { + args: [ + '--quiet', + ], + fixtures: [ + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + ], + result: { + errorCode: null, + stderr: '', + stdout: '', + }, + } + +## run `cli -q` with no patterns + +> Should support -q alias. + + { + args: [ + '-q', + ], + fixtures: [ + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + ], + result: { + errorCode: null, + stderr: '', + stdout: '', + }, + } + ## run `cli --check` with no patterns -> Should package.json is not sorted +> Should not sort package.json { args: [ @@ -62,6 +316,35 @@ Generated by [AVA](https://avajs.dev). }, } +## run `cli --check --quiet` with no patterns + +> Should not sort package.json or report a message. + + { + args: [ + '--check', + '--quiet', + ], + 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 -c` with no patterns > Should support `-c` alias @@ -76,7 +359,240 @@ Generated by [AVA](https://avajs.dev). "version": "1.0.0",␊ "name": "sort-package-json"␊ }`, - file: 'package.json', + file: 'package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + ], + result: { + errorCode: 1, + stderr: '', + stdout: `package.json␊ + ␊ + 1 of 1 matched file is not sorted.␊ + `, + }, + } + +## run `cli -c -q` with no patterns + +> Should support `-q` alias + + { + args: [ + '-c', + '-q', + ], + 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. + + { + args: [ + '*/package.json', + ], + fixtures: [ + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'bad/package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + ], + result: { + errorCode: null, + stderr: '', + stdout: `bad/package.json is sorted!␊ + `, + }, + } + +## run `cli --quiet` on 1 bad file + +> Should format 1 file without message. + + { + args: [ + '*/package.json', + '--quiet', + ], + fixtures: [ + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'bad/package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + ], + result: { + errorCode: null, + stderr: '', + stdout: '', + }, + } + +## run `cli --check` on 1 bad file + +> Should report 1 file. + + { + args: [ + '*/package.json', + '--check', + ], + fixtures: [ + { + expect: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + file: 'bad/package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + ], + result: { + errorCode: 1, + stderr: '', + stdout: `bad/package.json␊ + ␊ + 1 of 1 matched file is not sorted.␊ + `, + }, + } + +## run `cli --check --quiet` on 1 bad file + +> Should exit code 1 without report. + + { + args: [ + '*/package.json', + '--check', + '--quiet', + ], + fixtures: [ + { + expect: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + file: 'bad/package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + ], + result: { + errorCode: 1, + stderr: '', + stdout: '', + }, + } + +## run `cli` on 2 bad files + +> Should format 2 files. + + { + args: [ + '*/package.json', + ], + fixtures: [ + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'bad-1/package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'bad-2/package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + ], + result: { + errorCode: null, + stderr: '', + stdout: `bad-1/package.json is sorted!␊ + bad-2/package.json is sorted!␊ + `, + }, + } + +## run `cli --check` on 2 bad files + +> Should report 2 files. + + { + args: [ + '*/package.json', + '--check', + ], + fixtures: [ + { + expect: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + file: 'bad-1/package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + { + expect: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + file: 'bad-2/package.json', original: `{␊ "version": "1.0.0",␊ "name": "sort-package-json"␊ @@ -84,22 +600,24 @@ Generated by [AVA](https://avajs.dev). }, ], result: { - errorCode: 1, + errorCode: 2, stderr: '', - stdout: `package.json␊ + stdout: `bad-1/package.json␊ + bad-2/package.json␊ ␊ - 1 of 1 matched file is not sorted.␊ + 2 of 2 matched files are not sorted.␊ `, }, } -## run `cli` on 1 bad file +## run `cli --quiet` on 2 bad files -> Should format 1 file. +> Should format 2 files without messages. { args: [ '*/package.json', + '--quiet', ], fixtures: [ { @@ -107,7 +625,18 @@ Generated by [AVA](https://avajs.dev). "name": "sort-package-json",␊ "version": "1.0.0"␊ }`, - file: 'bad/package.json', + file: 'bad-1/package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'bad-2/package.json', original: `{␊ "version": "1.0.0",␊ "name": "sort-package-json"␊ @@ -117,19 +646,19 @@ Generated by [AVA](https://avajs.dev). result: { errorCode: null, stderr: '', - stdout: `bad/package.json is sorted!␊ - `, + stdout: '', }, } -## run `cli --check` on 1 bad file +## run `cli --check --quiet` on 2 bad files -> Should report 1 file. +> Should exit code 2. { args: [ '*/package.json', '--check', + '--quiet', ], fixtures: [ { @@ -137,7 +666,18 @@ Generated by [AVA](https://avajs.dev). "version": "1.0.0",␊ "name": "sort-package-json"␊ }`, - file: 'bad/package.json', + file: 'bad-1/package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + { + expect: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + file: 'bad-2/package.json', original: `{␊ "version": "1.0.0",␊ "name": "sort-package-json"␊ @@ -145,16 +685,13 @@ Generated by [AVA](https://avajs.dev). }, ], result: { - errorCode: 1, + errorCode: 2, stderr: '', - stdout: `bad/package.json␊ - ␊ - 1 of 1 matched file is not sorted.␊ - `, + stdout: '', }, } -## run `cli` on 2 bad files +## run `cli` on 2 good files and 2 bad files > Should format 2 files. @@ -185,6 +722,28 @@ Generated by [AVA](https://avajs.dev). "name": "sort-package-json"␊ }`, }, + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'good-1/package.json', + original: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + }, + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'good-2/package.json', + original: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + }, ], result: { errorCode: null, @@ -195,20 +754,20 @@ Generated by [AVA](https://avajs.dev). }, } -## run `cli --check` on 2 bad files +## run `cli --quiet` on 2 good files and 2 bad files -> Should report 2 files. +> Should format 2 files. { args: [ '*/package.json', - '--check', + '--quiet', ], fixtures: [ { expect: `{␊ - "version": "1.0.0",␊ - "name": "sort-package-json"␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ }`, file: 'bad-1/package.json', original: `{␊ @@ -218,8 +777,8 @@ Generated by [AVA](https://avajs.dev). }, { expect: `{␊ - "version": "1.0.0",␊ - "name": "sort-package-json"␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ }`, file: 'bad-2/package.json', original: `{␊ @@ -227,31 +786,50 @@ Generated by [AVA](https://avajs.dev). "name": "sort-package-json"␊ }`, }, + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'good-1/package.json', + original: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + }, + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'good-2/package.json', + original: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + }, ], result: { - errorCode: 2, + errorCode: null, stderr: '', - stdout: `bad-1/package.json␊ - bad-2/package.json␊ - ␊ - 2 of 2 matched files are not sorted.␊ - `, + stdout: '', }, } -## run `cli` on 2 good files and 2 bad files +## run `cli --check` on 2 good files and 2 bad files -> Should format 2 files. +> Should report 2 files. { args: [ '*/package.json', + '--check', ], fixtures: [ { expect: `{␊ - "name": "sort-package-json",␊ - "version": "1.0.0"␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ }`, file: 'bad-1/package.json', original: `{␊ @@ -261,8 +839,8 @@ Generated by [AVA](https://avajs.dev). }, { expect: `{␊ - "name": "sort-package-json",␊ - "version": "1.0.0"␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ }`, file: 'bad-2/package.json', original: `{␊ @@ -294,22 +872,25 @@ Generated by [AVA](https://avajs.dev). }, ], result: { - errorCode: null, + errorCode: 2, stderr: '', - stdout: `bad-1/package.json is sorted!␊ - bad-2/package.json is sorted!␊ + stdout: `bad-1/package.json␊ + bad-2/package.json␊ + ␊ + 2 of 4 matched files are not sorted.␊ `, }, } -## run `cli --check` on 2 good files and 2 bad files +## run `cli --check --quiet` on 2 good files and 2 bad files -> Should report 2 files. +> Should exit code 2. { args: [ '*/package.json', '--check', + '--quiet', ], fixtures: [ { @@ -360,11 +941,7 @@ Generated by [AVA](https://avajs.dev). result: { errorCode: 2, stderr: '', - stdout: `bad-1/package.json␊ - bad-2/package.json␊ - ␊ - 2 of 4 matched files are not sorted.␊ - `, + stdout: '', }, } @@ -391,9 +968,39 @@ Generated by [AVA](https://avajs.dev). ], result: { errorCode: 1, - stderr: '', - stdout: `No matching files.␊ + stderr: `No matching files.␊ + `, + stdout: '', + }, + } + +## run `cli --quiet` on none exists file + +> Should report no files matching. + + { + args: [ + 'NONE_EXISTS_FILE', + '--quiet', + ], + 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: `No matching files.␊ `, + stdout: '', }, } @@ -421,9 +1028,40 @@ Generated by [AVA](https://avajs.dev). ], result: { errorCode: 1, - stderr: '', - stdout: `No matching files.␊ + stderr: `No matching files.␊ + `, + stdout: '', + }, + } + +## run `cli --check --quiet` on none exists file + +> Should report no files matching regardless of quiet. + + { + args: [ + 'NONE_EXISTS_FILE', + '--check', + '--quiet', + ], + 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: `No matching files.␊ `, + stdout: '', }, } @@ -537,3 +1175,58 @@ Generated by [AVA](https://avajs.dev). `, }, } + +## run `cli --check --quiet` on duplicate patterns + +> Should not count `bad-1/package.json` more than once. Exit code 1 + + { + args: [ + 'bad-1/package.json', + 'bad-1/package.json', + 'bad-*/package.json', + '*/package.json', + '--check', + '--quiet', + ], + fixtures: [ + { + expect: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + file: 'bad-1/package.json', + original: `{␊ + "version": "1.0.0",␊ + "name": "sort-package-json"␊ + }`, + }, + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'good-1/package.json', + original: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + }, + { + expect: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + file: 'good-2/package.json', + original: `{␊ + "name": "sort-package-json",␊ + "version": "1.0.0"␊ + }`, + }, + ], + result: { + errorCode: 1, + stderr: '', + stdout: '', + }, + } diff --git a/tests/snapshots/cli.js.snap b/tests/snapshots/cli.js.snap index 4cfe5476..968ae376 100644 Binary files a/tests/snapshots/cli.js.snap and b/tests/snapshots/cli.js.snap differ