Skip to content

Commit

Permalink
chore: fix tests and extract getExclusions function
Browse files Browse the repository at this point in the history
  • Loading branch information
rudouglas committed Nov 30, 2021
1 parent d693b35 commit 397d757
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 56 deletions.
70 changes: 28 additions & 42 deletions scripts/actions/__tests__/add-files-to-translation-queue.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
'use strict';
const {
describe,
expect,
test,
beforeEach,
afterEach,
} = require('@jest/globals');
const { describe, expect, test, beforeEach } = require('@jest/globals');

const fs = require('fs');
const path = require('path');
const frontmatter = require('@github-docs/frontmatter');
const {
getLocalizedFileData,
excludeFiles,
} = require('../add-files-to-translation-queue');
const { getLocalizedFileData } = require('../add-files-to-translation-queue');

const MOCK_CONSTANTS = {
LOCALE_IDS: {
Expand All @@ -37,11 +27,6 @@ This is a test file
`;
};

const EXCLUSIONS = {
excludePath: { jp: ['excluded/path'], kr: ['excluded/path'] },
excludeType: { jp: ['excludedType'], kr: ['excludedType'] },
};

const mockReadFileSync = (translate = []) => {
const mdx = mockMdx(translate);
fs.readFileSync.mockReturnValueOnce(mdx);
Expand All @@ -53,6 +38,23 @@ describe('add-files-to-translation-queue tests', () => {
jest.resetModules();
});

const setup = () => {
const EXCLUSIONS = {
excludePath: { jp: ['excluded/path'], kr: ['excluded/path'] },
excludeType: { jp: ['excludedType'], kr: ['excludedType'] },
};
const originalAdd = jest.requireActual('../utils/constants.js');

jest.doMock('../utils/constants.js', () => {
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS };
});

jest.mock('../utils/helpers');
const { getExclusions } = require('../utils/helpers');

getExclusions.mockReturnValue(EXCLUSIONS);
};

describe('Queue translations', () => {
test('Adds the Human Translation Project Id for locale under `translate` in frontmatter', async () => {
const file = { filename: '/content/bar.mdx' };
Expand Down Expand Up @@ -117,14 +119,10 @@ describe('add-files-to-translation-queue tests', () => {
{ filename: 'included/path/content/bar.mdx', locale: 'ja-JP' },
{ filename: 'included/path/content/foo.mdx', locale: 'ko-KR' },
];
const originalAdd = jest.requireActual('../utils/constants.js');

jest.doMock('../utils/constants.js', () => {
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS };
});

setup();
const { excludeFiles } = require('../add-files-to-translation-queue');
const includedFiles = excludeFiles(files, EXCLUSIONS);

const includedFiles = excludeFiles(files);

expect(includedFiles).toEqual([
{ filename: 'included/path/content/bar.mdx', locale: 'ja-JP' },
Expand All @@ -145,14 +143,10 @@ describe('add-files-to-translation-queue tests', () => {
locale: 'ja-JP',
},
];
const originalAdd = jest.requireActual('../utils/constants.js');

jest.doMock('../utils/constants.js', () => {
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS };
});
setup();

const { excludeFiles } = require('../add-files-to-translation-queue');
const includedFiles = excludeFiles(files, EXCLUSIONS);
const includedFiles = excludeFiles(files);

expect(includedFiles).toEqual([
{
Expand All @@ -176,14 +170,10 @@ describe('add-files-to-translation-queue tests', () => {
locale: 'ja-JP',
},
];
const originalAdd = jest.requireActual('../utils/constants.js');

jest.doMock('../utils/constants.js', () => {
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS };
});
setup();

const { excludeFiles } = require('../add-files-to-translation-queue');
const includedFiles = excludeFiles(files, EXCLUSIONS);
const includedFiles = excludeFiles(files);

expect(includedFiles).toEqual([
{
Expand Down Expand Up @@ -212,14 +202,10 @@ describe('add-files-to-translation-queue tests', () => {
locale: 'ja-JP',
},
];
const originalAdd = jest.requireActual('../utils/constants.js');

jest.doMock('../utils/constants.js', () => {
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS };
});
setup();

const { excludeFiles } = require('../add-files-to-translation-queue');
const includedFiles = excludeFiles(files, EXCLUSIONS);
const includedFiles = excludeFiles(files);

expect(includedFiles).toEqual([
{
Expand Down
23 changes: 9 additions & 14 deletions scripts/actions/add-files-to-translation-queue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const frontmatter = require('@github-docs/frontmatter');
const {
getTranslations,
Expand All @@ -10,7 +9,8 @@ const {
const { fetchPaginatedGHResults } = require('./utils/github-api-helpers');
const checkArgs = require('./utils/check-args');
const { prop } = require('../utils/functional');
const { LOCALE_IDS, EXCLUSIONS_FILE } = require('./utils/constants');
const { LOCALE_IDS } = require('./utils/constants');
const { getExclusions } = require('./utils/helpers');

const STATUS = {
PENDING: 'PENDING',
Expand All @@ -32,29 +32,24 @@ const slugIntersection = (pendingFiles, filesToRemove) =>
const humanTranslatedProjectID = process.env.HUMAN_TRANSLATION_PROJECT_ID;
const machineTranslatedProjectID = process.env.MACHINE_TRANSLATION_PROJECT_ID;

/**
* Loads the Exclusions yaml file and converts it to a JSON object.
* @returns {Object} The Exclusions yaml file as a JSON object.
*/
const getExclusions = () => {
return yaml.load(fs.readFileSync(path.join(process.cwd(), EXCLUSIONS_FILE)));
};

/**
* Determines if a file should be included based on data from an exclusions file
* @param {Object[]} fileData The files to check
* @param {Object[]} exclusions The exclusions file
* @returns {Object[]} The files that should be included
*/
const excludeFiles = (fileData, exclusions) => {
const excludeFiles = (fileData) => {
const exclusions = getExclusions();

return fileData.filter(({ filename, locale, contentType }) => {
const localeKey = Object.keys(LOCALE_IDS).find(
(localeKey) => LOCALE_IDS[localeKey] === locale
);
return (
!exclusions.excludePath[localeKey]?.some((path) =>
filename.startsWith(path)
) && !exclusions.excludeType[localeKey]?.some((type) => contentType === type)
) &&
!exclusions.excludeType[localeKey]?.some((type) => contentType === type)
);
});
};
Expand Down Expand Up @@ -105,9 +100,9 @@ const main = async () => {
const changedMdxFileData = prFileData
.filter((file) => path.extname(file.filename) === '.mdx')
.filter((f) => f.status !== 'removed');
const exclusions = await getExclusions();

const allLocalizedFileData = changedMdxFileData.flatMap(getLocalizedFileData);
const includedFiles = excludeFiles(allLocalizedFileData, exclusions);
const includedFiles = excludeFiles(allLocalizedFileData);

const fileDataToAddToQueue = translationDifference(queue, includedFiles);

Expand Down
16 changes: 16 additions & 0 deletions scripts/actions/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const yaml = require('js-yaml');
const fs = require('fs');
const path = require('path');
const { EXCLUSIONS_FILE } = require('./constants');

/**
* Loads the Exclusions yaml file and converts it to a JSON object.
* @returns {Object} The Exclusions yaml file as a JSON object.
*/
const getExclusions = () => {
return yaml.load(fs.readFileSync(path.join(process.cwd(), EXCLUSIONS_FILE)));
};

module.exports = {
getExclusions,
};

0 comments on commit 397d757

Please sign in to comment.