Skip to content

Commit

Permalink
chore(deps): update dependency @types/diff to v5.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
renovate[bot] authored and kormide committed Jan 2, 2024
1 parent 50ece7f commit f75c0f6
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 7 deletions.
57 changes: 57 additions & 0 deletions src/domain/create-entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { expectThrownError } from "../test/util";
import {
CreateEntryService,
PatchModuleError,
VersionAlreadyPublishedError,
} from "./create-entry";
import { CANONICAL_BCR } from "./find-registry-fork";
Expand Down Expand Up @@ -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", () => {
Expand Down
20 changes: 18 additions & 2 deletions src/domain/create-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down
26 changes: 25 additions & 1 deletion src/domain/module-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
});
});
14 changes: 13 additions & 1 deletion src/domain/module-file.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
}
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1185,9 +1185,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"
Expand Down

0 comments on commit f75c0f6

Please sign in to comment.