From 2d67c3704764e0bad7d27237194854dff48df00f Mon Sep 17 00:00:00 2001 From: Aleksey Khoroshilov Date: Wed, 3 Jul 2024 15:04:18 +0700 Subject: [PATCH] Use git diff instead of diff package. Too much dependencies for a simple task. --- src/seed_tools/commands/check_study.ts | 10 ++-- src/seed_tools/commands/compare_seeds.ts | 14 ++--- src/seed_tools/utils/diff_strings.test.ts | 25 +++++++++ src/seed_tools/utils/diff_strings.ts | 56 +++++++++++++++++++ .../test1/expected_output.txt | 8 +-- 5 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 src/seed_tools/utils/diff_strings.test.ts create mode 100644 src/seed_tools/utils/diff_strings.ts diff --git a/src/seed_tools/commands/check_study.ts b/src/seed_tools/commands/check_study.ts index 1dba956e..1f22c16d 100644 --- a/src/seed_tools/commands/check_study.ts +++ b/src/seed_tools/commands/check_study.ts @@ -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'; @@ -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', + )), ); } } diff --git a/src/seed_tools/commands/compare_seeds.ts b/src/seed_tools/commands/compare_seeds.ts index fddd769a..4f2bba52 100644 --- a/src/seed_tools/commands/compare_seeds.ts +++ b/src/seed_tools/commands/compare_seeds.ts @@ -4,9 +4,9 @@ // 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') @@ -14,9 +14,9 @@ export default new Command('compare_seeds') .argument('', '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); @@ -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 { diff --git a/src/seed_tools/utils/diff_strings.test.ts b/src/seed_tools/utils/diff_strings.test.ts new file mode 100644 index 00000000..3406c86f --- /dev/null +++ b/src/seed_tools/utils/diff_strings.test.ts @@ -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!'); + }); +}); diff --git a/src/seed_tools/utils/diff_strings.ts b/src/seed_tools/utils/diff_strings.ts new file mode 100644 index 00000000..802cee79 --- /dev/null +++ b/src/seed_tools/utils/diff_strings.ts @@ -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 { + 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((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 }); + })(); + }); +} diff --git a/src/test/data/unformatted_studies/test1/expected_output.txt b/src/test/data/unformatted_studies/test1/expected_output.txt index b69a5193..42cf9421 100644 --- a/src/test/data/unformatted_studies/test1/expected_output.txt +++ b/src/test/data/unformatted_studies/test1/expected_output.txt @@ -1,5 +1,4 @@ -@@ -6,9 +6,9 @@ - "name": "Enabled", +@@ -7,7 +7,7 @@ "probability_weight": 100, "feature_association": { "enable_feature": [ @@ -8,9 +7,7 @@ ] } }, - { -@@ -20,21 +20,21 @@ - ] +@@ -21,19 +21,19 @@ } }, { @@ -34,4 +31,3 @@ "PLATFORM_MAC", "PLATFORM_LINUX", "PLATFORM_ANDROID" - ] \ No newline at end of file