-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5061 from newrelic/ru/exclude-file
feat: exclusion file and tests
- Loading branch information
Showing
7 changed files
with
297 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
process.env.HUMAN_TRANSLATION_PROJECT_ID = 'HT_ID'; | ||
process.env.MACHINE_TRANSLATION_PROJECT_ID = 'MT_ID'; | ||
process.env.DB_CONNECTION_INFO = '{}'; | ||
|
||
global.___loader = { | ||
enqueue: jest.fn(), | ||
}; |
219 changes: 219 additions & 0 deletions
219
scripts/actions/__tests__/add-files-to-translation-queue.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
'use strict'; | ||
const { describe, expect, test, beforeEach } = require('@jest/globals'); | ||
|
||
const fs = require('fs'); | ||
const frontmatter = require('@github-docs/frontmatter'); | ||
const { getLocalizedFileData } = require('../add-files-to-translation-queue'); | ||
|
||
const MOCK_CONSTANTS = { | ||
LOCALE_IDS: { | ||
jp: 'ja-JP', | ||
kr: 'ko-KR', | ||
}, | ||
}; | ||
|
||
jest.mock('fs'); | ||
jest.mock('path'); | ||
jest.mock('@github-docs/frontmatter'); | ||
jest.mock('../translation_workflow/database'); | ||
jest.mock('../utils/vendor-request'); | ||
|
||
const mockMdx = (translate = []) => { | ||
return `--- | ||
title: A test file | ||
${translate.length ? `translate:\n - ${translate.join('\n - ')}` : ''} | ||
--- | ||
This is a test file | ||
`; | ||
}; | ||
|
||
const mockReadFileSync = (translate = []) => { | ||
const mdx = mockMdx(translate); | ||
fs.readFileSync.mockReturnValueOnce(mdx); | ||
}; | ||
|
||
describe('add-files-to-translation-queue tests', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
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' }; | ||
mockReadFileSync(['jp']); | ||
frontmatter.mockReturnValueOnce({ data: { translate: ['jp'] } }); | ||
const toBeTranslated = getLocalizedFileData(file); | ||
|
||
expect(toBeTranslated).toEqual([ | ||
{ filename: '/content/bar.mdx', locale: 'ja-JP', project_id: 'HT_ID' }, | ||
]); | ||
}); | ||
|
||
test('Adds the Machine Translation Project Id when there is no `translate` in frontmatter', async () => { | ||
const file = { filename: '/content/bar.mdx' }; | ||
mockReadFileSync(); | ||
frontmatter.mockReturnValueOnce({ data: {} }); | ||
const toBeTranslated = getLocalizedFileData(file); | ||
|
||
expect(toBeTranslated).toEqual([ | ||
{ filename: '/content/bar.mdx', locale: 'ja-JP', project_id: 'MT_ID' }, | ||
]); | ||
}); | ||
|
||
test('Adds the relevant Project Id when there are multiple locales and only one `translate` in frontmatter', async () => { | ||
const file = { filename: '/content/bar.mdx' }; | ||
const originalAdd = jest.requireActual('../utils/constants.js'); | ||
|
||
jest.doMock('../utils/constants.js', () => { | ||
return { ...originalAdd, LOCALE_IDS: MOCK_CONSTANTS.LOCALE_IDS }; | ||
}); | ||
|
||
const { | ||
getLocalizedFileData, | ||
} = require('../add-files-to-translation-queue'); | ||
const fs = require('fs'); | ||
const frontmatter = require('@github-docs/frontmatter'); | ||
|
||
jest.doMock('fs'); | ||
jest.doMock('@github-docs/frontmatter'); | ||
|
||
const mdx = mockMdx(['jp']); | ||
fs.readFileSync.mockReturnValueOnce(mdx); | ||
frontmatter.mockReturnValueOnce({ data: { translate: ['jp'] } }); | ||
|
||
const toBeTranslated = getLocalizedFileData(file); | ||
expect(toBeTranslated).toEqual([ | ||
{ | ||
filename: '/content/bar.mdx', | ||
locale: 'ja-JP', | ||
project_id: 'HT_ID', | ||
}, | ||
{ | ||
filename: '/content/bar.mdx', | ||
locale: 'ko-KR', | ||
project_id: 'MT_ID', | ||
}, | ||
]); | ||
}); | ||
|
||
test('Doesnt exclude any files from translation', async () => { | ||
const files = [ | ||
{ filename: 'included/path/content/bar.mdx', locale: 'ja-JP' }, | ||
{ filename: 'included/path/content/foo.mdx', locale: 'ko-KR' }, | ||
]; | ||
setup(); | ||
const { excludeFiles } = require('../add-files-to-translation-queue'); | ||
|
||
const includedFiles = excludeFiles(files); | ||
|
||
expect(includedFiles).toEqual([ | ||
{ filename: 'included/path/content/bar.mdx', locale: 'ja-JP' }, | ||
{ filename: 'included/path/content/foo.mdx', locale: 'ko-KR' }, | ||
]); | ||
}); | ||
|
||
test('Excludes files under a set path', async () => { | ||
const files = [ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
contentType: 'doc', | ||
locale: 'ko-KR', | ||
}, | ||
{ | ||
filename: 'excluded/path/content/bar.mdx', | ||
contentType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
]; | ||
setup(); | ||
|
||
const { excludeFiles } = require('../add-files-to-translation-queue'); | ||
const includedFiles = excludeFiles(files); | ||
|
||
expect(includedFiles).toEqual([ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
contentType: 'doc', | ||
locale: 'ko-KR', | ||
}, | ||
]); | ||
}); | ||
|
||
test('Excludes specific file types', async () => { | ||
const files = [ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
contentType: 'excludedType', | ||
locale: 'ko-KR', | ||
}, | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
contentType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
]; | ||
setup(); | ||
|
||
const { excludeFiles } = require('../add-files-to-translation-queue'); | ||
const includedFiles = excludeFiles(files); | ||
|
||
expect(includedFiles).toEqual([ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
contentType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
]); | ||
}); | ||
|
||
test('Excludes files and file types under a set path', async () => { | ||
const files = [ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
contentType: 'doc', | ||
locale: 'ko-KR', | ||
}, | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
contentType: 'excludedType', | ||
locale: 'ja-JP', | ||
}, | ||
{ | ||
filename: 'excluded/path/content/bar.mdx', | ||
contentType: 'doc', | ||
locale: 'ja-JP', | ||
}, | ||
]; | ||
setup(); | ||
|
||
const { excludeFiles } = require('../add-files-to-translation-queue'); | ||
const includedFiles = excludeFiles(files); | ||
|
||
expect(includedFiles).toEqual([ | ||
{ | ||
filename: 'included/path/content/bar.mdx', | ||
contentType: 'doc', | ||
locale: 'ko-KR', | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const LOCALE_IDS = { | ||
jp: 'ja-JP', | ||
}; | ||
|
||
const EXCLUSIONS_FILE = 'scripts/utils/docs-content-tools/i18n-exclusions.yml'; | ||
|
||
module.exports = { | ||
LOCALE_IDS, | ||
EXCLUSIONS_FILE, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
excludePath: | ||
ja-JP: | ||
- /whats-new | ||
excludeType: | ||
ja-JP: | ||
- landingPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |