forked from wet-boew/cdts-sgdc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestFileGenerator.js
More file actions
79 lines (60 loc) · 3.85 KB
/
Copy pathTestFileGenerator.js
File metadata and controls
79 lines (60 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const fs = require('fs');
const { HtmlValidate, formatterFactory } = require('html-validate'); //(https://html-validate.org/dev/using-api.html)
const wetBuilderRegex = /wet\.builder\..*\({.*}\)/gm; //NOTE: Relies on the fact that there are no spaces between '('+'{' and '}'+')', e.g. wet.builder.function({...})
const wetLanguageRegex = /\/wet-(.*)\.js/;
let warningIssued = false;
function validateBuilderFunctions(content, theme, version) {
const htmlValidate = new HtmlValidate(require('./htmlvalidator.conf.js')); //config path relative
const htmlValidateFormatReport = formatterFactory('stylish'); //possible formatters: checkstyle, codeframe, json, stylish, text
//---[ Extract language from the "wet-??.js" files found in data
const language = (content.match(wetLanguageRegex) || [null, 'en'])[1] || 'en';
const distCompiledDirName = `./dist/app/cls/WET/${theme}/${version}/cdts/compiled`;
const wetFileName = `wet-${language}.js`;
//---[ Extract all the "wet.builder" calls out of the page content
const functionCalls = content.match(wetBuilderRegex);
if (functionCalls.length <= 0) return; //don't bother if the content does not include any wet.builder call
//---[ Mock global variable available in browsers and needed by wet-[en|fr].js
//NOTE: this is the navigator language, always setting to en-CA should be ok... right?
const navigator = {language: 'en-CA',}; //eslint-disable-line
//---[ Load soy/wet functions
//NOTE: Using eval on arbritrary files is a huge NO-NO, but we just generated these files and trust them
// (not to mention that they are not modules so require/import does not work with them)
eval(fs.readFileSync(`${distCompiledDirName}/soyutils.js`, 'utf8')); //eslint-disable-line
eval(fs.readFileSync(`${distCompiledDirName}/${wetFileName}`, 'utf8')); //eslint-disable-line
//---[ For each call in content: validate the html it generates
console.log(`***** Validating ${functionCalls.length} functions for ${wetFileName}...`);
for (let i=0; i<functionCalls.length; i++) {
const functionName = functionCalls[i].match(/(wet\.builder\..*)\(/)[1];
const output = eval(functionCalls[i]).toString(); //eslint-disable-line
console.log(`***** Function [${functionName}]; length=${output.length}`);
//---[ Validate HTML
const report = htmlValidate.validateString(output);
if ((!report.valid) || (report.warningCount > 0)) {
console.error(`${functionName}: ${report.errorCount} error(s), ${report.warningCount} warning(s) reported:`);
console.error(htmlValidateFormatReport(report.results));
if (report.errorCount > 0) {
console.error(`${functionName} content: [${output}]`);
throw new Error('HTML validator error reported, aborting.');
}
}
}
console.log('***************');
}
module.exports = function generateTestFile(inputFilePath, theme, outputFileName, sections) {
const version = process.env.CDTS_TEST_VERSION_NAME || 'v4_0_40';
const filePath = `./dist/app/cls/WET/${theme}/${version}/cdts/test/${outputFileName}.html`;
if (fs.existsSync(filePath)) {
if (!warningIssued) {
console.warn(`***** WARNING ***** Test file ${outputFileName}.html already exists. Skipping generation of this file and subsequent warnings will be suppressed.`);
warningIssued = true;
}
return;
}
let data = fs.readFileSync(inputFilePath, 'utf8');
for (let i=0; i<Object.keys(sections).length; i++) {
data = data.replace('"~' + Object.keys(sections)[i] + '~"', sections[Object.keys(sections)[i]]);
}
//---[ Before writing data to disk, validate the output of the various 'wet.builder.*' functions.
validateBuilderFunctions(data, theme, version);
fs.writeFileSync(filePath, data, 'utf8');
}