From e78db117cb897d237af12c621f4fa0f2406353b0 Mon Sep 17 00:00:00 2001 From: Joost Muller Date: Mon, 8 Jan 2024 21:07:04 +0100 Subject: [PATCH] test: add tests for handling `version-prefix` --- test/bump.test.ts | 86 +++++++++++++++++++++++++++++++++++++++++++++ test/config.test.ts | 9 ++++- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/test/bump.test.ts b/test/bump.test.ts index c2d59ee0..0965145d 100644 --- a/test/bump.test.ts +++ b/test/bump.test.ts @@ -68,6 +68,7 @@ beforeEach(() => { ]); /* + jest.spyOn(core, "debug").mockImplementation(console.log); jest.spyOn(core, "info").mockImplementation(console.log); jest.spyOn(core, "warning").mockImplementation(console.log); jest.spyOn(core, "error").mockImplementation(console.log); @@ -504,3 +505,88 @@ describe("Create changelog", () => { } }); }); + +describe("Version prefix handling", () => { + const TEST_VERSIONS = [ + { name: "versiona-3.4.5", commitSha: "345" }, + { name: "versionb-4.5.6", commitSha: "456" }, + { name: "versionc-1.2.3", commitSha: "123" }, + { name: "1.1.1", commitSha: "111" }, + ]; + const versionPrefixTests = [ + { + desc: "no prefix", + versionPrefixInput: null, + versionPrefixConfig: null, + expected: SemVer.fromString("versionb-4.5.6"), + }, + { + desc: "explicit no prefix", + versionPrefixInput: "", + versionPrefixConfig: "", + expected: SemVer.fromString("1.1.1"), + }, + { + desc: "input prefix", + versionPrefixInput: "versiona-", + versionPrefixConfig: "", + expected: SemVer.fromString("versiona-3.4.5"), + }, + { + desc: "config prefix", + versionPrefixInput: "", + versionPrefixConfig: "versionc-", + expected: SemVer.fromString("versionc-1.2.3"), + }, + { + desc: "config and input prefix (input takes precedence)", + versionPrefixInput: "versiona-", + versionPrefixConfig: "versionb-", + expected: SemVer.fromString("versiona-3.4.5"), + }, + { + desc: "non-matching config prefix", + versionPrefixInput: "", + versionPrefixConfig: "versiond-", + expected: null, + }, + ]; + + beforeEach(() => { + jest.spyOn(fs, "existsSync").mockReturnValue(true); + jest.spyOn(github, "getLatestTags").mockResolvedValue(TEST_VERSIONS); + }); + + test.each(versionPrefixTests)( + "$desc", + async ({ versionPrefixInput, versionPrefixConfig, expected }) => { + jest.spyOn(core, "getInput").mockImplementation((setting, options?) => { + switch (setting) { + case "version-prefix": + return versionPrefixInput ?? "*"; + } + return ""; + }); + + jest + .spyOn(fs, "readFileSync") + .mockReturnValue( + `version-scheme: "semver"` + + (versionPrefixConfig + ? `\nversion-prefix: ${versionPrefixConfig}` + : "") + ); + + await bumpaction.exportedForTesting.run(); + + const commitSha = + TEST_VERSIONS.find(version => version.name == expected?.toString()) + ?.commitSha ?? "dummy"; + + // We specifically test the matcher function passed to matchTagsToCommits here, + // as it's (sadly) the only way to test the actual behavior of the matcher. + const matcherFunc = github.matchTagsToCommits.mock.calls[0][1]; + expect(matcherFunc("", commitSha)).toEqual(expected); + } + ); +}); diff --git a/test/config.test.ts b/test/config.test.ts index 0712c5ec..d4a1383c 100644 --- a/test/config.test.ts +++ b/test/config.test.ts @@ -388,6 +388,12 @@ describe("Configurable options", () => { }); }); + test("Default version prefix", () => { + withConfig("", config => { + expect(config.versionPrefix).toBe("*"); + }); + }); + test("Boolean defaults", () => { withConfig( "version-scheme: sdkver\nsdkver-create-release-branches: true", @@ -405,9 +411,10 @@ describe("Configurable options", () => { test("String values", () => { withConfig( - "version-scheme: sdkver\nsdkver-create-release-branches: some-release-prefix-", + "version-scheme: sdkver\nsdkver-create-release-branches: some-release-prefix-\nversion-prefix: X", config => { expect(config.sdkverCreateReleaseBranches).toBe("some-release-prefix-"); + expect(config.versionPrefix).toBe("X"); } ); });