-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.config.js
196 lines (180 loc) · 6.16 KB
/
tests.config.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Script to setup tests.
import * as fs from 'fs';
import * as path from 'path';
import * as cp from 'child_process';
const indir = 'expected/';
const jsondir = 'ts/json/';
const actionsdir = 'ts/actions/'; // To run tests as github actions.
const alldir = 'ts/output/'; // To run tests with output.
const analysedir = 'ts/analyse/'; // To run tests with analytics.
const analysisdir = 'analysis';
const allAnalyse = [];
const allOutputs = {};
let addAllOutputs = function(out, dir) {
if (!allOutputs[out]) {
allOutputs[out] = [];
}
allOutputs[out].push(dir);
};
/**
* Recursively find all files with .json extension under the given path.
* @param dir The top pathname.
* @param result Accumulator for pathnames.
*/
let readDir = function(dir, result) {
if (typeof dir === 'undefined') {
return;
}
let file = indir + dir;
if (fs.lstatSync(file).isDirectory()) {
let files = fs.readdirSync(file);
files.forEach(
x => readDir(dir ? dir + path.sep + x : x, result));
return;
}
if (dir.match(/\.json$/)) {
let grep = cp.spawnSync('grep', ['"active"', file]);
if (!grep.status) {
addAllOutputs(
grep.stdout.toString()
.match(/\"active\": *\"(.*)\"/)[1],
file);
}
grep = cp.spawnSync('grep', ['"name"', file]);
if (!grep.status) {
allAnalyse.push(dir);
// allAnalyse.set(
// grep.stdout.toString()
// .match(/\"name\": *\"(.*)\"/)[1], dir);
}
result.push(dir);
}
};
let createFile = function(dir, file, content) {
fs.mkdirSync(dir, {recursive: true});
fs.writeFileSync(dir + path.sep + file, content.join('\n'));
};
let createJsonTests = function(files) {
for (let file of files) {
let dir = path.dirname(file);
let depth = dir.match(/\//g);
let base = Array((depth ? depth.length : 0) + 3).join('../');
let content = [];
content.push(`import {ExampleFiles} from '${base}classes/abstract_examples.js';`);
content.push(`import {runJsonTest} from '${base}jest.js';`);
content.push('ExampleFiles.noOutput = true;');
content.push(`runJsonTest('${file}');`);
content.push(``);
createFile(jsondir + dir, path.basename(file).replace(/.json$/, '.test.ts'), content);
}
};
let createActionTests = function(files) {
let metaFiles = {};
for (let file of files) {
let dir = file.split(path.sep)[0];
if (!metaFiles[dir]) {
metaFiles[dir] = [];
}
metaFiles[dir].push(file);
}
for (let [dir, files] of Object.entries(metaFiles)) {
let filename = `${dir}.test.ts`;
let content = [];
content.push(`import {ExampleFiles} from '../classes/abstract_examples.js';`);
content.push(`import {runJsonTest} from '../jest.js';`);
content.push('ExampleFiles.noOutput = true;');
files.forEach(x => content.push(`runJsonTest('${x}');`));
content.push(``);
createFile(actionsdir, filename, content);
}
};
let createOutputTests = function() {
for (let file of Object.keys(allOutputs)) {
let files = allOutputs[file];
let content = [];
content.push(`import {ExampleFiles} from '../classes/abstract_examples.js';`);
content.push(`import {runJsonTest} from '../jest.js';`);
content.push('afterAll(() => {\n ExampleFiles.closeFiles();\n});');
files.forEach(x => content.push(`runJsonTest('${x}');`));
content.push(``);
createFile(alldir, file + '.test.ts', content);
}
};
let createAnalyseTests = function() {
for (let file of allAnalyse) {
let dir = path.dirname(file);
let depth = dir.match(/\//g);
let base = Array((depth ? depth.length : 0) + 3).join('../');
let content = [];
content.push(`import AnalyticsTest from '${base}analytics/analytics_test.js';`);
content.push(`import {ExampleFiles} from '${base}classes/abstract_examples.js';`);
content.push(`import {runJsonTest} from '${base}jest.js';`);
content.push('AnalyticsTest.deep = true;');
content.push('ExampleFiles.noOutput = true;');
content.push('afterAll(() => {\n AnalyticsTest.output();\n});');
content.push(`runJsonTest('${file}');`);
content.push(``);
createFile(analysedir + dir, path.basename(file).replace(/.json$/, '.test.ts'), content);
}
};
let createFiles = function() {
let files = [];
readDir('', files);
createJsonTests(files);
createActionTests(files);
createOutputTests();
createAnalyseTests(files);
};
let createHtmlFiles = function() {
let languages = {};
let tables = {};
for (let key of Object.keys(allOutputs)) {
let language = key.match(/^DefaultSymbols(.*)/);
if (!language || !language[1]) continue;
languages[language[1]] = allOutputs[key][0].split(path.sep)[1];
tables[language[1]] = {};
}
for (let [language, iso] of Object.entries(languages)) {
let output = Object.keys(allOutputs).filter(x => x.match(language));
for (let file of output) {
let type = allOutputs[file][0].split(path.sep)[2];
if (tables[language][type]) {
tables[language][type].push(file);
} else {
tables[language][type] = [file];
}
}
}
let str = '<h1>Locales</h1>\n<ul>\n';
let langKeys = Object.keys(tables).sort();
langKeys.forEach(lang => str += `<li><a href="#${lang}">${lang}</a> (${languages[lang]})</li>\n`);
str += '</ul>\n';
let cap = x => x[0].toUpperCase() + x.slice(1);
for (let language of langKeys) {
let iso = languages[language];
str += `<h2 id="${language}">${language}</h2>\n`;
str += '<table border="2">\n';
for (let [type, files] of Object.entries(tables[language])) {
str += `<tr><th>${cap(type)}</th></tr>\n`;
for (let file of files) {
str += `<tr><td><a href="${iso}${path.sep}${file}.html">${file}</a></td></tr>\n`;
}
}
str += '</table>';
str += '\n\n';
}
fs.mkdirSync('output', { recursive: true });
fs.writeFileSync('output/index.html',
'<!DOCTYPE html>\n<html>\n<body>\n' + str+ '</body>\n</html>\n');
};
export let cleanFiles = function() {
fs.rmSync(jsondir, {force: true, recursive: true});
fs.rmSync(alldir, {force: true, recursive: true});
fs.rmSync(analysedir, {force: true, recursive: true});
};
export let build = function() {
cleanFiles();
createFiles();
createHtmlFiles();
};
build();