Skip to content

Commit

Permalink
fix: prevent infinite loop with empty locale (#2179)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
jly36963 and bcoe committed May 15, 2022
1 parent 4dac5b8 commit b672e70
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/yargs-factory.ts
Expand Up @@ -895,7 +895,7 @@ export class YargsInstance {
}
locale(locale?: string): YargsInstance | string {
argsert('[string]', [locale], arguments.length);
if (!locale) {
if (locale === undefined) {
this[kGuessLocale]();
return this.#shim.y18n.getLocale();
}
Expand Down
32 changes: 32 additions & 0 deletions test/yargs.cjs
Expand Up @@ -757,6 +757,38 @@ describe('yargs dsl tests', () => {
r.logs.join(' ').should.match(/Parlay this here code of conduct/);
});

// Addresses: https://github.com/yargs/yargs/issues/2178
it('does not enter infinite loop when locale is invalid', () => {
// Save env vars
const lcAll = process.env.LC_ALL;
const lcMessages = process.env.LC_MESSAGES;
const lang = process.env.LANG;
const language = process.env.LANGUAGE;
// Change
delete process.env.LC_ALL;
delete process.env.LC_MESSAGES;
process.env.LANG = '.UTF-8';
delete process.env.LANGUAGE;
try {
yargs
.command({
command: 'cmd1',
desc: 'cmd1 desc',
builder: () => {},
handler: _argv => {},
})
.parse();
} catch {
expect.fail();
} finally {
// Restore
process.env.LC_ALL = lcAll;
process.env.LC_MESSAGES = lcMessages;
process.env.LANG = lang;
process.env.LANGUAGE = language;
}
});

describe('updateLocale', () => {
it('allows you to override the default locale strings', () => {
const r = checkOutput(() => {
Expand Down

0 comments on commit b672e70

Please sign in to comment.