diff --git a/e2e/__snapshots__/e2e.spec.ts.snap b/e2e/__snapshots__/e2e.spec.ts.snap index 2f65c9d..6b5331f 100644 --- a/e2e/__snapshots__/e2e.spec.ts.snap +++ b/e2e/__snapshots__/e2e.spec.ts.snap @@ -168,7 +168,10 @@ 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 @@ -176,9 +179,12 @@ 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 @@ -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 } @@ -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\\": { diff --git a/e2e/fixtures/unversioned/MODULE.bazel b/e2e/fixtures/unversioned/MODULE.bazel index e00336e..93d4911 100644 --- a/e2e/fixtures/unversioned/MODULE.bazel +++ b/e2e/fixtures/unversioned/MODULE.bazel @@ -1 +1,3 @@ -module(name = "unversioned") +module( + name = "unversioned" +) diff --git a/e2e/fixtures/zero-versioned/README.md b/e2e/fixtures/zero-versioned/README.md index d1da306..6322257 100644 --- a/e2e/fixtures/zero-versioned/README.md +++ b/e2e/fixtures/zero-versioned/README.md @@ -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. diff --git a/src/domain/module-file.spec.ts b/src/domain/module-file.spec.ts index bc42d49..e6afda1 100644 --- a/src/domain/module-file.spec.ts +++ b/src/domain/module-file.spec.ts @@ -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", () => { @@ -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", () => { diff --git a/src/domain/module-file.ts b/src/domain/module-file.ts index 36817ac..7c0b368 100644 --- a/src/domain/module-file.ts +++ b/src/domain/module-file.ts @@ -21,10 +21,10 @@ 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 { @@ -32,7 +32,7 @@ export class ModuleFile { } 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, @@ -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)` ); } }