Skip to content

Commit

Permalink
fix: apply patches to entry module file
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed Sep 30, 2023
1 parent 9ad8490 commit a6e0cb6
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 24 deletions.
75 changes: 64 additions & 11 deletions src/domain/create-entry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, jest, test } from "@jest/globals";
import { createTwoFilesPatch } from "diff";
import { Mocked, mocked } from "jest-mock";
import { randomUUID } from "node:crypto";
import fs, { PathLike } from "node:fs";
Expand Down Expand Up @@ -664,13 +665,13 @@ describe("createEntryFiles", () => {
writtenPatchContent.includes(`\
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,6 +1,6 @@
module(
name = "rules_bar",
compatibility_level = 1,
- version = "1.2.3",
+ version = "4.5.6",
)`)
@@ -1,5 +1,5 @@
module(
name = "rules_bar",
compatibility_level = 1,
- version = "1.2.3",
+ version = "4.5.6",
)`)
).toEqual(true);
});
});
Expand Down Expand Up @@ -738,6 +739,53 @@ describe("createEntryFiles", () => {
expectedPatchPath
);
});

test("applies a patch to the entry's MODULE.bazel file", async () => {
const extractedModule = fakeModuleFile({
version: "1.2.3",
moduleName: "rules_bar",
deps: false,
});

const exptectedPatchedModule = fakeModuleFile({
version: "1.2.3",
moduleName: "rules_bar",
deps: true,
});

const patch = createTwoFilesPatch(
"a/MODULE.bazel",
"b/MODULE.bazel",
extractedModule,
exptectedPatchedModule
);

mockRulesetFiles({
extractedModuleContent: extractedModule,
patches: {
"patch_deps.patch": patch,
},
});

const tag = "v1.2.3";
const rulesetRepo = await RulesetRepository.create("repo", "owner", tag);
const bcrRepo = CANONICAL_BCR;

await createEntryService.createEntryFiles(rulesetRepo, bcrRepo, tag, ".");

const moduleFilePath = path.join(
bcrRepo.diskPath,
"modules",
"rules_bar",
"1.2.3",
"MODULE.bazel"
);

expect(fs.writeFileSync).toHaveBeenCalledWith(
moduleFilePath,
exptectedPatchedModule
);
});
});

describe("commitEntryToNewBranch", () => {
Expand Down Expand Up @@ -957,6 +1005,7 @@ describe("pushEntryToFork", () => {

function mockRulesetFiles(
options: {
extractedModuleContent?: string;
extractedModuleName?: string;
extractedModuleVersion?: string;
metadataHomepage?: string;
Expand All @@ -971,10 +1020,14 @@ function mockRulesetFiles(
mockGitClient.checkout.mockImplementation(
async (repoPath: string, ref?: string) => {
const moduleRoot = options?.moduleRoot || ".";
mockedFileReads[EXTRACTED_MODULE_PATH] = fakeModuleFile({
version: options.extractedModuleVersion || "1.2.3",
moduleName: options.extractedModuleName,
});
if (options.extractedModuleContent) {
mockedFileReads[EXTRACTED_MODULE_PATH] = options.extractedModuleContent;
} else {
mockedFileReads[EXTRACTED_MODULE_PATH] = fakeModuleFile({
version: options.extractedModuleVersion || "1.2.3",
moduleName: options.extractedModuleName,
});
}
const templatesDir = path.join(
repoPath,
RulesetRepository.BCR_TEMPLATE_DIR
Expand Down
22 changes: 19 additions & 3 deletions src/domain/create-entry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createTwoFilesPatch } from "diff";
import { createTwoFilesPatch, parsePatch } from "diff";
import { randomBytes } from "node:crypto";
import fs from "node:fs";
import fs, { readFileSync } from "node:fs";
import path from "node:path";
import { GitClient } from "../infrastructure/git.js";
import { GitHubClient } from "../infrastructure/github.js";
Expand Down Expand Up @@ -83,6 +83,7 @@ export class CreateEntryService {
this.addPatches(
rulesetRepo,
sourceTemplate,
moduleFile,
bcrVersionEntryPath,
moduleRoot
);
Expand Down Expand Up @@ -150,6 +151,7 @@ export class CreateEntryService {
private addPatches(
rulesetRepo: RulesetRepository,
sourceTemplate: SourceTemplate,
moduleFile: ModuleFile,
bcrVersionEntryPath: string,
moduleRoot: string
): void {
Expand All @@ -169,10 +171,24 @@ export class CreateEntryService {
}

for (const patch of patches) {
const patchSrc = path.join(patchesPath, patch);
const patchDest = path.join(bcrVersionEntryPath, "patches", patch);
fs.mkdirSync;
fs.copyFileSync(path.join(patchesPath, patch), patchDest);
fs.copyFileSync(patchSrc, patchDest);
sourceTemplate.addPatch(patch, computeIntegrityHash(patchDest), 1);

// If the user-provided patch patches MODULE.bazel, also apply it to
// the copy in the entry since it needs to be identical to the archived
// MODULE.bazel with any patches.
const diffs = parsePatch(readFileSync(patchSrc, "utf8"));
for (const diff of diffs) {
if (
diff.oldFileName === "a/MODULE.bazel" &&
diff.newFileName === "b/MODULE.bazel"
) {
moduleFile.patchContent(diff);
}
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/domain/module-file.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, jest, test } from "@jest/globals";
import { createTwoFilesPatch, parsePatch } from "diff";
import { mocked } from "jest-mock";
import fs from "node:fs";
import { fakeModuleFile } from "../test/mock-template-files";
Expand All @@ -9,6 +10,7 @@ jest.mock("node:fs");
const MODULE_FILE_CONTENT = fakeModuleFile({
moduleName: "rules_foo",
version: "1.2.3",
deps: false,
});

beforeEach(() => {
Expand Down Expand Up @@ -72,3 +74,31 @@ describe("save", () => {
);
});
});

describe("patchContent", () => {
test("applies a diff", () => {
const patchedModuleFile = fakeModuleFile({
moduleName: "rules_foo",
version: "1.2.3",
deps: true,
});

const moduleFile = new ModuleFile("MODULE.bazel");

expect(moduleFile.content).not.toEqual(patchedModuleFile);

const patch = parsePatch(
createTwoFilesPatch(
"a/MODULE.bazel",
"b/MODULE.bazel",
moduleFile.content,
patchedModuleFile
)
);

expect(patch.length).toEqual(1);

moduleFile.patchContent(patch[0]);
expect(moduleFile.content).toEqual(patchedModuleFile);
});
});
5 changes: 5 additions & 0 deletions src/domain/module-file.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ParsedDiff, applyPatch } from "diff";
import fs from "node:fs";

export class ModuleFile {
Expand Down Expand Up @@ -34,4 +35,8 @@ export class ModuleFile {
public save(destPath: string) {
fs.writeFileSync(destPath, this.moduleContent);
}

public patchContent(patch: ParsedDiff): void {
this.moduleContent = applyPatch(this.moduleContent, patch);
}
}
20 changes: 10 additions & 10 deletions src/test/mock-template-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ export function fakeModuleFile(
return randomUUID();
}
let content = `\
module(
${
overrides.missingName
? ""
: `name = "${overrides.moduleName || "fake_ruleset"}",`
}
compatibility_level = 1,
version = "${overrides.version || "0.0.0"}",
)
`;
module(
${
overrides.missingName
? ""
: `name = "${overrides.moduleName || "fake_ruleset"}",`
}
compatibility_level = 1,
version = "${overrides.version || "0.0.0"}",
)
`;
if (overrides.deps) {
content += `\
bazel_dep(name = "bazel_skylib", version = "1.1.1")
Expand Down

0 comments on commit a6e0cb6

Please sign in to comment.