Skip to content

Commit 0279fcf

Browse files
baywetferrarimarco
andauthored
fix: a bug where a reference to an missing variable in PowerShell would cause an exception (#2619)
* fix: a bug where a reference to an missing variable in PowerShell would cause an exception Signed-off-by: Vincent Biret <[email protected]> --------- Signed-off-by: Vincent Biret <[email protected]> Co-authored-by: Marco Ferrari <[email protected]>
1 parent fbed6c3 commit 0279fcf

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/util/coerce-option.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {readFileSync, statSync, Stats} from 'fs';
1717
// if an option looks like a file path, assume it's a
1818
// path to a key and load it.
1919
export function coerceOption(option: string): string {
20-
if (option.match(/[\\/]/)) {
20+
// Only check .match if option is a string, to avoid TypeError.
21+
// This might happen if the user didn't pass any payload for
22+
// the option, or if the payload is empty. Example:
23+
// --token $EMPTY_VAR
24+
if (typeof option === 'string' && option.match(/[\\/]/)) {
2125
try {
2226
const stat: Stats = statSync(option);
2327
if (stat.isDirectory()) return option;

test/util/coerce-option.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ describe('coerceOption', () => {
3838
const coerced = coerceOption(resolve(fixturesPath, 'key.txt'));
3939
expect(coerced).to.equal('abc123');
4040
});
41+
it('should not throw when a non-string value is passed', () => {
42+
expect(() => coerceOption(undefined as unknown as string)).not.to.Throw();
43+
expect(() => coerceOption(null as unknown as string)).not.to.Throw();
44+
expect(() => coerceOption(123 as unknown as string)).not.to.Throw();
45+
expect(() => coerceOption({} as unknown as string)).not.to.Throw();
46+
expect(() => coerceOption([] as unknown as string)).not.to.Throw();
47+
});
4148
});

0 commit comments

Comments
 (0)