-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathgentest-driver.ts
More file actions
143 lines (126 loc) · 4.05 KB
/
gentest-driver.ts
File metadata and controls
143 lines (126 loc) · 4.05 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
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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import * as fs from 'node:fs/promises';
import {format} from 'node:util';
import {parse, dirname} from 'path';
import * as process from 'node:process';
import {Builder, logging} from 'selenium-webdriver';
import {Options} from 'selenium-webdriver/chrome.js';
import {fileURLToPath} from 'url';
import {stdin, stdout} from 'node:process';
import minimist from 'minimist';
import readline from 'node:readline/promises';
import signedsource from 'signedsource';
function addSignatureToSourceCode(sourceCode: string): string {
const codeWithToken = sourceCode.replace(
'MAGIC_PLACEHOLDER',
signedsource.getSigningToken(),
);
return signedsource.signFile(codeWithToken);
}
const argv = minimist(process.argv.slice(2));
const specificFixture = argv.f || argv.fixture;
const suspend = argv.s || argv.suspend;
const createWindow = argv.w || argv['create-window'];
const gentestDir = dirname(fileURLToPath(import.meta.url));
const yogaDir = dirname(gentestDir);
let fixtures = await fs.readdir(`${gentestDir}/fixtures`);
try {
if (specificFixture != null) {
await fs.access(`fixtures/${specificFixture}.html`, fs.constants.F_OK);
fixtures = [specificFixture + '.html'];
}
} catch (e) {
const errorMessage = e instanceof Error ? e.message : '';
console.log(
`Trying to access ${specificFixture}.html threw an exception. Executing against all fixtures. ${errorMessage}`,
);
}
const options = new Options();
options.addArguments(
'--force-device-scale-factor=1',
'--window-position=0,0',
'--hide-scrollbars',
);
if (!createWindow) {
options.addArguments('--headless');
}
options.setLoggingPrefs({
browser: 'ALL',
performance: 'ALL',
});
const driver = await new Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build();
for (const fileName of fixtures) {
const fixture = await fs.readFile(
`${gentestDir}/fixtures/${fileName}`,
'utf8',
);
const fileNameNoExtension = parse(fileName).name;
console.log('Generate', fileNameNoExtension);
// TODO: replace this with something more robust than just blindly replacing
// start/end in the entire fixture
const ltrFixture = fixture
.replaceAll('start', 'left')
.replaceAll('end', 'right')
.replaceAll('flex-left', 'flex-start')
.replaceAll('flex-right', 'flex-end');
const rtlFixture = fixture
.replaceAll('start', 'right')
.replaceAll('end', 'left')
.replaceAll('flex-right', 'flex-start')
.replaceAll('flex-left', 'flex-end');
const template = await fs.readFile(
`${gentestDir}/test-template.html`,
'utf8',
);
const f = await fs.open(`${gentestDir}/test.html`, 'w');
await f.write(
format(template, fileNameNoExtension, ltrFixture, rtlFixture, fixture),
);
await f.close();
await driver.get('file://' + process.cwd() + '/test.html');
const logs = await driver.manage().logs().get(logging.Type.BROWSER);
const testLogs = logs.filter(
log => !log.message.replace(/^[^"]*/, '').startsWith('"gentest-log:'),
);
await fs.writeFile(
`${yogaDir}/tests/generated/${fileNameNoExtension}.cpp`,
addSignatureToSourceCode(
JSON.parse(testLogs[0].message.replace(/^[^"]*/, '')),
),
);
await fs.writeFile(
`${yogaDir}/java/tests/generated/com/facebook/yoga/${fileNameNoExtension}.java`,
addSignatureToSourceCode(
JSON.parse(testLogs[1].message.replace(/^[^"]*/, '')).replace(
'YogaTest',
fileNameNoExtension,
),
),
);
await fs.writeFile(
`${yogaDir}/javascript/tests/generated/${fileNameNoExtension}.test.ts`,
addSignatureToSourceCode(
JSON.parse(testLogs[2].message.replace(/^[^"]*/, '')).replace(
'YogaTest',
fileNameNoExtension,
),
),
);
if (suspend) {
const rl = readline.createInterface({input: stdin, output: stdout});
await rl.question('');
rl.close();
}
}
await fs.unlink(`${gentestDir}/test.html`);
await driver.quit();