Skip to content

Commit

Permalink
Merge pull request #240 from sap-contributions/metadata-equals
Browse files Browse the repository at this point in the history
Do not check DeprecationDate when comparing dependencies
  • Loading branch information
dmikusa committed May 26, 2023
2 parents 647f13f + 919c85e commit 0e2d0f4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
17 changes: 17 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,22 @@ 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 = b1.DeprecationDate.Truncate(time.Second).In(time.UTC)
b2.DeprecationDate = b2.DeprecationDate.Truncate(time.Second).In(time.UTC)

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

return reflect.DeepEqual(b1, b2)
}

// 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

0 comments on commit 0e2d0f4

Please sign in to comment.