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

remove unnecessary util.Dedup and util.RightJoinMap methods #1705

Merged
merged 5 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
8 changes: 4 additions & 4 deletions pkg/build/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"text/template"

Expand All @@ -36,7 +37,6 @@ import (

"chainguard.dev/melange/pkg/config"
"chainguard.dev/melange/pkg/sca"
"chainguard.dev/melange/pkg/util"

"chainguard.dev/apko/pkg/apk/tarball"
"github.com/chainguard-dev/clog"
Expand Down Expand Up @@ -342,15 +342,15 @@ func (pc *PackageBuild) GenerateDependencies(ctx context.Context, hdl sca.SCAHan
unvendored := removeSelfProvidedDeps(generated.Runtime, generated.Vendored)

newruntime := append(pc.Dependencies.Runtime, unvendored...)
pc.Dependencies.Runtime = util.Dedup(newruntime)
pc.Dependencies.Runtime = slices.Compact(slices.Sorted(slices.Values(newruntime)))

newprovides := append(pc.Dependencies.Provides, generated.Provides...)
pc.Dependencies.Provides = util.Dedup(newprovides)
pc.Dependencies.Provides = slices.Compact(slices.Sorted(slices.Values(newprovides)))

pc.Dependencies.Runtime = removeSelfProvidedDeps(pc.Dependencies.Runtime, pc.Dependencies.Provides)

// Sets .PKGINFO `# vendored = ...` comments; does not affect resolution.
pc.Dependencies.Vendored = util.Dedup(generated.Vendored)
pc.Dependencies.Vendored = slices.Compact(slices.Sorted(slices.Values(generated.Vendored)))

pc.Dependencies.Summarize(ctx)

Expand Down
5 changes: 2 additions & 3 deletions pkg/sca/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"testing"

"chainguard.dev/melange/pkg/config"
"chainguard.dev/melange/pkg/util"
"github.com/chainguard-dev/clog/slogtest"
"github.com/google/go-cmp/cmp"
)
Expand All @@ -36,13 +35,13 @@ func TestGoFipsBinDeps(t *testing.T) {
t.Fatal(err)
}
want := config.Dependencies{
Runtime: util.Dedup([]string{
Runtime: []string{
"openssl-config-fipshardened",
"so:ld-linux-x86-64.so.2",
"so:libc.so.6",
"so:libcrypto.so.3",
"so:libssl.so.3",
}),
},
Provides: []string{"cmd:go-fips-bin=0.0.1-r0"},
}
if diff := cmp.Diff(want, got); diff != "" {
Expand Down
7 changes: 3 additions & 4 deletions pkg/sca/sca.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/chainguard-dev/go-pkgconfig"

"chainguard.dev/melange/pkg/config"
"chainguard.dev/melange/pkg/util"
)

var libDirs = []string{"lib/", "usr/lib/", "lib64/", "usr/lib64/"}
Expand Down Expand Up @@ -783,9 +782,9 @@ func Analyze(ctx context.Context, hdl SCAHandle, generated *config.Dependencies)
}
}

generated.Runtime = util.Dedup(generated.Runtime)
generated.Provides = util.Dedup(generated.Provides)
generated.Vendored = util.Dedup(generated.Vendored)
generated.Runtime = slices.Compact(slices.Sorted(slices.Values(generated.Runtime)))
generated.Provides = slices.Compact(slices.Sorted(slices.Values(generated.Provides)))
generated.Vendored = slices.Compact(slices.Sorted(slices.Values(generated.Vendored)))

