forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-integration-tests.js
More file actions
57 lines (45 loc) · 1.64 KB
/
Copy pathgenerate-integration-tests.js
File metadata and controls
57 lines (45 loc) · 1.64 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
'use strict';
/**
* Generates executable JS from integration rule *.json + *.html pairs in
* tmp/integration-tests/. Picked up by web-test-runner via test:unit:integration.
*/
const path = require('path');
const fs = require('fs');
const { globSync } = require('glob');
const rootDir = path.join(__dirname, '..');
const rulesDir = path.join(rootDir, 'test', 'integration', 'rules');
const outDir = path.join(rootDir, 'tmp', 'integration-tests');
const runnerTemplate = fs.readFileSync(
path.join(rulesDir, 'runner.js'),
'utf-8'
);
// Clean and recreate output directory
fs.rmSync(outDir, { recursive: true, force: true });
fs.mkdirSync(outDir, { recursive: true });
const jsonFiles = globSync('**/*.json', { cwd: rulesDir });
let count = 0;
for (const relPath of jsonFiles) {
const jsonPath = path.join(rulesDir, relPath);
const htmlPath = jsonPath.replace(/\.json$/, '.html');
if (!fs.existsSync(htmlPath)) {
// Some JSON files may not have a sibling HTML (e.g. nested frame fixtures)
continue;
}
let test;
try {
test = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));
} catch (e) {
throw new Error(`Unable to parse ${jsonPath}: ${e.message}`);
}
const html = fs.readFileSync(htmlPath, 'utf-8');
test.content = html;
const outPath = path.join(outDir, relPath.replace(/\.json$/, '.test.js'));
const outDirForFile = path.dirname(outPath);
fs.mkdirSync(outDirForFile, { recursive: true });
const output = runnerTemplate.replace('{}; /*tests*/', JSON.stringify(test));
fs.writeFileSync(outPath, output, 'utf-8');
count++;
}
console.log(
`Generated ${count} integration test files in tmp/integration-tests/`
);