From aed3a223202ffd675f0cb420f70768ca81fd9a11 Mon Sep 17 00:00:00 2001 From: Sophie Wigmore Date: Thu, 9 Nov 2023 13:14:48 -0500 Subject: [PATCH] update tests, unexport highest patch function --- internal/image.go | 8 ++-- internal/image_test.go | 91 ++++++++++++++++++++++++++++++------------ 2 files changed, 70 insertions(+), 29 deletions(-) diff --git a/internal/image.go b/internal/image.go index 3535ee3..cf017c1 100644 --- a/internal/image.go +++ b/internal/image.go @@ -83,7 +83,7 @@ func FindLatestImageOnCNBRegistry(uri, api, patchVersion string) (Image, error) versions = append(versions, v.Version) } - highestPatch, err := GetHighestPatch(patchVersion, versions) + highestPatch, err := getHighestPatch(patchVersion, versions) if err != nil { return Image{}, fmt.Errorf("could not get the highest patch in the %s line: %w", patchVersion, err) } @@ -123,7 +123,7 @@ func FindLatestImage(uri, patchVersion string) (Image, error) { } if patchVersion != "" { - highestPatch, err := GetHighestPatch(patchVersion, tags) + highestPatch, err := getHighestPatch(patchVersion, tags) if err != nil { return Image{}, fmt.Errorf("could not get the highest patch in the %s line: %w", patchVersion, err) } @@ -132,8 +132,8 @@ func FindLatestImage(uri, patchVersion string) (Image, error) { Path: reference.Path(named), Version: highestPatch, }, nil - } + var versions []*semver.Version for _, tag := range tags { version, err := semver.StrictNewVersion(tag) @@ -258,7 +258,7 @@ func GetBuildpackageID(uri string) (string, error) { return metadata.BuildpackageID, nil } -func GetHighestPatch(patchVersion string, allVersions []string) (string, error) { +func getHighestPatch(patchVersion string, allVersions []string) (string, error) { versionConstraint, err := semver.NewConstraint(fmt.Sprintf("~%s", patchVersion)) if err != nil { return "", fmt.Errorf("version constraint ~%s is not a valid semantic version constraint: %w", patchVersion, err) diff --git a/internal/image_test.go b/internal/image_test.go index 32aacb8..716f923 100644 --- a/internal/image_test.go +++ b/internal/image_test.go @@ -64,6 +64,36 @@ func testImage(t *testing.T, context spec.G, it spec.S) { ] }`) + case "/v1/buildpacks/no-new-patch": + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, `{ + "latest": { + "version": "0.1.0" + }, + "versions": [ + { + "version": "0.1.0", + "_link": "https://registry.buildpacks.io//api/v1/buildpacks/no-new-patch/0.1.0" + }, + { + "version": "0.0.3-rc", + "_link": "https://registry.buildpacks.io//api/v1/buildpacks/no-new-patch/0.0.3-beta.1" + }, + { + "version": "random-version", + "_link": "https://registry.buildpacks.io//api/v1/buildpacks/no-new-patch/0.0.3-random-version" + }, + { + "version": "0.0.2-rc", + "_link": "https://registry.buildpacks.io//api/v1/buildpacks/no-new-patch/0.0.2-rc" + }, + { + "version": "0.0.1", + "_link": "https://registry.buildpacks.io//api/v1/buildpacks/no-new-patch/0.0.1" + } + ] + }`) + case "/v1/buildpacks/paketo-buildpacks/go": w.WriteHeader(http.StatusOK) fmt.Fprintln(w, `{ @@ -146,6 +176,18 @@ func testImage(t *testing.T, context spec.G, it spec.S) { Version: "0.0.3", })) }) + + context("there are newer patches available, but they are pre-releases or not semantically versioned", func() { + it("returns the highest semantically versioned regular patch", func() { + image, err := internal.FindLatestImageOnCNBRegistry("urn:cnb:registry:no-new-patch@0.0.1", server.URL, "0.0.1") + Expect(err).NotTo(HaveOccurred()) + Expect(image).To(Equal(internal.Image{ + Name: "urn:cnb:registry:no-new-patch", + Path: "no-new-patch", + Version: "0.0.1", + })) + }) + }) }) context("failure cases", func() { @@ -206,6 +248,18 @@ func testImage(t *testing.T, context spec.G, it spec.S) { ] }`) + case "/v2/some-org/no-new-patch/tags/list": + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, `{ + "tags": [ + "0.0.1", + "0.0.2-bad-version", + "0.0.3-rc", + "0.1.0", + "latest" + ] + }`) + case "/v2/some-org/some-other-repo/tags/list": w.WriteHeader(http.StatusOK) fmt.Fprintln(w, `{ @@ -270,6 +324,18 @@ func testImage(t *testing.T, context spec.G, it spec.S) { Version: "0.0.10", })) }) + + context("there are newer patches available, but they are pre-releases or not semantically versioned", func() { + it("returns the highest semantically versioned regular patch", func() { + image, err := internal.FindLatestImage(fmt.Sprintf("%s/some-org/no-new-patch:0.0.1", strings.TrimPrefix(server.URL, "http://")), "0.0.1") + Expect(err).NotTo(HaveOccurred()) + Expect(image).To(Equal(internal.Image{ + Name: fmt.Sprintf("%s/some-org/no-new-patch", strings.TrimPrefix(server.URL, "http://")), + Path: "some-org/no-new-patch", + Version: "0.0.1", + })) + }) + }) }) context("failure cases", func() { @@ -506,29 +572,4 @@ func testImage(t *testing.T, context spec.G, it spec.S) { }) }) }) - - context("GetHighestPatch", func() { - it("returns the highest patch version in a minor version line given a patch", func() { - version, err := internal.GetHighestPatch("1.2.3", []string{"0.0.1", "0.0.2", "1.2.3", "1.2.4", "1.3.0", "2.3.4"}) - Expect(err).NotTo(HaveOccurred()) - Expect(version).To(Equal("1.2.4")) - }) - - context("the patch version provided is already the highest patch available", func() { - it("returns the given patch version", func() { - version, err := internal.GetHighestPatch("1.2.4", []string{"0.0.1", "0.0.2", "1.2.3", "1.2.4", "1.3.0", "2.3.4"}) - Expect(err).NotTo(HaveOccurred()) - Expect(version).To(Equal("1.2.4")) - }) - }) - - context("failure cases", func() { - context("the patch version given is not a semantic version", func() { - it("returns an error", func() { - _, err := internal.GetHighestPatch("bad-version", []string{"0.0.1", "0.0.2", "1.2.3", "1.2.4", "1.3.0", "2.3.4"}) - Expect(err).To(MatchError(ContainSubstring("version constraint ~bad-version is not a valid semantic version constraint"))) - }) - }) - }) - }) }