Skip to content

Commit 51f736f

Browse files
committed
Get mocking and path validation working
1 parent b8af865 commit 51f736f

2 files changed

Lines changed: 50 additions & 16 deletions

File tree

scripts/generateTranslations.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
import * as dotenv from 'dotenv';
77
import fs from 'fs';
8+
// eslint-disable-next-line you-dont-need-lodash-underscore/get
9+
import get from 'lodash/get';
810
import path from 'path';
911
import type {TemplateExpression} from 'typescript';
1012
import ts from 'typescript';
@@ -14,6 +16,7 @@ import dedent from '@libs/StringUtils/dedent';
1416
import hashStr from '@libs/StringUtils/hash';
1517
import {isTranslationTargetLocale, LOCALES, TRANSLATION_TARGET_LOCALES} from '@src/CONST/LOCALES';
1618
import type {Locale, TranslationTargetLocale} from '@src/CONST/LOCALES';
19+
import en from '@src/languages/en';
1720
import type {TranslationPaths} from '@src/languages/types';
1821
import CLI from './utils/CLI';
1922
import Prettier from './utils/Prettier';
@@ -584,6 +587,9 @@ class TranslationGenerator {
584587
* The main function mostly contains CLI and file I/O logic, while TS parsing and translation logic is encapsulated in TranslationGenerator.
585588
*/
586589
async function main(): Promise<void> {
590+
const languagesDir = process.env.LANGUAGES_DIR ?? path.join(__dirname, '../src/languages');
591+
const enSourceFile = path.join(languagesDir, 'en.ts');
592+
587593
/* eslint-disable @typescript-eslint/naming-convention */
588594
const cli = new CLI({
589595
flags: {
@@ -621,8 +627,25 @@ async function main(): Promise<void> {
621627
parse: (val: string): TranslationPaths[] => {
622628
const rawPaths = val.split(',').map((translationPath) => translationPath.trim());
623629
const validatedPaths: TranslationPaths[] = [];
630+
const invalidPaths: string[] = [];
631+
632+
// We disable eslint here and do a dynamic require because tests mock the en.ts file using fs, and normal imports can't be mocked by jest
633+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
634+
// const en = require(enSourceFile).default;
635+
636+
for (const rawPath of rawPaths) {
637+
if (get(en, rawPath)) {
638+
validatedPaths.push(rawPath as TranslationPaths);
639+
} else {
640+
invalidPaths.push(rawPath);
641+
}
642+
}
643+
644+
if (invalidPaths.length > 0) {
645+
throw new Error(`⚠️ Warning: --paths contains the following invalid paths: ${invalidPaths.join(', ')}`);
646+
}
624647

625-
// TODO: validate paths
648+
return validatedPaths;
626649
},
627650
supersedes: ['compare-ref'],
628651
required: false,
@@ -647,9 +670,6 @@ async function main(): Promise<void> {
647670
translator = new ChatGPTTranslator(process.env.OPENAI_API_KEY);
648671
}
649672

650-
const languagesDir = process.env.LANGUAGES_DIR ?? path.join(__dirname, '../src/languages');
651-
const enSourceFile = path.join(languagesDir, 'en.ts');
652-
653673
const generator = new TranslationGenerator({
654674
targetLanguages: cli.namedArgs.locales,
655675
languagesDir,

tests/unit/generateTranslationsTest.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ import dedent from '@libs/StringUtils/dedent';
99
import generateTranslations, {GENERATED_FILE_PREFIX} from '@scripts/generateTranslations';
1010
import Translator from '@scripts/utils/Translator/Translator';
1111

12+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment
13+
let mockEn: any = jest.requireActual('@src/languages/en');
14+
jest.mock('@src/languages/en', () => ({
15+
// eslint-disable-next-line @typescript-eslint/naming-convention
16+
__esModule: true,
17+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
18+
get default() {
19+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
20+
return mockEn;
21+
},
22+
}));
1223
jest.mock('openai');
1324

1425
let tempDir: string;
@@ -563,21 +574,24 @@ describe('generateTranslations', () => {
563574
});
564575

565576
it('translates only specified paths when --paths is provided', async () => {
577+
const strings = {
578+
greeting: 'Hello',
579+
farewell: 'Goodbye',
580+
common: {
581+
save: 'Save',
582+
cancel: 'Cancel',
583+
},
584+
errors: {
585+
generic: 'An error occurred',
586+
network: 'Network error',
587+
},
588+
};
589+
mockEn = strings;
590+
566591
fs.writeFileSync(
567592
EN_PATH,
568593
dedent(`
569-
const strings = {
570-
greeting: 'Hello',
571-
farewell: 'Goodbye',
572-
common: {
573-
save: 'Save',
574-
cancel: 'Cancel',
575-
},
576-
errors: {
577-
generic: 'An error occurred',
578-
network: 'Network error',
579-
},
580-
};
594+
const strings = ${JSON.stringify(strings)};
581595
export default strings;
582596
`),
583597
'utf8',

0 commit comments

Comments
 (0)