Skip to content

Commit

Permalink
Cope with missing version attribute on module (#139)
Browse files Browse the repository at this point in the history
Cope with missing `version` attribute on module

---------

Co-authored-by: Derek Cormier <[email protected]>
  • Loading branch information
lalten and kormide authored Jan 30, 2024
1 parent e765a50 commit 4b72df8
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 21 deletions.
89 changes: 81 additions & 8 deletions e2e/__snapshots__/e2e.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,21 @@ modules/unversioned/1.0.0/MODULE.bazel
----------------------------------------------------
module(
name = \\"unversioned\\",
version = \\"1.0.0\\",
version = \\"1.0.0\\",
)
----------------------------------------------------
modules/unversioned/1.0.0/patches/module_dot_bazel_version.patch
----------------------------------------------------
===================================================================
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,4 +1,4 @@
@@ -1,3 +1,4 @@
module(
name = \\"unversioned\\",
- version = \\"0.0.0\\",
+ version = \\"1.0.0\\",
- name = \\"unversioned\\"
+ name = \\"unversioned\\",
+ version = \\"1.0.0\\",
)
\\\\ No newline at end of file
----------------------------------------------------
modules/unversioned/1.0.0/presubmit.yml
Expand All @@ -204,11 +204,11 @@ bcr_test_module:
modules/unversioned/1.0.0/source.json
----------------------------------------------------
{
\\"integrity\\": \\"sha256-eXQOU+DNwg/Q29tX59jP0+SFLvRHrQf3z+KJVS/gIRk=\\",
\\"integrity\\": \\"sha256-oLg/mgWyhr6DFepRYkjoPphQ8KOriLgu6M4x2RSsLbU=\\",
\\"strip_prefix\\": \\"unversioned-1.0.0\\",
\\"url\\": \\"https://github.com/testorg/unversioned/archive/refs/tags/v1.0.0.tar.gz\\",
\\"patches\\": {
\\"module_dot_bazel_version.patch\\": \\"sha256-y0kC8heeH9bQKhrfx2JuX+RK0KyjwHhPac3wBc4Nkg4=\\"
\\"module_dot_bazel_version.patch\\": \\"sha256-LGXyh9FLhgIPbe0gHfxAPnEQ7HVR+HUP/IDbPdl3ZkA=\\"
},
\\"patch_strip\\": 1
}
Expand Down Expand Up @@ -292,6 +292,79 @@ modules/versioned/metadata.json
"
`;

exports[`e2e tests [snapshot] ruleset with zero-versioned module in source 1`] = `
"----------------------------------------------------
modules/zero-versioned/1.0.0/MODULE.bazel
----------------------------------------------------
module(
name = \\"zero-versioned\\",
version = \\"1.0.0\\",
)
----------------------------------------------------
modules/zero-versioned/1.0.0/patches/module_dot_bazel_version.patch
----------------------------------------------------
===================================================================
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,4 +1,4 @@
module(
name = \\"zero-versioned\\",
- version = \\"0.0.0\\",
+ version = \\"1.0.0\\",
)
----------------------------------------------------
modules/zero-versioned/1.0.0/presubmit.yml
----------------------------------------------------
bcr_test_module:
module_path: \\"e2e/bzlmod\\"
matrix:
platform: [\\"debian10\\", \\"macos\\", \\"ubuntu2004\\", \\"windows\\"]
tasks:
run_tests:
name: \\"Run test module\\"
platform: \${{ platform }}
test_targets:
- \\"//...\\"
----------------------------------------------------
modules/zero-versioned/1.0.0/source.json
----------------------------------------------------
{
\\"integrity\\": \\"sha256-sLh05DKTEOuO3vz9+IDUJJNOf0GSLxdGDJ8qOLZRUCQ=\\",
\\"strip_prefix\\": \\"zero-versioned-1.0.0\\",
\\"url\\": \\"https://github.com/testorg/zero-versioned/archive/refs/tags/v1.0.0.tar.gz\\",
\\"patches\\": {
\\"module_dot_bazel_version.patch\\": \\"sha256-KpbuC1vv5mfhdTs5nnTl3/pH7Y/6JCnD1b1XLsqyOAo=\\"
},
\\"patch_strip\\": 1
}
----------------------------------------------------
modules/zero-versioned/metadata.json
----------------------------------------------------
{
\\"homepage\\": \\"https://github.com/testorg/zero-versioned\\",
\\"maintainers\\": [
{
\\"name\\": \\"Foo McBar\\",
\\"email\\": \\"[email protected]\\",
\\"github\\": \\"foobar\\"
}
],
\\"repository\\": [
\\"github:testorg/zero-versioned\\"
],
\\"versions\\": [
\\"1.0.0\\"
],
\\"yanked_versions\\": {}
}
"
`;

exports[`e2e tests [snapshot] ruleset with zip release archive 1`] = `
"----------------------------------------------------
modules/zip/1.0.0/MODULE.bazel
Expand Down
39 changes: 39 additions & 0 deletions e2e/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,45 @@ describe("e2e tests", () => {
expect(snapshot).toMatchSnapshot();
});

test("[snapshot] ruleset with zero-versioned module in source", async () => {
const repo = Fixture.ZeroVersioned;
const tag = "v1.0.0";
await setupLocalRemoteRulesetRepo(repo, tag, releaser);

fakeGitHub.mockUser(releaser);
fakeGitHub.mockRepository(testOrg, repo);
fakeGitHub.mockRepository(
testOrg,
"bazel-central-registry",
"bazelbuild",
"bazel-central-registry"
);
const installationId = fakeGitHub.mockAppInstallation(testOrg, repo);
fakeGitHub.mockAppInstallation(testOrg, "bazel-central-registry");

const releaseArchive = await makeReleaseTarball(repo, "zero-versioned-1.0.0");
await fakeGitHub.mockReleaseArchive(
`/${testOrg}/${repo}/archive/refs/tags/${tag}.tar.gz`,
releaseArchive
);

const response = await publishReleaseEvent(
cloudFunctions.getBaseUrl(),
secrets.webhookSecret,
installationId,
{
owner: testOrg,
repo,
tag,
releaser,
}
);
expect(response.status).toEqual(200);

const snapshot = await rollupEntryFiles();
expect(snapshot).toMatchSnapshot();
});

test("[snapshot] ruleset with versioned module in source", async () => {
const repo = Fixture.Versioned;
const tag = "v1.0.0";
Expand Down
5 changes: 2 additions & 3 deletions e2e/fixtures/unversioned/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module(
name = "unversioned",
version = "0.0.0",
)
name = "unversioned"
)
2 changes: 1 addition & 1 deletion e2e/fixtures/unversioned/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Ruleset repo that doesn't update the source MODULE.bazel's `version` field.
Ruleset repo that doesn't have the source MODULE.bazel's `version` field.
15 changes: 15 additions & 0 deletions e2e/fixtures/zero-versioned/.bcr/metadata.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"homepage": "https://github.com/testorg/zero-versioned",
"maintainers": [
{
"name": "Foo McBar",
"email": "[email protected]",
"github": "foobar"
}
],
"repository": [
"github:testorg/zero-versioned"
],
"versions": [],
"yanked_versions": {}
}
10 changes: 10 additions & 0 deletions e2e/fixtures/zero-versioned/.bcr/presubmit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bcr_test_module:
module_path: "e2e/bzlmod"
matrix:
platform: ["debian10", "macos", "ubuntu2004", "windows"]
tasks:
run_tests:
name: "Run test module"
platform: ${{ platform }}
test_targets:
- "//..."
5 changes: 5 additions & 0 deletions e2e/fixtures/zero-versioned/.bcr/source.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"integrity": "",
"strip_prefix": "{REPO}-{VERSION}",
"url": "https://github.com/{OWNER}/{REPO}/archive/refs/tags/{TAG}.tar.gz"
}
4 changes: 4 additions & 0 deletions e2e/fixtures/zero-versioned/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module(
name = "zero-versioned",
version = "0.0.0",
)
1 change: 1 addition & 0 deletions e2e/fixtures/zero-versioned/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ruleset repo that doesn't set the source MODULE.bazel's `version` field.
1 change: 1 addition & 0 deletions e2e/helpers/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum Fixture {
NoPrefix = "no-prefix",
Tarball = "tarball",
Unversioned = "unversioned",
ZeroVersioned = "zero-versioned",
Versioned = "versioned",
Zip = "zip",
}
Expand Down
3 changes: 2 additions & 1 deletion src/domain/create-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ export class CreateEntryService {
): void {
if (moduleFile.version !== version) {
console.log(
"Archived MODULE.bazel version does not match release version. Creating a version patch."
`Archived MODULE.bazel version ${moduleFile.version} does not match release version ${version}.`,
"Creating a version patch.",
);
const patchFileName = "module_dot_bazel_version.patch";
const existingContent = moduleFile.content;
Expand Down
42 changes: 41 additions & 1 deletion src/domain/module-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ describe("moduleName", () => {
});
});

