Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent infinite loop with locale #2179

Merged
merged 2 commits into from May 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/yargs-factory.ts
Expand Up @@ -871,7 +871,7 @@ export class YargsInstance {
}
locale(locale?: string): YargsInstance | string {
argsert('[string]', [locale], arguments.length);
if (!locale) {
if (locale === undefined) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'' is falsey, and it causes the two functions to infinitely call each other

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