From 3a8b0bdcfb20ae64059959e24a746fc429c22d6d Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Tue, 2 Jan 2024 00:36:43 +0000 Subject: [PATCH] chore(deps): update dependency @types/diff to v5.0.9 --- src/domain/create-entry.spec.ts | 57 +++++++++++++++++++++++++++++++++ src/domain/create-entry.ts | 20 ++++++++++-- src/domain/module-file.spec.ts | 26 ++++++++++++++- src/domain/module-file.ts | 14 +++++++- yarn.lock | 6 ++-- 5 files changed, 116 insertions(+), 7 deletions(-) diff --git a/src/domain/create-entry.spec.ts b/src/domain/create-entry.spec.ts index 8728813..e5987ac 100644 --- a/src/domain/create-entry.spec.ts +++ b/src/domain/create-entry.spec.ts @@ -15,6 +15,7 @@ import { import { expectThrownError } from "../test/util"; import { CreateEntryService, + PatchModuleError, VersionAlreadyPublishedError, } from "./create-entry"; import { CANONICAL_BCR } from "./find-registry-fork"; @@ -809,6 +810,62 @@ describe("createEntryFiles", () => { exptectedPatchedModule ); }); + + test("throws when a patch that alters MODULE.bazel cannot be applied", async () => { + const patchFrom = fakeModuleFile({ + version: "1.0.0", + moduleName: "rules_bar", + deps: false, + }); + + const patchTo = fakeModuleFile({ + version: "1.2.3", + moduleName: "rules_bar", + deps: true, + }); + + const badPatch = createTwoFilesPatch( + "a/MODULE.bazel", + "b/MODULE.bazel", + patchFrom, + patchTo + ); + + mockRulesetFiles({ + // Different from the patch origin + extractedModuleContent: fakeModuleFile({ + version: "1.2.3", + moduleName: "rules_bar", + deps: false, + }), + patches: { + "patch_deps.patch": badPatch, + }, + }); + + const tag = "v1.2.3"; + const rulesetRepo = await RulesetRepository.create("repo", "owner", tag); + const bcrRepo = CANONICAL_BCR; + + let caughtError: any; + try { + await createEntryService.createEntryFiles(rulesetRepo, bcrRepo, tag, "."); + } catch (e) { + caughtError = e; + } + + expect(caughtError).toBeDefined(); + expect(caughtError instanceof PatchModuleError); + const patchPath = path.join( + rulesetRepo.diskPath, + RulesetRepository.BCR_TEMPLATE_DIR, + "patches", + "patch_deps.patch" + ); + expect((caughtError as Error).message).toEqual( + expect.stringContaining(patchPath) + ); + }); }); describe("commitEntryToNewBranch", () => { diff --git a/src/domain/create-entry.ts b/src/domain/create-entry.ts index 88676ee..7d0c9d0 100644 --- a/src/domain/create-entry.ts +++ b/src/domain/create-entry.ts @@ -7,7 +7,10 @@ import { GitHubClient } from "../infrastructure/github.js"; import { UserFacingError } from "./error.js"; import { computeIntegrityHash } from "./integrity-hash.js"; import { MetadataFile } from "./metadata-file.js"; -import { ModuleFile } from "./module-file.js"; +import { + ModuleFile, + PatchModuleError as _PatchModuleError, +} from "./module-file.js"; import { ReleaseArchive } from "./release-archive.js"; import { Repository } from "./repository.js"; import { RulesetRepository } from "./ruleset-repository.js"; @@ -20,6 +23,12 @@ export class VersionAlreadyPublishedError extends UserFacingError { } } +export class PatchModuleError extends UserFacingError { + public constructor(patchPath: string) { + super(`Failed to apply patch ${patchPath} to MODULE.bazel`); + } +} + export class CreateEntryService { constructor( private readonly gitClient: GitClient, @@ -185,7 +194,14 @@ export class CreateEntryService { diff.oldFileName === "a/MODULE.bazel" && diff.newFileName === "b/MODULE.bazel" ) { - moduleFile.patchContent(diff); + try { + moduleFile.patchContent(diff); + } catch (e) { + if (e instanceof _PatchModuleError) { + throw new PatchModuleError(patchSrc); + } + throw e; + } } } } diff --git a/src/domain/module-file.spec.ts b/src/domain/module-file.spec.ts index 3c0fb79..bc42d49 100644 --- a/src/domain/module-file.spec.ts +++ b/src/domain/module-file.spec.ts @@ -2,7 +2,7 @@ import { createTwoFilesPatch, parsePatch } from "diff"; import { mocked } from "jest-mock"; import fs from "node:fs"; import { fakeModuleFile } from "../test/mock-template-files"; -import { ModuleFile } from "./module-file"; +import { ModuleFile, PatchModuleError } from "./module-file"; jest.mock("node:fs"); @@ -100,4 +100,28 @@ describe("patchContent", () => { moduleFile.patchContent(patch[0]); expect(moduleFile.content).toEqual(patchedModuleFile); }); + + test("throws when the patch could not be applied", () => { + const patchedModuleFile = fakeModuleFile({ + moduleName: "rules_foo", + version: "1.2.3", + deps: true, + }); + + const moduleFile = new ModuleFile("MODULE.bazel"); + + const patch = parsePatch( + createTwoFilesPatch( + "a/MODULE.bazel", + "b/MODULE.bazel", + moduleFile.content, + patchedModuleFile + ) + ); + + // Change the module file's version so that the generated patch + // will no longer apply correctly. + moduleFile.stampVersion("10.20.30"); + expect(() => moduleFile.patchContent(patch[0])).toThrow(PatchModuleError); + }); }); diff --git a/src/domain/module-file.ts b/src/domain/module-file.ts index 6d06260..ef66ea7 100644 --- a/src/domain/module-file.ts +++ b/src/domain/module-file.ts @@ -1,6 +1,12 @@ import { ParsedDiff, applyPatch } from "diff"; import fs from "node:fs"; +export class PatchModuleError extends Error { + public constructor() { + super("Failed to apply patch to MODULE.bazel file"); + } +} + export class ModuleFile { private moduleContent: string; @@ -37,6 +43,12 @@ export class ModuleFile { } public patchContent(patch: ParsedDiff): void { - this.moduleContent = applyPatch(this.moduleContent, patch); + const result = applyPatch(this.moduleContent, patch); + + if (result === false) { + throw new PatchModuleError(); + } + + this.moduleContent = result; } } diff --git a/yarn.lock b/yarn.lock index d38dfe9..e30988b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1240,9 +1240,9 @@ "@types/node" "*" "@types/diff@^5.0.2": - version "5.0.2" - resolved "https://registry.yarnpkg.com/@types/diff/-/diff-5.0.2.tgz#dd565e0086ccf8bc6522c6ebafd8a3125c91c12b" - integrity sha512-uw8eYMIReOwstQ0QKF0sICefSy8cNO/v7gOTiIy9SbwuHyEecJUm7qlgueOO5S1udZ5I/irVydHVwMchgzbKTg== + version "5.0.9" + resolved "https://registry.yarnpkg.com/@types/diff/-/diff-5.0.9.tgz#31977962175079c2048315febeb8fd5f520192c6" + integrity sha512-RWVEhh/zGXpAVF/ZChwNnv7r4rvqzJ7lYNSmZSVTxjV0PBLf6Qu7RNg+SUtkpzxmiNkjCx0Xn2tPp7FIkshJwQ== "@types/express-serve-static-core@^4.17.33": version "4.17.41"