Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compare_python_gen helper. #1236

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/seed_tools/commands/compare_python_gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2024 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

import { Command } from '@commander-js/extra-typings';
import { execSync } from 'child_process';
import { assert } from 'console';
import { existsSync, promises as fs } from 'fs';
import * as os from 'os';
import * as path from 'path';

export default function createCommand() {
return new Command('compare_python_gen')
.description(
'Run python and typescript seed generators and compare results',
)
.action(main);
}

async function main() {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'seed-compare-'));
const mockSerialNumber = 'mock_serial_number';

try {
const pythonSeedFilePath = path.join(tempDir, 'python_seed.bin');
const typescriptSeedFilePath = path.join(tempDir, 'typescript_seed.bin');
const pythonSeedSerialNumberFilePath = path.join(
tempDir,
'python_serialnumber',
);
const typescriptSeedSerialNumberFilePath = path.join(
tempDir,
'typescript_serialnumber',
);

// Run Python seed generator
execSync(
`python3 ./seed/serialize.py ./seed/seed.json --mock_serial_number ${mockSerialNumber}`,
{
stdio: 'inherit',
},
);
// Move generated seed.bin and serialnumber to temporary directory.
await moveFile('./seed.bin', pythonSeedFilePath);
await moveFile('./serialnumber', pythonSeedSerialNumberFilePath);

// Run TypeScript seed generator
execSync(
`npm run seed_tools create ./studies ${typescriptSeedFilePath} -- --mock_serial_number ${mockSerialNumber} --output_serial_number_file ${typescriptSeedSerialNumberFilePath}`,
{ stdio: 'inherit' },
);

// Run seed comparator
execSync(
`npm run seed_tools compare_seeds ${pythonSeedFilePath} ${typescriptSeedFilePath} --seed1_serialnumber_file ${pythonSeedSerialNumberFilePath} --seed2_serialnumber_file ${typescriptSeedSerialNumberFilePath}`,
{ stdio: 'inherit' },
);
} finally {
// Clean up temporary directory
await fs.rm(tempDir, { recursive: true, force: true });
}
}

async function moveFile(src: string, dest: string) {
await fs.copyFile(src, dest);
await fs.unlink(src);
assert(!existsSync(src));
}
48 changes: 40 additions & 8 deletions src/seed_tools/commands/compare_seeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ export default function createCommand() {
.description('Compare two seed.bin')
.argument('<seed1_file>', 'seed1 file')
.argument('<seed2_file>', 'seed2 file')
.option('--seed1_serialnumber_file <file>', 'seed1 serialnumber file')
.option('--seed2_serialnumber_file <file>', 'seed2 serialnumber file')
.action(main);
}

async function main(seed1FilePath: string, seed2FilePath: string) {
interface Options {
seed1_serialnumber_file?: string;
seed2_serialnumber_file?: string;
}

async function main(
seed1FilePath: string,
seed2FilePath: string,
options: Options,
) {
const seed1Binary: Buffer = await fs.readFile(seed1FilePath);
const seed2Binary: Buffer = await fs.readFile(seed2FilePath);
if (seed1Binary.equals(seed2Binary)) {
console.log('Seeds are equal');
process.exit(0);
}

const seed1Content = VariationsSeed.fromBinary(seed1Binary);
const seed2Content = VariationsSeed.fromBinary(seed2Binary);

Expand Down Expand Up @@ -52,9 +58,35 @@ async function main(seed1FilePath: string, seed2FilePath: string) {
seed2FilePath,
),
);
} else {
process.exit(1);
}

if (!seed1Binary.equals(seed2Binary)) {
console.error('Seeds semantically equal but binary different');
process.exit(1);
}

if (options.seed1_serialnumber_file !== undefined) {
const seed1Serialnumber: string = await fs.readFile(
options.seed1_serialnumber_file,
'utf8',
);
if (seed1Content.serial_number !== seed1Serialnumber) {
console.error('Seed1 serial number does not match');
process.exit(1);
}
}

if (options.seed2_serialnumber_file !== undefined) {
const seed2Serialnumber: string = await fs.readFile(
options.seed2_serialnumber_file,
'utf8',
);
if (seed2Content.serial_number !== seed2Serialnumber) {
console.error('Seed2 serial number does not match');
process.exit(1);
}
}

process.exit(1);
console.log('Seeds are equal');
}
33 changes: 29 additions & 4 deletions src/seed_tools/commands/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,35 @@ describe('create command', () => {
);
});

describe('serial number is equal in the seed and in the generated file', () => {
const validSeedsDir = path.join(testDataDir, 'valid_seeds');
it.each(fs_sync.readdirSync(validSeedsDir))(
'correctly creates %s',
async (testCase) => {
const testCaseDir = path.join(validSeedsDir, testCase);
const studiesDir = path.join(testCaseDir, 'studies');
const outputFile = path.join(tempDir, 'output.bin');
const serialNumberPath = path.join(tempDir, 'serial_number.txt');

await create().parseAsync([
'node',
'create',
studiesDir,
outputFile,
'--output_serial_number_file',
serialNumberPath,
]);

const output = await fs.readFile(outputFile);
const outputSerialNumber = await fs.readFile(serialNumberPath, 'utf-8');
expect(outputSerialNumber).not.toEqual('1');
expect(VariationsSeed.fromBinary(output).serial_number).toEqual(
outputSerialNumber,
);
},
);
});

describe('invalid studies', () => {
const invalidStudiesDir = path.join(testDataDir, 'invalid_studies');
it.each(fs_sync.readdirSync(invalidStudiesDir))(
Expand All @@ -117,8 +146,6 @@ describe('create command', () => {
'create',
studiesDir,
outputFile,
'--mock_serial_number',
'1',
'--output_serial_number_file',
serialNumberPath,
]),
Expand All @@ -143,8 +170,6 @@ describe('create command', () => {
'create',
studiesDir,
outputFile,
'--mock_serial_number',
'1',
'--output_serial_number_file',
serialNumberPath,
]),
Expand Down
2 changes: 2 additions & 0 deletions src/seed_tools/seed_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { program } from '@commander-js/extra-typings';

import compare_python_gen from './commands/compare_python_gen';
import compare_seeds from './commands/compare_seeds';
import create from './commands/create';
import lint from './commands/lint';
Expand All @@ -13,6 +14,7 @@ import split_seed_json from './commands/split_seed_json';
program
.name('seed_tools')
.description('Seed tools for manipulating study files.')
.addCommand(compare_python_gen())
.addCommand(compare_seeds())
.addCommand(create())
.addCommand(lint())
Expand Down