Skip to content

Commit

Permalink
wip: expect-entries is always parsed as boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Jan 2, 2023
1 parent 8cb1ab2 commit 39911ae
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 52 deletions.
7 changes: 7 additions & 0 deletions lib/base-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,17 @@ class BaseCommand {
// Compare the number of entries with what was expected
checkExpected (entries) {
const expected = this.npm.config.get('expect-entries')

if (expected === null) {
// By default we do nothing
return
}

console.error({
parsed: expected,
original: this.npm.config.parsedArgv.original.slice(-2).join(' '),
})

if (typeof expected === 'number') {
if (entries !== expected) {
process.exitCode = 1
Expand Down
108 changes: 56 additions & 52 deletions test/lib/commands/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,69 +181,73 @@ t.test('global', async t => {
t.matchSnapshot(joinedOutput(), 'should return global package')
})

t.test('expect entries', t => {
const prefixDir = {
node_modules: {
a: { name: 'a', version: '1.0.0' },
},
'package.json': JSON.stringify({
name: 'project',
dependencies: { a: '^1.0.0' },
}),
t.test('expect entries', async t => {
const mockExpect = async (t, query, expectEntries) => {
const { joinedOutput } = await loadMockNpm(t, {
command: 'query',
exec: [query],
config: { 'expect-entries': expectEntries },
prefixDir: {
node_modules: {
a: { name: 'a', version: '1.0.0' },
b: { name: 'b', version: '1.0.0' },
c: { name: 'c', version: '1.0.0' },
},
'package.json': JSON.stringify({
name: 'project',
dependencies: { a: '^1.0.0', b: '^1.0.0', c: '^1.0.0' },
}),
},
})
return {
exitCode: process.exitCode,
entries: JSON.parse(joinedOutput()),
}
}

t.test('false, has entries', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, {
prefixDir,
config: { 'expect-entries': false },
})
await npm.exec('query', ['#a'])
t.not(joinedOutput(), '[]', 'has entries')
t.ok(process.exitCode, 'exits with code')
const { exitCode, entries } = await mockExpect(t, ':root > .prod', false)
t.equal(entries.length, 3, 'has entries')
t.ok(exitCode, 'exits with code')
})
t.test('false, no entries', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, {
prefixDir,
config: { 'expect-entries': false },
})
await npm.exec('query', ['#b'])
t.equal(joinedOutput(), '[]', 'does not have entries')
t.notOk(process.exitCode, 'exits without code')
const { exitCode, entries } = await mockExpect(t, ':root > .dev', false)
t.equal(entries.length, 0, 'does not have entries')
t.notOk(exitCode, 'exits without code')
})
t.test('true, has entries', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, {
prefixDir,
config: { 'expect-entries': true },
})
await npm.exec('query', ['#a'])
t.not(joinedOutput(), '[]', 'has entries')
t.notOk(process.exitCode, 'exits without code')
const { exitCode, entries } = await mockExpect(t, ':root > .prod', true)
t.equal(entries.length, 3, 'has entries')
t.notOk(exitCode, 'exits without code')
})
t.test('true, no entries', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, {
prefixDir,
config: { 'expect-entries': true },
})
await npm.exec('query', ['#b'])
t.equal(joinedOutput(), '[]', 'does not have entries')
t.ok(process.exitCode, 'exits with code')
const { exitCode, entries } = await mockExpect(t, ':root > .dev', true)
t.equal(entries.length, 0, 'does not have entries')
t.ok(exitCode, 'exits with code')
})
t.test('number, matches', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, {
prefixDir,
config: { 'expect-entries': 1 },
})
await npm.exec('query', ['#a'])
t.not(joinedOutput(), '[]', 'has entries')
t.notOk(process.exitCode, 'exits without code')
const { exitCode, entries } = await mockExpect(t, ':root > .prod', 3)
t.equal(entries.length, 3, 'has entries')
t.notOk(exitCode, 'exits without code')
})
t.test('number, does not match', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, {
prefixDir,
config: { 'expect-entries': 1 },
})
await npm.exec('query', ['#b'])
t.equal(joinedOutput(), '[]', 'does not have entries')
t.ok(process.exitCode, 'exits with code')
const { exitCode, entries } = await mockExpect(t, ':root > .prod', 2)
t.equal(entries.length, 3, 'has no entries')
t.ok(exitCode, 'exits with code')
})
t.test('number, no entries', async t => {
const { exitCode, entries } = await mockExpect(t, ':root > .dev', 2)
t.equal(entries.length, 0, 'has no entries')
t.ok(exitCode, 'exits with code')
})
t.test('zero, no entries', async t => {
const { exitCode, entries } = await mockExpect(t, ':root > .dev', 0)
t.equal(entries.length, 0, 'has no entries')
t.notOk(exitCode, 'exits without code')
})
t.test('zero, has entries', async t => {
const { exitCode, entries } = await mockExpect(t, ':root > .prod', 0)
t.equal(entries.length, 3, 'has no entries')
t.ok(exitCode, 'exits with code')
})
t.end()
})

0 comments on commit 39911ae

Please sign in to comment.