Skip to content

Commit

Permalink
Disallow --format lines with json options and other formatting options.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Sep 17, 2023
1 parent aa09e63 commit a72c284
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/lib/initOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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' : ''}.`)
Expand Down
91 changes: 90 additions & 1 deletion test/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,101 @@ 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 {
await fs.rm(tempDir, { recursive: true, force: true })
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()
}
})
})

0 comments on commit a72c284

Please sign in to comment.