if hdl.Options().NoCommands {
generated.Provides = slices.DeleteFunc(generated.Provides, func(s string) bool {
Expand Down
33 changes: 18 additions & 15 deletions pkg/sca/sca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"chainguard.dev/apko/pkg/apk/apk"
"chainguard.dev/apko/pkg/apk/expandapk"
"chainguard.dev/melange/pkg/config"
"chainguard.dev/melange/pkg/util"
"github.com/chainguard-dev/clog/slogtest"
"github.com/google/go-cmp/cmp"
"gopkg.in/ini.v1"
Expand Down Expand Up @@ -151,16 +150,16 @@ func TestExecableSharedObjects(t *testing.T) {
}

want := config.Dependencies{
Runtime: util.Dedup([]string{
Runtime: []string{
"so:ld-linux-aarch64.so.1",
"so:libc.so.6",
"so:libcap.so.2",
"so:libpsx.so.2",
}),
Provides: util.Dedup([]string{
},
Provides: []string{
"so:libcap.so.2=2",
"so:libpsx.so.2=2",
}),
},
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Analyze(): (-want, +got):\n%s", diff)
Expand All @@ -181,18 +180,22 @@ func TestVendoredPkgConfig(t *testing.T) {
}

want := config.Dependencies{
Runtime: util.Dedup([]string{
Runtime: []string{
// We only include libecpg_compat.so.3 to test that "libexec" isn't treated as a library directory.
// These are dependencies of libecpg_compat.so.3, but if we had the whole neon APK it would look different.
"so:libecpg.so.6", "so:libpgtypes.so.3", "so:libpq.so.5", "so:libc.so.6", "so:ld-linux-aarch64.so.1",
}),
Vendored: util.Dedup([]string{
"so:libecpg_compat.so.3=3",
"so:ld-linux-aarch64.so.1",
"so:libc.so.6",
"so:libecpg.so.6",
"so:libpgtypes.so.3",
"so:libpq.so.5",
},
Vendored: []string{
"pc:libecpg=4604-r0",
"pc:libecpg_compat=4604-r0",
"pc:libpgtypes=4604-r0",
"pc:libpq=4604-r0",
}),
"so:libecpg_compat.so.3=3",
},
}

if diff := cmp.Diff(want, got); diff != "" {
Expand Down Expand Up @@ -252,7 +255,7 @@ func TestUnstableSonames(t *testing.T) {
}

want := config.Dependencies{
Runtime: util.Dedup([]string{
Runtime: []string{
"so:ld-linux-aarch64.so.1",
"so:libaws-c-auth.so.1.0.0",
"so:libaws-c-cal.so.1.0.0",
Expand All @@ -262,7 +265,7 @@ func TestUnstableSonames(t *testing.T) {
"so:libaws-c-s3.so.0unstable",
"so:libaws-checksums.so.1.0.0",
"so:libc.so.6",
}),
},
Provides: []string{"so:libaws-c-s3.so.0unstable=0"},
}

Expand All @@ -278,13 +281,13 @@ func TestShbangDeps(t *testing.T) {
defer th.exp.Close()

want := config.Dependencies{
Runtime: util.Dedup([]string{
Runtime: []string{
"cmd:bash",
"cmd:envDashSCmd",
"cmd:python3.12",
"so:ld-linux-x86-64.so.2",
"so:libc.so.6",
}),
},
Provides: nil,
}

Expand Down
15 changes: 1 addition & 14 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@

package util

import (
"cmp"
"slices"
)

// Given a left and right map, perform a right join and return the result
func RightJoinMap(left map[string]string, right map[string]string) map[string]string {
// this is the worst case possible length, assuming no overlaps.
length := len(left) + len(right)
output := make(map[string]string, length)
output := map[string]string{}

// copy the left-side first
for k, v := range left {
Expand All @@ -37,9 +30,3 @@ func RightJoinMap(left map[string]string, right map[string]string) map[string]st

return output
}

// Dedup wraps slices.Sort and slices.Compact to deduplicate a slice.
func Dedup[S ~[]E, E cmp.Ordered](s S) S {
slices.Sort(s)
return slices.Compact(s)
}
14 changes: 0 additions & 14 deletions pkg/util/util_test.go

This file was deleted.

Loading