Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/util/coerce-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {readFileSync, statSync, Stats} from 'fs';
// if an option looks like a file path, assume it's a
// path to a key and load it.
export function coerceOption(option: string): string {
if (option.match(/[\\/]/)) {
// Only check .match if option is a string, to avoid TypeError.
// This might happen if the user didn't pass any payload for
// the option, or if the payload is empty. Example:
// --token $EMPTY_VAR
if (typeof option === 'string' && option.match(/[\\/]/)) {
try {
const stat: Stats = statSync(option);
if (stat.isDirectory()) return option;
Expand Down
7 changes: 7 additions & 0 deletions test/util/coerce-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ describe('coerceOption', () => {
const coerced = coerceOption(resolve(fixturesPath, 'key.txt'));
expect(coerced).to.equal('abc123');
});
it('should not throw when a non-string value is passed', () => {
expect(() => coerceOption(undefined as unknown as string)).not.to.Throw();
expect(() => coerceOption(null as unknown as string)).not.to.Throw();
expect(() => coerceOption(123 as unknown as string)).not.to.Throw();
expect(() => coerceOption({} as unknown as string)).not.to.Throw();
expect(() => coerceOption([] as unknown as string)).not.to.Throw();
});
});
Loading