test: check replaced iff translatable comment
All translatable comments should be replaced, but nothing else.
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
d2ecd03013
commit
793fa8fb52
@ -32,7 +32,11 @@ const vm = require('vm');
|
||||
|
||||
const puppeteer = require('puppeteer');
|
||||
|
||||
const { getChallengesForLang, getMetaForBlock } = require('../getChallenges');
|
||||
const {
|
||||
getChallengesForLang,
|
||||
getMetaForBlock,
|
||||
getTranslatableComments
|
||||
} = require('../getChallenges');
|
||||
|
||||
const MongoIds = require('./utils/mongoIds');
|
||||
const ChallengeTitles = require('./utils/challengeTitles');
|
||||
@ -54,8 +58,19 @@ const {
|
||||
|
||||
const { sortChallenges } = require('./utils/sort-challenges');
|
||||
|
||||
const TRANSLATABLE_COMMENTS = getTranslatableComments(
|
||||
path.resolve(__dirname, '..', 'dictionaries')
|
||||
);
|
||||
|
||||
const testEvaluator = require('../../client/config/test-evaluator').filename;
|
||||
|
||||
const commentExtractors = {
|
||||
html: require('./utils/extract-html-comments'),
|
||||
js: require('./utils/extract-js-comments'),
|
||||
jsx: require('./utils/extract-jsx-comments'),
|
||||
css: require('./utils/extract-css-comments')
|
||||
};
|
||||
|
||||
// rethrow unhandled rejections to make sure the tests exit with -1
|
||||
process.on('unhandledRejection', err => handleRejection(err));
|
||||
|
||||
@ -284,6 +299,72 @@ function populateTestsForLang({ lang, challenges, meta }) {
|
||||
challengeTitles.check(title, pathAndTitle);
|
||||
});
|
||||
|
||||
it('Has replaced all the English comments', () => {
|
||||
// special cases are where this process breaks for some reason, but
|
||||
// we have validated that the challenge gets parsed correctly.
|
||||
const specialCases = [
|
||||
'587d7b84367417b2b2512b36',
|
||||
'587d7b84367417b2b2512b37',
|
||||
'587d7db0367417b2b2512b82',
|
||||
'587d7dbe367417b2b2512bb8',
|
||||
'5a24c314108439a4d4036161',
|
||||
'5a24c314108439a4d4036154',
|
||||
'5a94fe0569fb03452672e45c',
|
||||
'5a94fe7769fb03452672e463',
|
||||
'5a24c314108439a4d4036148'
|
||||
];
|
||||
if (specialCases.includes(challenge.id)) return;
|
||||
if (lang === 'english') return;
|
||||
// If no .files, then no seed:
|
||||
if (!challenge.files) return;
|
||||
|
||||
// - None of the translatable comments should appear in the
|
||||
// translations. While this is a crude check, no challenges
|
||||
// currently have the text of a comment elsewhere. If that happens
|
||||
// we can handle that challenge separately.
|
||||
TRANSLATABLE_COMMENTS.forEach(comment => {
|
||||
Object.values(challenge.files).forEach(file => {
|
||||
if (file.contents.includes(comment))
|
||||
throw Error(
|
||||
`English comment '${comment}' should be replaced with its translation`
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// - None of the translated comment texts should appear *outside* a
|
||||
// comment
|
||||
Object.values(challenge.files).forEach(file => {
|
||||
let comments = {};
|
||||
|
||||
// We get all the actual comments using the appropriate parsers
|
||||
if (file.ext === 'html') {
|
||||
const commentTypes = ['css', 'html'];
|
||||
for (let type of commentTypes) {
|
||||
const newComments = commentExtractors[type](file.contents);
|
||||
for (const [key, value] of Object.entries(newComments)) {
|
||||
comments[key] = comments[key]
|
||||
? comments[key] + value
|
||||
: value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
comments = commentExtractors[file.ext](file.contents);
|
||||
}
|
||||
|
||||
// Then we compare the number of times a given comment appears
|
||||
// (count) with the number of times the text within it appears
|
||||
// (commentTextCount)
|
||||
for (const [comment, count] of Object.entries(comments)) {
|
||||
const commentTextCount =
|
||||
file.contents.split(comment).length - 1;
|
||||
if (commentTextCount !== count)
|
||||
throw Error(
|
||||
`Translated comment text, ${comment}, should only appear inside comments`
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const { challengeType } = challenge;
|
||||
if (
|
||||
challengeType !== challengeTypes.html &&
|
||||
|
Reference in New Issue
Block a user