Create node version of gentest script (#1498)
Summary: Pull Request resolved: https://github.com/facebook/yoga/pull/1498 The only instance of ruby in this repository is `gentest.rb` used to generate test cases from html fixtures. This is quite annoying as ruby is not the most popular compared to something like Node and it does not integrate into the rest of our stack. I changed this to use Node.js instead. Instead of `watir` we now use `selenium-webdriver`. `watir` is backed by Selenium so I do not expect anything to change. Next commits will add command line options, clean up gentest.rb and its references, and change the README allow-large-files Reviewed By: yungsters, NickGerleman Differential Revision: D51874433 fbshipit-source-id: ef8588d48aa7f8b720b57df08738bbd01e9e74a3
This commit is contained in:
committed by
Facebook GitHub Bot
parent
43cb24fdce
commit
98552078b1
12
gentest/babel.config.cjs
Normal file
12
gentest/babel.config.cjs
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
presets: ['@babel/preset-typescript'],
|
||||
};
|
94
gentest/gentest-driver.ts
Normal file
94
gentest/gentest-driver.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* 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';
|
||||
|
||||
const options = new Options();
|
||||
options.addArguments(
|
||||
'--force-device-scale-factor=1',
|
||||
'--window-position=0,0',
|
||||
'--hide-scrollbars',
|
||||
);
|
||||
options.setLoggingPrefs({
|
||||
browser: 'ALL',
|
||||
performance: 'ALL',
|
||||
});
|
||||
const driver = await new Builder()
|
||||
.forBrowser('chrome')
|
||||
.setChromeOptions(options)
|
||||
.build();
|
||||
|
||||
const gentestDir = dirname(fileURLToPath(import.meta.url));
|
||||
const yogaDir = dirname(gentestDir);
|
||||
|
||||
const fixtures = await fs.readdir(`${gentestDir}/fixtures`);
|
||||
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);
|
||||
|
||||
await fs.writeFile(
|
||||
`${yogaDir}/tests/generated/${fileNameNoExtension}.cpp`,
|
||||
JSON.parse(logs[0].message.replace(/^[^"]*/, '')),
|
||||
);
|
||||
|
||||
await fs.writeFile(
|
||||
`${yogaDir}/java/tests/com/facebook/yoga/${fileNameNoExtension}.java`,
|
||||
JSON.parse(logs[1].message.replace(/^[^"]*/, '')).replace(
|
||||
'YogaTest',
|
||||
fileNameNoExtension,
|
||||
),
|
||||
);
|
||||
|
||||
await fs.writeFile(
|
||||
`${yogaDir}/javascript/tests/generated/${fileNameNoExtension}.test.ts`,
|
||||
JSON.parse(logs[2].message.replace(/^[^"]*/, '')).replace(
|
||||
'YogaTest',
|
||||
fileNameNoExtension,
|
||||
),
|
||||
);
|
||||
}
|
||||
await fs.unlink(`${gentestDir}/test.html`);
|
||||
await driver.quit();
|
20
gentest/package.json
Normal file
20
gentest/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "gentest",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"gentest": "node --loader=babel-register-esm ./gentest-driver.ts"
|
||||
},
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"selenium-webdriver": "^4.16.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.10.3",
|
||||
"@types/selenium-webdriver": "^4.1.21",
|
||||
"babel-register-esm": "^1.2.5",
|
||||
"@babel/core": "^7.23.0",
|
||||
"@babel/preset-typescript": "^7.23.0",
|
||||
"@tsconfig/node18": "^18.2.2"
|
||||
}
|
||||
}
|
6
gentest/tsconfig.json
Normal file
6
gentest/tsconfig.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "@tsconfig/node18/tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"noEmit": true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user