1 | import path, {dirname} from 'path';
|
2 | import fs from 'fs';
|
3 | import glob from 'glob';
|
4 | import localizeURLs from '../test/integration/lib/localize-urls';
|
5 | import {fileURLToPath} from 'url';
|
6 | import {createRequire} from 'module';
|
7 |
|
8 | const __dirname = dirname(fileURLToPath(import.meta.url));
|
9 | const requireFn = createRequire(import.meta.url);
|
10 | const OUTPUT_FILE = 'fixtures.json';
|
11 | const rootFixturePath = 'test/integration/query/';
|
12 | const suitePath = 'tests';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function generateFixtureJson(rootDirectory: string, suiteDirectory: string, outputDirectory: string = 'test/integration/query/dist', includeImages:boolean = false) {
|
24 | const globs = getAllFixtureGlobs(rootDirectory, suiteDirectory);
|
25 | const jsonPaths = globs[0];
|
26 | const imagePaths = globs[1];
|
27 |
|
28 | const allFiles = {};
|
29 | let allPaths = glob.sync(jsonPaths);
|
30 | if (includeImages) {
|
31 | allPaths = allPaths.concat(glob.sync(imagePaths));
|
32 | }
|
33 |
|
34 |
|
35 | const malformedTests = {};
|
36 |
|
37 | for (const fixturePath of allPaths) {
|
38 | const testName = path.dirname(fixturePath);
|
39 | const fileName = path.basename(fixturePath);
|
40 | const extension = path.extname(fixturePath);
|
41 | try {
|
42 | if (extension === '.json') {
|
43 | let json = parseJsonFromFile(fixturePath);
|
44 |
|
45 |
|
46 | if (fileName === 'style.json') {
|
47 | json = processStyle(testName, json);
|
48 | }
|
49 |
|
50 | allFiles[fixturePath] = json;
|
51 | } else if (extension === '.png') {
|
52 | allFiles[fixturePath] = pngToBase64Str(fixturePath);
|
53 | } else {
|
54 | throw new Error(`${extension} is incompatible , file path ${fixturePath}`);
|
55 | }
|
56 | } catch (e) {
|
57 | console.log(`Error parsing file: ${fixturePath} ${e.message}`);
|
58 | malformedTests[testName] = true;
|
59 | }
|
60 | }
|
61 |
|
62 |
|
63 | const result = {};
|
64 | for (const fullPath in allFiles) {
|
65 | const testName = path.dirname(fullPath).replace(rootDirectory, '');
|
66 |
|
67 | if (malformedTests[testName]) { continue; }
|
68 |
|
69 |
|
70 | if (result[testName] == null) {
|
71 | result[testName] = {};
|
72 | }
|
73 |
|
74 | const fileName = path.basename(fullPath, path.extname(fullPath));
|
75 | result[testName][fileName] = allFiles[fullPath];
|
76 | }
|
77 |
|
78 | const outputStr = JSON.stringify(result, null, 4);
|
79 | const outputPath = path.join(outputDirectory, OUTPUT_FILE);
|
80 |
|
81 | return new Promise<void>((resolve, reject) => {
|
82 | fs.writeFile(outputPath, outputStr, {encoding: 'utf8'}, (err) => {
|
83 | if (err) { reject(err); }
|
84 |
|
85 | resolve();
|
86 | });
|
87 | });
|
88 | }
|
89 |
|
90 | function getAllFixtureGlobs(rootDirectory, suiteDirectory) {
|
91 | const basePath = path.join(rootDirectory, suiteDirectory);
|
92 | const jsonPaths = path.join(basePath, '*.json');
|
93 | const imagePaths = path.join(basePath, '*.png');
|
94 |
|
95 | return [jsonPaths, imagePaths];
|
96 | }
|
97 |
|
98 | function parseJsonFromFile(filePath) {
|
99 | return JSON.parse(fs.readFileSync(filePath, {encoding: 'utf8'}));
|
100 | }
|
101 |
|
102 | function pngToBase64Str(filePath) {
|
103 | return fs.readFileSync(filePath).toString('base64');
|
104 | }
|
105 |
|
106 | function processStyle(testName, style) {
|
107 | const clone = JSON.parse(JSON.stringify(style));
|
108 | localizeURLs(clone, 7357, path.join(__dirname, '../test/integration'), requireFn);
|
109 |
|
110 | clone.metadata = clone.metadata || {};
|
111 |
|
112 | // eslint-disable-next-line no-restricted-properties
|
113 | clone.metadata.test = Object.assign({
|
114 | testName,
|
115 | width: 512,
|
116 | height: 512,
|
117 | pixelRatio: 1,
|
118 | recycleMap: false,
|
119 | allowed: 0.00015
|
120 | }, clone.metadata.test);
|
121 |
|
122 | return clone;
|
123 | }
|
124 | // @ts-ignore
|
125 | await generateFixtureJson(rootFixturePath, suitePath);
|
126 |
|
\ | No newline at end of file |