Skip to content

Commit

Permalink
Merge pull request #1441 from snyk/fix/woof-safety
Browse files Browse the repository at this point in the history
woof command language selection works
  • Loading branch information
Avishagp committed Oct 5, 2020
2 parents 7c7d579 + c5432cd commit 2fae1cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/cli/commands/woof.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { MethodArgs, ArgsOptions } from '../args';

interface WoofOptions {
language?: string; // ISO 639-1 code
}

const woofs = {
en: 'Woof!',
he: ' !הב ',
Expand All @@ -12,13 +8,22 @@ const woofs = {
cs: ' Haf!',
};

export = function woof(...args0: MethodArgs) {
const args = [...args0];
let options: WoofOptions = {};
if (typeof args[args.length - 1] === 'object') {
options = (args.pop() as ArgsOptions) as WoofOptions;
export function getWoofLanguage(args: MethodArgs): string {
const options = args.pop() as ArgsOptions;
let lang = 'en';

if (
typeof options.language === 'string' &&
Object.keys(woofs).includes(options.language)
) {
lang = options.language;
}
const lang = options.language || 'en';

return lang;
}

export default function woof(...args: MethodArgs) {
const lang = getWoofLanguage(args);
console.log(`
| |
/| |\\
Expand All @@ -35,4 +40,4 @@ export = function woof(...args0: MethodArgs) {
\\U/ --( ${woofs[lang]} )
\\-----/
`);
};
}
28 changes: 28 additions & 0 deletions test/woof.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getWoofLanguage } from '../src/cli/commands/woof';

describe('Woof command - Language option', () => {
it('Default language is "en"', () => {
// $ snyk woof
expect(getWoofLanguage([{} as any])).toEqual('en');
});

it('Returns selected language', () => {
expect(
getWoofLanguage([
{
language: 'he',
} as any,
]),
).toEqual('he');
});

it('Returns default when selected language is invalid', () => {
expect(
getWoofLanguage([
{
language: 'toString',
} as any,
]),
).toEqual('en');
});
});

0 comments on commit 2fae1cb

Please sign in to comment.