Skip to content

Commit

Permalink
typecheck the preserveRoot option properly
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 24, 2023
1 parent dc2fd42 commit 385f86f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const isRimrafOptions = (o: any): o is RimrafOptions =>
!!o &&
typeof o === 'object' &&
typeOrUndef(o.preserveRoot, 'boolean') &&
typeOrUndef(o.preserveRoot, 'number') &&
typeOrUndef(o.tmp, 'string') &&
typeOrUndef(o.maxRetries, 'number') &&
typeOrUndef(o.retryDelay, 'number') &&
typeOrUndef(o.backoff, 'number') &&
Expand Down
70 changes: 70 additions & 0 deletions test/opt-arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,73 @@ t.throws(() => optArg(true))
t.throws(() => optArg(null))
t.throws(() => optArg('hello'))
t.throws(() => optArg({ maxRetries: 'banana' }))

t.test('every kind of invalid option value', t => {
// skip them when it's undefined, and skip the case
// where they're all undefined, otherwise try every
// possible combination of the values here.
const badBool = [undefined, 1, null, 'x', {}]
const badNum = [undefined, true, false, null, 'x', '1', {}]
const badStr = [undefined, { toString: () => 'hi' }, /hi/, Symbol.for('hi')]
for (const preserveRoot of badBool) {
for (const tmp of badStr) {
for (const maxRetries of badNum) {
for (const retryDelay of badNum) {
for (const backoff of badNum) {
for (const maxBackoff of badNum) {
if (
preserveRoot === undefined &&
maxRetries === undefined &&
retryDelay === undefined &&
backoff === undefined &&
maxBackoff === undefined
) {
continue
}
t.throws(() =>
optArg({
preserveRoot,
maxRetries,
retryDelay,
backoff,
maxBackoff,
})
)
}
}
}
}
}
}
t.end()
})

t.test('test every allowed combination', t => {
const goodBool = [undefined, true, false]
// note that a few of these actually aren't *valid*,
// but it's verifying what the initial opt checker does.
const goodNum = [undefined, 1, Math.pow(2, 32), -1]
const goodStr = [undefined, 'hi']
for (const preserveRoot of goodBool) {
for (const tmp of goodStr) {
for (const maxRetries of goodNum) {
for (const retryDelay of goodNum) {
for (const backoff of goodNum) {
for (const maxBackoff of goodNum) {
t.ok(
optArg({
preserveRoot,
maxRetries,
retryDelay,
backoff,
maxBackoff,
})
)
}
}
}
}
}
}
t.end()
})

0 comments on commit 385f86f

Please sign in to comment.