mirror of
https://github.com/strapi/strapi.git
synced 2025-12-25 14:14:10 +00:00
Update tests not to write file to disks
This commit is contained in:
parent
d85a387b24
commit
afa40877f9
@ -3,5 +3,4 @@ module.exports = {
|
||||
setupFilesAfterEnv: ['<rootDir>/test/unit.setup.js'],
|
||||
modulePathIgnorePatterns: ['.cache'],
|
||||
testMatch: ['/**/__tests__/**/*.[jt]s?(x)'],
|
||||
watchPathIgnorePatterns: ['<rootDir>/.*\\.json'],
|
||||
};
|
||||
|
||||
@ -1,14 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs-extra');
|
||||
const { addMissingKeyForSingleFile } = require('../add-missing-keys-to-other-language');
|
||||
const { updateMissingKeysToJSON } = require('../add-missing-keys-to-other-language');
|
||||
|
||||
// Do not run this test in --watch mode.
|
||||
// Since `addMissingKeyForSingleFile` makes changes to the files (`en.json`, `vi.json`),
|
||||
// accidentally triggering this test to re-run infinite times (because of jest watch mode)
|
||||
// You can use run `yarn test:unit scripts/front/__tests__/add-missing-keys-to-other-language.test.js`
|
||||
// if you want to focus on this test.
|
||||
describe('addMissingKeyForSingleFile', () => {
|
||||
describe('updateMissingKeysToJSON', () => {
|
||||
it('should add missing keys from en.json to translation file', async () => {
|
||||
const TARGET_TRANSLATION_FILE_PATH = 'scripts/front/__tests__/vi.json';
|
||||
const SOURCE_TRANSLATION_FILE_PATH = 'scripts/front/__tests__/en.json';
|
||||
@ -21,24 +16,18 @@ describe('addMissingKeyForSingleFile', () => {
|
||||
const mainTranslationFileJSON = await fs.readJSON(SOURCE_TRANSLATION_FILE_PATH);
|
||||
|
||||
// Add missing keys for `vi.json`
|
||||
await addMissingKeyForSingleFile(TARGET_TRANSLATION_FILE_PATH);
|
||||
const updatedTargetTranslationFileJSON = JSON.parse(
|
||||
fs.readFileSync(TARGET_TRANSLATION_FILE_PATH, 'utf8')
|
||||
const updatedTargetTranslationFileJSON = await updateMissingKeysToJSON(
|
||||
TARGET_TRANSLATION_FILE_PATH
|
||||
);
|
||||
|
||||
// `vi.json` should have all keys from `en.json`
|
||||
Object.keys(mainTranslationFileJSON).forEach((key) => {
|
||||
Object.keys(mainTranslationFileJSON).forEach(key => {
|
||||
expect(key in updatedTargetTranslationFileJSON).toBe(true);
|
||||
});
|
||||
|
||||
// `vi.json` should keep the current translation
|
||||
Object.keys(originalTargetTranslationFileContent).forEach((key) => {
|
||||
Object.keys(originalTargetTranslationFileContent).forEach(key => {
|
||||
expect(updatedTargetTranslationFileJSON[key]).toEqual(originalTargetTranslationFileJSON[key]);
|
||||
});
|
||||
|
||||
// Restore original `vi.json` file content
|
||||
await fs.writeJSON(TARGET_TRANSLATION_FILE_PATH, originalTargetTranslationFileJSON, {
|
||||
spaces: 2,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -12,25 +12,30 @@ const fs = require('fs-extra');
|
||||
const glob = promisify(require('glob').glob);
|
||||
const chalk = require('chalk');
|
||||
|
||||
const addMissingKeyForSingleFile = async (filePath) => {
|
||||
const updateMissingKeysToJSON = async filePath => {
|
||||
// Read translation file
|
||||
const currentTranslationFileJSON = await fs.readJSON(filePath);
|
||||
// Read en.json
|
||||
const mainTranslationFile = join(dirname(filePath), 'en.json');
|
||||
const mainTranslationFileJSON = await fs.readJSON(mainTranslationFile);
|
||||
// Add missing keys from en.json to translation file
|
||||
const updatedFileJSON = Object.keys(mainTranslationFileJSON).reduce((acc, current) => {
|
||||
if (currentTranslationFileJSON[current]) {
|
||||
acc[current] = currentTranslationFileJSON[current];
|
||||
} else {
|
||||
acc[current] = mainTranslationFileJSON[current];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
return updatedFileJSON;
|
||||
};
|
||||
|
||||
const addMissingKeyForSingleFile = async filePath => {
|
||||
console.log('Start adding missing keys to', filePath);
|
||||
try {
|
||||
// Read translation file
|
||||
const currentTranslationFileJSON = await fs.readJSON(filePath);
|
||||
// Read en.json
|
||||
const mainTranslationFile = join(dirname(filePath), 'en.json');
|
||||
const mainTranslationFileJSON = await fs.readJSON(mainTranslationFile);
|
||||
// Add missing keys from en.json to translation file
|
||||
const updatedFile = Object.keys(mainTranslationFileJSON).reduce((acc, current) => {
|
||||
if (currentTranslationFileJSON[current]) {
|
||||
acc[current] = currentTranslationFileJSON[current];
|
||||
} else {
|
||||
acc[current] = mainTranslationFileJSON[current];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
await fs.writeJson(filePath, updatedFile, { spaces: 2 });
|
||||
const updatedFileJSON = await updateMissingKeysToJSON(filePath);
|
||||
await fs.writeJson(filePath, updatedFileJSON, { spaces: 2 });
|
||||
console.log('Added missing keys to', filePath);
|
||||
return Promise.resolve();
|
||||
} catch (err) {
|
||||
@ -38,7 +43,7 @@ const addMissingKeyForSingleFile = async (filePath) => {
|
||||
}
|
||||
};
|
||||
|
||||
const addMissingKeys = async (lang) => {
|
||||
const addMissingKeys = async lang => {
|
||||
// Get translation files
|
||||
const corePackageDirs = await glob('packages/core/*');
|
||||
const pluginsPackageDirs = await glob('packages/plugins/*');
|
||||
@ -46,10 +51,10 @@ const addMissingKeys = async (lang) => {
|
||||
const pathToTranslationsFolder = ['admin', 'src', 'translations'];
|
||||
|
||||
const translationFiles = packageDirs
|
||||
.filter((dir) => {
|
||||
.filter(dir => {
|
||||
return fs.existsSync(join(dir, ...pathToTranslationsFolder, `${lang}.json`));
|
||||
})
|
||||
.map((dir) => {
|
||||
.map(dir => {
|
||||
return join(dir, ...pathToTranslationsFolder, `${lang}.json`);
|
||||
});
|
||||
console.log('List of files to add missing keys', translationFiles, '\n');
|
||||
@ -68,9 +73,9 @@ if (process.argv.length < 3) {
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
addMissingKeys(process.argv[2]).catch((err) => console.error(err));
|
||||
addMissingKeys(process.argv[2]).catch(err => console.error(err));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addMissingKeyForSingleFile,
|
||||
updateMissingKeysToJSON,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user