describe("moduleVersion", () => {
describe("version", () => {
test("parses module version", () => {
const moduleFile = new ModuleFile("MODULE.bazel");
expect(moduleFile.version).toEqual("1.2.3");
});

test("returns undefined when the version is missing", () => {
mocked(fs.readFileSync).mockReturnValue(`\
module(name = "rules_foo")
`);
const moduleFile = new ModuleFile("MODULE.bazel");
expect(moduleFile.version).toBeUndefined();
});
});

describe("content", () => {
Expand All @@ -60,6 +68,38 @@ describe("stampVersion", () => {
fakeModuleFile({ moduleName: "rules_foo", version: "4.5.6" })
);
});

test("stamps the version when the version field was originally missing", () => {
mocked(fs.readFileSync).mockReturnValue(`\
module(
name = "rules_foo"
)`);
const moduleFile = new ModuleFile("MODULE.bazel");
moduleFile.stampVersion("4.5.6");

expect(moduleFile.content).toEqual(`\
module(
name = "rules_foo",
version = "4.5.6",
)`);
});

test("stamps the version when the version field was originally missing and the last field is comma-trailed", () => {
mocked(fs.readFileSync).mockReturnValue(`\
module(
name = "rules_foo",
compatibility_level = 1,
)`);
const moduleFile = new ModuleFile("MODULE.bazel");
moduleFile.stampVersion("4.5.6");

expect(moduleFile.content).toEqual(`\
module(
name = "rules_foo",
compatibility_level = 1,
version = "4.5.6",
)`);
});
});

describe("save", () => {
Expand Down
23 changes: 16 additions & 7 deletions src/domain/module-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,30 @@ export class ModuleFile {
return name;
}

public get version(): string {
public get version(): string | undefined {
const regex = /module\([^)]*?version\s*=\s*"(.+?)"/s;
const version = this.moduleContent.match(regex)[1];
return version;
const match = this.moduleContent.match(regex);
return match ? match[1] : undefined;
}

public get content(): string {
return this.moduleContent;
}

public stampVersion(version: string): void {
this.moduleContent = this.moduleContent.replace(
/(^.*?module\(.*?version\s*=\s*")[\w.]+(".*$)/s,
`$1${version}$2`
);
if (this.version !== undefined) {
// update the version
this.moduleContent = this.moduleContent.replace(
/(^.*?module\(.*?version\s*=\s*")[\w.]+(".*$)/s,
`$1${version}$2`
);
} else {
// add the version
this.moduleContent = this.moduleContent.replace(
/(^.*?module\(.*?),?(\s*)\)/s,
`$1,\n version = "${version}",\n)`
);
}
}

public save(destPath: string) {
Expand Down

0 comments on commit 4b72df8

Please sign in to comment.