Skip to content

Commit

Permalink
Use git diff instead of diff package. Too much dependencies for a sim…
Browse files Browse the repository at this point in the history
…ple task.
  • Loading branch information
goodov committed Jul 3, 2024
1 parent cafd56c commit 2d67c37
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/seed_tools/commands/check_study.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// You can obtain one at https://mozilla.org/MPL/2.0/.

import { Command } from '@commander-js/extra-typings';
import * as diff from 'diff';
import { promises as fs } from 'fs';
import DefaultMap from 'src/base/containers/default_map';
import { type Study } from '../../proto/generated/study';
import diffStrings from '../utils/diff_strings';
import * as study_json_utils from '../utils/study_json_utils';
import * as study_validation from '../utils/study_validation';

Expand Down Expand Up @@ -76,12 +76,12 @@ async function checkAndOptionallyFixFormat(
} else {
errors.push(
`Format required:\n` +
diff.createTwoFilesPatch(
studyFilePath,
studyFilePath + '.formatted',
(await diffStrings(
studyArrayString,
stringifiedStudyArray,
),
studyFilePath,
studyFilePath + '.formatted',
)),
);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/seed_tools/commands/compare_seeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
// You can obtain one at https://mozilla.org/MPL/2.0/.

import { Command } from '@commander-js/extra-typings';
import * as diff from 'diff';
import { promises as fs } from 'fs';
import { VariationsSeed } from '../../proto/generated/variations_seed';
import diffStrings from '../utils/diff_strings';

export default new Command('compare_seeds')
.description('Compare two seed.bin')
.argument('<seed1_file>', 'seed1 file')
.argument('<seed2_file>', 'seed2 file')
.action(main);

async function main(seed1File: string, seed2File: string) {
const seed1Binary: Buffer = await fs.readFile(seed1File);
const seed2Binary: Buffer = await fs.readFile(seed2File);
async function main(seed1FilePath: string, seed2FilePath: string) {
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);
Expand All @@ -43,11 +43,11 @@ async function main(seed1File: string, seed2File: string) {
if (seed1JsonString !== seed2JsonString) {
console.error('Seeds are different');
console.error(
diff.createTwoFilesPatch(
seed1File,
seed2File,
await diffStrings(
seed1JsonString,
seed2JsonString,
seed1FilePath,
seed2FilePath,
),
);
} else {
Expand Down
25 changes: 25 additions & 0 deletions src/seed_tools/utils/diff_strings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 diffStrings from './diff_strings';

describe('diffStrings', () => {
it('should return the diff between two strings', async () => {
const string1 = 'Hello, world!';
const string2 = 'Hello, brave!';
const displayFileName1 = 'file1.txt';
const displayFileName2 = 'file2.txt';

const result = await diffStrings(
string1,
string2,
displayFileName1,
displayFileName2,
);

expect(result).toContain('-Hello, world!');
expect(result).toContain('+Hello, brave!');
});
});
56 changes: 56 additions & 0 deletions src/seed_tools/utils/diff_strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 { exec } from 'child_process';
import fs from 'fs/promises';
import os from 'os';
import path from 'path';

export default async function diffStrings(
string1: string,
string2: string,
displayFileName1: string,
displayFileName2: string,
): Promise<string> {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'diffString-'));

const tmpFile1 = path
.join(tmpDir, `file1-${Date.now()}.txt`)
.replaceAll('\\', '/');
const tmpFile2 = path
.join(tmpDir, `file2-${Date.now()}.txt`)
.replaceAll('\\', '/');

// Write strings to temporary files.
await fs.writeFile(tmpFile1, string1);
await fs.writeFile(tmpFile2, string2);

return await new Promise<string>((resolve, reject) => {
// Use git diff to generate readable diff.
exec(
`git diff --no-index --src-prefix= --dst-prefix= -- ${tmpFile1} ${tmpFile2}`,
{ encoding: 'utf8' },
(error, stdout, stderr) => {
if (error !== null && error.code !== 1) {
// git diff returns 1 if there are differences
reject(new Error(stderr));
return;
}

const result = stdout
.replaceAll(tmpFile1, displayFileName1)
.replaceAll(tmpFile2, displayFileName2);

resolve(result);
},
);
}).finally(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
// Clean up temporary directory.
await fs.rm(tmpDir, { recursive: true });
})();
});
}
8 changes: 2 additions & 6 deletions src/test/data/unformatted_studies/test1/expected_output.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@@ -6,9 +6,9 @@
"name": "Enabled",
@@ -7,7 +7,7 @@
"probability_weight": 100,
"feature_association": {
"enable_feature": [
Expand All @@ -8,9 +7,7 @@
]
}
},
{
@@ -20,21 +20,21 @@
]
@@ -21,19 +21,19 @@
}
},
{
Expand All @@ -34,4 +31,3 @@
"PLATFORM_MAC",
"PLATFORM_LINUX",
"PLATFORM_ANDROID"
]

0 comments on commit 2d67c37

Please sign in to comment.