Fixup gentest-validate when directories have folders

Summary: This reworks `gentest-validate`, to be more resilient to changes like D58307002, which cause [validation failures](https://github.com/facebook/yoga/actions/runs/9653979292/job/26627690870) due to the file traversal code not handling the presence of directories.

Differential Revision: D58987293
This commit is contained in:
Nick Gerleman
2024-06-24 21:10:25 -07:00
committed by Facebook GitHub Bot
parent db383714d3
commit cedac5edf4
7 changed files with 120 additions and 60 deletions

View File

@@ -11,30 +11,37 @@ import * as fs from 'node:fs/promises';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
import signedsource from 'signedsource';
import {glob} from 'glob';
const yogaDir = dirname(dirname(fileURLToPath(import.meta.url)));
const cppTestDir = `${yogaDir}/tests/generated`;
const jsTestDir = `${yogaDir}/javascript/tests/generated`;
const javaTestDir = `${yogaDir}/java/tests/com/facebook/yoga`;
const testDirs = [cppTestDir, jsTestDir, javaTestDir];
const yogaRootDir = dirname(dirname(fileURLToPath(import.meta.url)));
for (const testDir of testDirs) {
const tests = await fs.readdir(testDir);
const signedSourcePattern = new RegExp(
`${'@'}generated (?:SignedSource<<([a-f0-9]{32})>>)`,
);
for (const test of tests) {
const testData = await fs.readFile(`${testDir}/${test}`, 'utf8');
try {
const validSignature = signedsource.verifySignature(testData);
if (!validSignature) {
console.error(`Invalid signature for ${test}`);
process.exitCode = 1;
}
} catch (e) {
// Java test dir does not separate generated tests from non-generated ones
if (testDir != javaTestDir) {
console.error(`${test}: ${e}`);
process.exitCode = 1;
}
const filesToValidate = await glob(
[
'tests/generated/**/*.{h,cpp}',
'javascript/tests/generated/**/*.test.ts',
'java/tests/com/facebook/yoga/**/*.java',
],
{
cwd: yogaRootDir,
},
);
console.log(`Found ${filesToValidate.length} files to validate`);
for (const file of filesToValidate) {
const content = await fs.readFile(`${yogaRootDir}/${file}`, 'utf8');
if (signedSourcePattern.test(content)) {
console.log(`Checking ${file}`);
const validSignature = signedsource.verifySignature(content);
if (!validSignature) {
console.error(`Invalid signature "${file}"`);
process.exitCode = 1;
}
} else {
console.log(`Skipped ${file}`);
}
}