|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Generates test files for integration rule tests in tmp/integration-tests/. |
| 5 | + * This replaces the Karma preprocessor (test/integration/rules/preprocessor.js) |
| 6 | + * that combined *.json + *.html pairs into executable JS at serve-time. |
| 7 | + * |
| 8 | + * Output files are picked up by web-test-runner via the test:unit:integration script. |
| 9 | + */ |
| 10 | + |
| 11 | +const path = require('path'); |
| 12 | +const fs = require('fs'); |
| 13 | +const { globSync } = require('glob'); |
| 14 | + |
| 15 | +const rootDir = path.join(__dirname, '..'); |
| 16 | +const rulesDir = path.join(rootDir, 'test', 'integration', 'rules'); |
| 17 | +const outDir = path.join(rootDir, 'tmp', 'integration-tests'); |
| 18 | +const runnerTemplate = fs.readFileSync( |
| 19 | + path.join(rulesDir, 'runner.js'), |
| 20 | + 'utf-8' |
| 21 | +); |
| 22 | + |
| 23 | +// Clean and recreate output directory |
| 24 | +fs.rmSync(outDir, { recursive: true, force: true }); |
| 25 | +fs.mkdirSync(outDir, { recursive: true }); |
| 26 | + |
| 27 | +const jsonFiles = globSync('**/*.json', { cwd: rulesDir }); |
| 28 | + |
| 29 | +let count = 0; |
| 30 | +for (const relPath of jsonFiles) { |
| 31 | + const jsonPath = path.join(rulesDir, relPath); |
| 32 | + const htmlPath = jsonPath.replace(/\.json$/, '.html'); |
| 33 | + |
| 34 | + if (!fs.existsSync(htmlPath)) { |
| 35 | + // Some JSON files may not have a sibling HTML (e.g. nested frame fixtures) |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + let test; |
| 40 | + try { |
| 41 | + test = JSON.parse(fs.readFileSync(jsonPath, 'utf-8')); |
| 42 | + } catch (e) { |
| 43 | + throw new Error(`Unable to parse ${jsonPath}: ${e.message}`); |
| 44 | + } |
| 45 | + |
| 46 | + const html = fs.readFileSync(htmlPath, 'utf-8'); |
| 47 | + test.content = html; |
| 48 | + |
| 49 | + const outPath = path.join(outDir, relPath.replace(/\.json$/, '.test.js')); |
| 50 | + const outDirForFile = path.dirname(outPath); |
| 51 | + fs.mkdirSync(outDirForFile, { recursive: true }); |
| 52 | + |
| 53 | + const output = runnerTemplate.replace('{}; /*tests*/', JSON.stringify(test)); |
| 54 | + fs.writeFileSync(outPath, output, 'utf-8'); |
| 55 | + count++; |
| 56 | +} |
| 57 | + |
| 58 | +console.log( |
| 59 | + `Generated ${count} integration test files in tmp/integration-tests/` |
| 60 | +); |
0 commit comments