Skip to content

Commit

Permalink
update tests, unexport highest patch function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophie Wigmore authored and robdimsdale committed Nov 9, 2023
1 parent 843b5d5 commit aed3a22
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 29 deletions.
8 changes: 4 additions & 4 deletions internal/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
91 changes: 66 additions & 25 deletions internal/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, `{
Expand Down Expand Up @@ -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:[email protected]", 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() {
Expand Down Expand Up @@ -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, `{
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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")))
})
})
})
})
}

0 comments on commit aed3a22

Please sign in to comment.