Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not check DeprecationDate when comparing dependencies #240

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package libpak
import (
"fmt"
"os"
"reflect"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -95,6 +96,23 @@ type BuildpackDependency struct {
DeprecationDate time.Time `toml:"deprecation_date"`
}

// Equals compares the 2 structs if they are equal. This is very simiar to reflect.DeepEqual
// except that properties that will not work (e.g. DeprecationDate) are ignored.
func (b1 BuildpackDependency) Equals(b2 BuildpackDependency) bool {

b1.DeprecationDate = time.Time{}
b2.DeprecationDate = time.Time{}

if len(b1.CPEs) == 0 {
b1.CPEs = nil
}
if len(b2.CPEs) == 0 {
b2.CPEs = nil
}

return reflect.DeepEqual(b1, b2)
c0d1ngm0nk3y marked this conversation as resolved.
Show resolved Hide resolved
}

// AsBOMEntry renders a bill of materials entry describing the dependency.
//
// Deprecated: as of Buildpacks RFC 95, use `BuildpackDependency.AsSyftArtifact` instead
Expand Down
31 changes: 30 additions & 1 deletion buildpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/buildpacks/libcnb"
. "github.com/onsi/gomega"
"github.com/pelletier/go-toml"
"github.com/sclevine/spec"

"github.com/paketo-buildpacks/libpak"
Expand All @@ -37,6 +38,33 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
Expect = NewWithT(t).Expect
)

it("is equal after toml Marshal and Unmarshal", func() {
dependency := libpak.BuildpackDependency{
ID: "test-id",
Name: "test-name",
Version: "1.1.1",
URI: "test-uri",
SHA256: "test-sha256",
DeprecationDate: time.Now(),
Stacks: []string{"test-stack"},
Licenses: []libpak.BuildpackDependencyLicense{
{
Type: "test-type",
URI: "test-uri",
},
},
}

bytes, err := toml.Marshal(dependency)
Expect(err).NotTo(HaveOccurred())

var newDependency libpak.BuildpackDependency
err = toml.Unmarshal(bytes, &newDependency)
Expect(err).NotTo(HaveOccurred())

Expect(dependency.Equals(newDependency)).To(BeTrue())
})

it("renders dependency as a BOMEntry", func() {
dependency := libpak.BuildpackDependency{
ID: "test-id",
Expand Down Expand Up @@ -573,6 +601,7 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
logger := bard.NewLogger(buff)
resolver.Logger = &logger
soonDeprecated := time.Now().UTC().Add(30 * 24 * time.Hour)
notSoSoonDeprecated := time.Now().UTC().Add(60 * 24 * time.Hour)
resolver.Dependencies = []libpak.BuildpackDependency{
{
ID: "missing-deprecation-date",
Expand All @@ -583,7 +612,7 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
ID: "valid-dependency",
Name: "valid-dependency",
Version: "1.1",
DeprecationDate: time.Now().UTC().Add(60 * 24 * time.Hour),
DeprecationDate: notSoSoonDeprecated,
},
{
ID: "soon-deprecated-dependency",
Expand Down
5 changes: 2 additions & 3 deletions dependency_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"strings"

"github.com/buildpacks/libcnb"
Expand Down Expand Up @@ -138,7 +137,7 @@ func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...Reque
return nil, fmt.Errorf("unable to decode download metadata %s\n%w", file, err)
}

if reflect.DeepEqual(dependency, actual) {
if dependency.Equals(actual) {
d.Logger.Bodyf("%s cached download from buildpack", color.GreenString("Reusing"))
return os.Open(filepath.Join(d.CachePath, dependency.SHA256, filepath.Base(uri)))
}
Expand All @@ -152,7 +151,7 @@ func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...Reque
return nil, fmt.Errorf("unable to decode download metadata %s\n%w", file, err)
}

if reflect.DeepEqual(dependency, actual) {
if dependency.Equals(actual) {
d.Logger.Bodyf("%s previously cached download", color.GreenString("Reusing"))
return os.Open(filepath.Join(d.DownloadPath, dependency.SHA256, filepath.Base(uri)))
}
Expand Down
14 changes: 8 additions & 6 deletions dependency_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/buildpacks/libcnb"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -140,12 +141,13 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
server = ghttp.NewServer()

dependency = libpak.BuildpackDependency{
ID: "test-id",
Name: "test-name",
Version: "1.1.1",
URI: fmt.Sprintf("%s/test-path", server.URL()),
SHA256: "576dd8416de5619ea001d9662291d62444d1292a38e96956bc4651c01f14bca1",
Stacks: []string{"test-stack"},
ID: "test-id",
Name: "test-name",
Version: "1.1.1",
URI: fmt.Sprintf("%s/test-path", server.URL()),
SHA256: "576dd8416de5619ea001d9662291d62444d1292a38e96956bc4651c01f14bca1",
Stacks: []string{"test-stack"},
DeprecationDate: time.Now(),
Licenses: []libpak.BuildpackDependencyLicense{
{
Type: "test-type",
Expand Down