From ec79fcb157dad046a2439fb189667e0c2347961b Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Fri, 6 May 2022 15:23:13 -0700 Subject: [PATCH] fix: prevent infinite loop with locale --- lib/yargs-factory.ts | 2 +- test/yargs.cjs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts index 279d8bcce..c3f88348f 100644 --- a/lib/yargs-factory.ts +++ b/lib/yargs-factory.ts @@ -871,7 +871,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(); } diff --git a/test/yargs.cjs b/test/yargs.cjs index e743a70da..42b4b83b8 100644 --- a/test/yargs.cjs +++ b/test/yargs.cjs @@ -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(() => {