From a72c28455cb8f512eee35964c15625708f2f1723 Mon Sep 17 00:00:00 2001 From: Raine Revere Date: Sun, 17 Sep 2023 14:53:51 +0000 Subject: [PATCH] Disallow --format lines with json options and other formatting options. --- src/lib/initOptions.ts | 12 ++++-- test/format.test.ts | 91 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 4 deletions(-) diff --git a/src/lib/initOptions.ts b/src/lib/initOptions.ts index faa1aacd..0c7464d2 100644 --- a/src/lib/initOptions.ts +++ b/src/lib/initOptions.ts @@ -120,6 +120,7 @@ async function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } = const filterVersion = parseFilterExpression(options.filterVersion) const reject = parseFilterExpression(options.reject) const rejectVersion = parseFilterExpression(options.rejectVersion) + // convert to string for comparison purposes // otherwise ['a b'] will not match ['a', 'b'] if (options.filter && args && !isEqual(args.join(' '), Array.isArray(filter) ? filter.join(' ') : filter)) { @@ -135,17 +136,22 @@ async function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } = `Cannot specify both --packageFile and --deep. --deep is an alias for --packageFile '**/package.json'`, ) } - + // disallow --format lines and --jsonUpgraded + else if (options.format?.includes('lines') && options.jsonUpgraded) { + programError(options, 'Cannot specify both --format lines and --jsonUpgraded.') + } else if (options.format?.includes('lines') && options.jsonAll) { + programError(options, 'Cannot specify both --format lines and --jsonAll.') + } else if (options.format?.includes('lines') && options.format.length > 1) { + programError(options, 'Cannot use --format lines with other formatting options.') + } // disallow --workspace and --workspaces else if (options.workspace?.length && options.workspaces) { programError(options, 'Cannot specify both --workspace and --workspaces.') } - // disallow --workspace(s) and --deep else if (options.deep && (options.workspace?.length || options.workspaces)) { programError(options, `Cannot specify both --deep and --workspace${options.workspaces ? 's' : ''}.`) } - // disallow --workspace(s) and --doctor else if (options.doctor && (options.workspace?.length || options.workspaces)) { programError(options, `Doctor mode is not currently supported with --workspace${options.workspaces ? 's' : ''}.`) diff --git a/test/format.test.ts b/test/format.test.ts index 92975129..5f6ea165 100644 --- a/test/format.test.ts +++ b/test/format.test.ts @@ -74,7 +74,6 @@ describe('format', () => { 'utf-8', ) try { - await spawn('npm', ['install'], { cwd: tempDir }) const output = await spawn('node', [bin, '--format', 'lines'], { cwd: tempDir }) output.should.equals('ncu-test-v2@^2.0.0\nncu-test-tag@^1.1.0\n') } finally { @@ -82,4 +81,94 @@ describe('format', () => { stub.restore() } }) + + it('disallow --format lines with --jsonUpgraded', async () => { + const stub = stubNpmView( + { + 'ncu-test-v2': '2.0.0', + 'ncu-test-tag': '1.1.0', + }, + { spawn: true }, + ) + const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-')) + const pkgFile = path.join(tempDir, 'package.json') + await fs.writeFile( + pkgFile, + JSON.stringify({ + dependencies: { + 'ncu-test-v2': '^1.0.0', + 'ncu-test-tag': '^1.0.0', + }, + }), + 'utf-8', + ) + try { + await spawn('node', [bin, '--format', 'lines', '--jsonUpgraded'], { + cwd: tempDir, + }).should.eventually.be.rejectedWith('Cannot specify both --format lines and --jsonUpgraded.') + } finally { + await fs.rm(tempDir, { recursive: true, force: true }) + stub.restore() + } + }) + + it('disallow --format lines with --jsonAll', async () => { + const stub = stubNpmView( + { + 'ncu-test-v2': '2.0.0', + 'ncu-test-tag': '1.1.0', + }, + { spawn: true }, + ) + const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-')) + const pkgFile = path.join(tempDir, 'package.json') + await fs.writeFile( + pkgFile, + JSON.stringify({ + dependencies: { + 'ncu-test-v2': '^1.0.0', + 'ncu-test-tag': '^1.0.0', + }, + }), + 'utf-8', + ) + try { + await spawn('node', [bin, '--format', 'lines', '--jsonAll'], { + cwd: tempDir, + }).should.eventually.be.rejectedWith('Cannot specify both --format lines and --jsonAll.') + } finally { + await fs.rm(tempDir, { recursive: true, force: true }) + stub.restore() + } + }) + + it('disallow --format lines with other format options', async () => { + const stub = stubNpmView( + { + 'ncu-test-v2': '2.0.0', + 'ncu-test-tag': '1.1.0', + }, + { spawn: true }, + ) + const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-')) + const pkgFile = path.join(tempDir, 'package.json') + await fs.writeFile( + pkgFile, + JSON.stringify({ + dependencies: { + 'ncu-test-v2': '^1.0.0', + 'ncu-test-tag': '^1.0.0', + }, + }), + 'utf-8', + ) + try { + await spawn('node', [bin, '--format', 'lines,group'], { + cwd: tempDir, + }).should.eventually.be.rejectedWith('Cannot use --format lines with other formatting options.') + } finally { + await fs.rm(tempDir, { recursive: true, force: true }) + stub.restore() + } + }) })