UNPKG

4.5 kBPlain TextView Raw
1import path, {dirname} from 'path';
2import fs from 'fs';
3import glob from 'glob';
4import localizeURLs from '../test/integration/lib/localize-urls';
5import {fileURLToPath} from 'url';
6import {createRequire} from 'module';
7
8const __dirname = dirname(fileURLToPath(import.meta.url));
9const requireFn = createRequire(import.meta.url);
10const OUTPUT_FILE = 'fixtures.json';
11const rootFixturePath = 'test/integration/query/';
12const suitePath = 'tests';
13
14/**
15 * Analyzes the contents of the specified `path.join(rootDirectory, suiteDirectory)`, and inlines
16 * the contents into a single json file which can then be imported and built into a bundle
17 * to be shipped to the browser.
18 * @param rootDirectory - The root directory
19 * @param suiteDirectory - The suite directory
20 * @param outputDirectory - The output directory
21 * @param includeImages - Flag to include images
22 */
23function 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 //Extract the filedata into a flat dictionary
28 const allFiles = {};
29 let allPaths = glob.sync(jsonPaths);
30 if (includeImages) {
31 allPaths = allPaths.concat(glob.sync(imagePaths));
32 }
33
34 //A Set that stores test names that are malformed so they can be removed later
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 //Special case for style json which needs some preprocessing
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 // Re-nest by directory path, each directory path defines a testcase.
63 const result = {};
64 for (const fullPath in allFiles) {
65 const testName = path.dirname(fullPath).replace(rootDirectory, '');
66 //Skip if test is malformed
67 if (malformedTests[testName]) { continue; }
68
69 //Lazily initaialize an object to store each file wihin a particular testName
70 if (result[testName] == null) {
71 result[testName] = {};
72 }
73 //Trim extension from filename
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
90function 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
98function parseJsonFromFile(filePath) {
99 return JSON.parse(fs.readFileSync(filePath, {encoding: 'utf8'}));
100}
101
102function pngToBase64Str(filePath) {
103 return fs.readFileSync(filePath).toString('base64');
104}
105
106function 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
125await generateFixtureJson(rootFixturePath, suitePath);
126
\No newline at end of file