Skip to content

Commit

Permalink
additional fixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed Jan 30, 2024
1 parent 3e3b166 commit 037f706
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
20 changes: 13 additions & 7 deletions e2e/__snapshots__/e2e.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,23 @@ exports[`e2e tests [snapshot] ruleset with unversioned module in source 1`] = `
"----------------------------------------------------
modules/unversioned/1.0.0/MODULE.bazel
----------------------------------------------------
module(name = \\"unversioned\\", version = \\"1.0.0\\")
module(
name = \\"unversioned\\",
version = \\"1.0.0\\"
)
----------------------------------------------------
modules/unversioned/1.0.0/patches/module_dot_bazel_version.patch
----------------------------------------------------
===================================================================
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,1 +1,1 @@
-module(name = \\"unversioned\\")
+module(name = \\"unversioned\\", version = \\"1.0.0\\")
@@ -1,3 +1,4 @@
module(
- name = \\"unversioned\\"
+ name = \\"unversioned\\",
+ version = \\"1.0.0\\"
)
----------------------------------------------------
modules/unversioned/1.0.0/presubmit.yml
Expand All @@ -198,11 +204,11 @@ bcr_test_module:
modules/unversioned/1.0.0/source.json
----------------------------------------------------
{
\\"integrity\\": \\"sha256-X2ZIYATg3qceheFSj/qaTU0SqqW/hFc8UIGkrK3FmZQ=\\",
\\"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-OoXz9ahBlGjW4IVb4QCmXMZInK7XbFEjCHya3wx8AAk=\\"
\\"module_dot_bazel_version.patch\\": \\"sha256-CahdP4+xcGJVM9GyA0Sg2rUe5rIPHzKJ6J4SfFTm2x8=\\"
},
\\"patch_strip\\": 1
}
Expand Down Expand Up @@ -326,7 +332,7 @@ bcr_test_module:
modules/zero-versioned/1.0.0/source.json
----------------------------------------------------
{
\\"integrity\\": \\"sha256-P4olS5x2mo1PJPI6iU0TMfPHWs9inbRZFJZHmhEY4no=\\",
\\"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\\": {
Expand Down
4 changes: 3 additions & 1 deletion e2e/fixtures/unversioned/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module(name = "unversioned")
module(
name = "unversioned"
)
2 changes: 1 addition & 1 deletion e2e/fixtures/zero-versioned/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 set the source MODULE.bazel's `version` field.
25 changes: 24 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,21 @@ 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"
)`);
});
});

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

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

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

public stampVersion(version: string): void {
if (this.version) {
if (this.version !== undefined) {
// update the version
this.moduleContent = this.moduleContent.replace(
/(^.*?module\(.*?version\s*=\s*")[\w.]+(".*$)/s,
Expand All @@ -41,8 +41,8 @@ export class ModuleFile {
} else {
// add the version
this.moduleContent = this.moduleContent.replace(
/(^.*?module\(.*?)\)/s,
`$1, version = "${version}")`
/(^.*?module\(.*?)(\s*)\)/s,
`$1,\n version = "${version}"\n)`
);
}
}
Expand Down

0 comments on commit 037f706

Please sign in to comment.