Skip to content

Commit

Permalink
Add Makefile and fix some linting issues
Browse files Browse the repository at this point in the history
Signed-off-by: Ralf Pannemans <[email protected]>
  • Loading branch information
c0d1ngm0nk3y committed Oct 13, 2023
1 parent 64d6194 commit e571619
Show file tree
Hide file tree
Showing 30 changed files with 1,306 additions and 84 deletions.
31 changes: 31 additions & 0 deletions .github/pipeline-descriptor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,34 @@ github:
codeowners:
- path: "*"
owner: "@paketo-buildpacks/java-maintainers"

test:
steps:
- name: Install richgo
run: |
#!/usr/bin/env bash
set -euo pipefail
echo "Installing richgo ${RICHGO_VERSION}"
mkdir -p "${HOME}"/bin
echo "${HOME}/bin" >> "${GITHUB_PATH}"
curl \
--location \
--show-error \
--silent \
"https://github.com/kyoh86/richgo/releases/download/v${RICHGO_VERSION}/richgo_${RICHGO_VERSION}_linux_amd64.tar.gz" \
| tar -C "${HOME}"/bin -xz richgo
env:
RICHGO_VERSION: 0.3.10
- name: Run Tests
run: |
#!/usr/bin/env bash
set -euo pipefail
GOCMD=richgo make
env:
RICHGO_FORCE_COLOR: "1"
2 changes: 1 addition & 1 deletion .github/workflows/pb-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ jobs:
set -euo pipefail
richgo test ./...
GOCMD=richgo make
env:
RICHGO_FORCE_COLOR: "1"
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Go parameters
GOCMD?=go
GO_VERSION=$(shell go list -m -f "{{.GoVersion}}")
PACKAGE_BASE=github.com/buildpacks/libpak/v2

all: test

install-goimports:
@echo "> Installing goimports..."
cd tools && $(GOCMD) install golang.org/x/tools/cmd/goimports

format: install-goimports
@echo "> Formating code..."
@goimports -l -w -local ${PACKAGE_BASE} .

install-golangci-lint:
@echo "> Installing golangci-lint..."
cd tools && $(GOCMD) install github.com/golangci/golangci-lint/cmd/golangci-lint

lint: install-golangci-lint
@echo "> Linting code..."
@golangci-lint run -c golangci.yaml

test: format lint
$(GOCMD) test -parallel=1 -count=1 -v ./...
14 changes: 7 additions & 7 deletions buildmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,18 @@ type BuildModuleDependency struct {

// 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 BuildModuleDependency) Equals(b2 BuildModuleDependency) bool {
b1.DeprecationDate = b1.DeprecationDate.Truncate(time.Second).In(time.UTC)
func (b BuildModuleDependency) Equals(b2 BuildModuleDependency) bool {
b.DeprecationDate = b.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(b.CPEs) == 0 {
b.CPEs = nil
}
if len(b2.CPEs) == 0 {
b2.CPEs = nil
}

return reflect.DeepEqual(b1, b2)
return reflect.DeepEqual(b, b2)
}

// AsSyftArtifact renders a bill of materials entry describing the dependency as Syft.
Expand Down Expand Up @@ -436,8 +436,8 @@ type DependencyResolver struct {
}

// NewDependencyResolver creates a new instance from the build module metadata and stack id.
func NewDependencyResolver(md BuildModuleMetadata, stackId string) (DependencyResolver, error) {
return DependencyResolver{Dependencies: md.Dependencies, StackID: stackId}, nil
func NewDependencyResolver(md BuildModuleMetadata, stackID string) (DependencyResolver, error) {
return DependencyResolver{Dependencies: md.Dependencies, StackID: stackID}, nil
}

// NoValidDependenciesError is returned when the resolver cannot find any valid dependencies given the constraints.
Expand Down
1 change: 0 additions & 1 deletion buildmodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {

Expect(buff.String()).To(Equal(fmt.Sprintf(" \x1b[33mDeprecation Notice:\x1b[0m\n\x1b[2m \x1b[33mVersion 1.1 of soon-deprecated-dependency will be deprecated after %s.\x1b[0m\x1b[2m\x1b[0m\n\x1b[2m \x1b[33mMigrate your application to a supported version of soon-deprecated-dependency before this time.\x1b[0m\x1b[2m\x1b[0m\n \x1b[33mDeprecation Notice:\x1b[0m\n\x1b[2m \x1b[33mVersion 1.1 of deprecated-dependency is deprecated.\x1b[0m\x1b[2m\x1b[0m\n\x1b[2m \x1b[33mMigrate your application to a supported version of deprecated-dependency.\x1b[0m\x1b[2m\x1b[0m\n", soonDeprecated.Format("2006-01-02"))))
})

})

it("indicates whether error is NoValidDependenciesError", func() {
Expand Down
4 changes: 0 additions & 4 deletions buildpack_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func testBuildpackPlan(t *testing.T, context spec.G, it spec.S) {
)

context("ShallowMerge", func() {

it("merges with empty", func() {
a := libcnb.BuildpackPlanEntry{}
b := libcnb.BuildpackPlanEntry{Name: "test-name"}
Expand Down Expand Up @@ -109,11 +108,9 @@ func testBuildpackPlan(t *testing.T, context spec.G, it spec.S) {
Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
})
})

})

context("PlanEntryResolver", func() {

context("ResolveWithMerge", func() {
var (
resolver = libpak.PlanEntryResolver{}
Expand Down Expand Up @@ -165,7 +162,6 @@ func testBuildpackPlan(t *testing.T, context spec.G, it spec.S) {
})

context("Resolve", func() {

it("merges with empty", func() {
a := libcnb.BuildpackPlanEntry{}
b := libcnb.BuildpackPlanEntry{Name: "test-name"}
Expand Down
1 change: 0 additions & 1 deletion carton/build_image_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ func (i BuildImageDependency) Update(options ...Option) {
config.exitHandler.Error(fmt.Errorf("unable to write %s\n%w", i.BuilderPath, err))
return
}

}
6 changes: 3 additions & 3 deletions carton/buildmodule_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,16 @@ func (b BuildModuleDependency) Update(options ...Option) {
}

for _, dep := range dependencies {
depIdUnwrapped, found := dep["id"]
depIDUnwrapped, found := dep["id"]
if !found {
continue
}
depId, ok := depIdUnwrapped.(string)
depID, ok := depIDUnwrapped.(string)
if !ok {
continue
}

if depId == b.ID {
if depID == b.ID {
depVersionUnwrapped, found := dep["version"]
if !found {
continue
Expand Down
1 change: 0 additions & 1 deletion carton/lifecycle_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ func (l LifecycleDependency) Update(options ...Option) {
config.exitHandler.Error(fmt.Errorf("unable to write %s\n%w", l.BuilderPath, err))
return
}

}
6 changes: 3 additions & 3 deletions carton/package_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ func (p PackageDependency) Update(options ...Option) {
continue
}

bpIdUnwrappd, found := bp["id"]
bpIDUnwrappd, found := bp["id"]
if !found {
continue
}

bpId, ok := bpIdUnwrappd.(string)
bpID, ok := bpIDUnwrappd.(string)
if !ok {
continue
}

if bpId == id {
if bpID == id {
bp["version"] = p.Version
}
}
Expand Down
7 changes: 3 additions & 4 deletions carton/package_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ group = [
Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[buildpacks]]
id = "paketo-buildpacks/test-1"
uri = "docker://gcr.io/paketo-buildpacks/test-1:test-version-3"
[[buildpacks]]
id = "paketo-buildpacks/test-2"
uri = "docker://gcr.io/paketo-buildpacks/test-2:test-version-2"`))
Expand All @@ -196,7 +196,7 @@ group = [

Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[dependencies]]
uri = "docker://gcr.io/paketo-buildpacks/test-1:test-version-3"
[[dependencies]]
uri = "docker://gcr.io/paketo-buildpacks/test-2:test-version-2"`))
})
Expand All @@ -217,9 +217,8 @@ group = [

Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[dependencies]]
uri = "docker://docker.io/paketocommunity/test-1:test-version-3"
[[dependencies]]
uri = "docker://docker.io/paketocommunity/test-2:test-version-2"`))
})

}
3 changes: 0 additions & 3 deletions carton/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ include-files = [
"buildpack.toml",
]
`), 0644)).To(Succeed())

})

it.After(func() {
Expand Down Expand Up @@ -355,7 +354,6 @@ include-files = [
"extension.toml",
]
`), 0644)).To(Succeed())

})

it.After(func() {
Expand Down Expand Up @@ -593,5 +591,4 @@ include-files = [
})
})
})

}
26 changes: 13 additions & 13 deletions dependency_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/paketo-buildpacks/libpak/v2/sherpa"
)

type HttpClientTimeouts struct {
type HTTPlientTimeouts struct {
DialerTimeout time.Duration
DialerKeepAlive time.Duration
TLSHandshakeTimeout time.Duration
Expand All @@ -65,7 +65,7 @@ type DependencyCache struct {
Mappings map[string]string

// httpClientTimeouts contains the timeout values used by HTTP client
HttpClientTimeouts HttpClientTimeouts
HTTPClientTimeouts HTTPlientTimeouts
}

// NewDependencyCache creates a new instance setting the default cache path (<BUILDMODULE_PATH>/dependencies) and user
Expand All @@ -84,16 +84,16 @@ func NewDependencyCache(buildModuleID string, buildModuleVersion string, buildMo
}
cache.Mappings = mappings

clientTimeouts, err := customizeHttpClientTimeouts()
clientTimeouts, err := customizeHTTPClientTimeouts()
if err != nil {
return DependencyCache{}, fmt.Errorf("unable to read custom timeout settings\n%w", err)
}
cache.HttpClientTimeouts = *clientTimeouts
cache.HTTPClientTimeouts = *clientTimeouts

return cache, nil
}

func customizeHttpClientTimeouts() (*HttpClientTimeouts, error) {
func customizeHTTPClientTimeouts() (*HTTPlientTimeouts, error) {
rawStr := sherpa.GetEnvWithDefault("BP_DIALER_TIMEOUT", "6")
dialerTimeout, err := strconv.Atoi(rawStr)
if err != nil {
Expand Down Expand Up @@ -124,7 +124,7 @@ func customizeHttpClientTimeouts() (*HttpClientTimeouts, error) {
return nil, fmt.Errorf("unable to convert BP_EXPECT_CONTINUE_TIMEOUT=%s to integer\n%w", rawStr, err)
}

return &HttpClientTimeouts{
return &HTTPlientTimeouts{
DialerTimeout: time.Duration(dialerTimeout) * time.Second,
DialerKeepAlive: time.Duration(dialerKeepAlive) * time.Second,
TLSHandshakeTimeout: time.Duration(tlsHandshakeTimeout) * time.Second,
Expand Down Expand Up @@ -250,7 +250,7 @@ func (d DependencyCache) download(uri string, destination string, mods ...Reques
return d.downloadFile(url.Path, destination, mods...)
}

return d.downloadHttp(uri, destination, mods...)
return d.downloadHTTP(uri, destination, mods...)
}

func (d DependencyCache) downloadFile(source string, destination string, mods ...RequestModifierFunc) error {
Expand All @@ -277,7 +277,7 @@ func (d DependencyCache) downloadFile(source string, destination string, mods ..
return nil
}

func (d DependencyCache) downloadHttp(uri string, destination string, mods ...RequestModifierFunc) error {
func (d DependencyCache) downloadHTTP(uri string, destination string, mods ...RequestModifierFunc) error {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return fmt.Errorf("unable to create new GET request for %s\n%w", uri, err)
Expand All @@ -297,12 +297,12 @@ func (d DependencyCache) downloadHttp(uri string, destination string, mods ...Re
client := http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: d.HttpClientTimeouts.DialerTimeout,
KeepAlive: d.HttpClientTimeouts.DialerKeepAlive,
Timeout: d.HTTPClientTimeouts.DialerTimeout,
KeepAlive: d.HTTPClientTimeouts.DialerKeepAlive,
}).Dial,
TLSHandshakeTimeout: d.HttpClientTimeouts.TLSHandshakeTimeout,
ResponseHeaderTimeout: d.HttpClientTimeouts.ResponseHeaderTimeout,
ExpectContinueTimeout: d.HttpClientTimeouts.ExpectContinueTimeout,
TLSHandshakeTimeout: d.HTTPClientTimeouts.TLSHandshakeTimeout,
ResponseHeaderTimeout: d.HTTPClientTimeouts.ResponseHeaderTimeout,
ExpectContinueTimeout: d.HTTPClientTimeouts.ExpectContinueTimeout,
Proxy: http.ProxyFromEnvironment,
},
}
Expand Down
22 changes: 11 additions & 11 deletions dependency_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
it("set default CachePath and UserAgent", func() {
dependencyCache, err := libpak.NewDependencyCache(ctx.Buildpack.Info.ID, ctx.Buildpack.Info.Version, ctx.Buildpack.Path, ctx.Platform.Bindings, log.NewDiscardLogger())
Expect(err).NotTo(HaveOccurred())
Expect(dependencyCache.CachePath).To(Equal(filepath.Join("some/path/dependencies")))
Expect(dependencyCache.CachePath).To(Equal("some/path/dependencies"))
Expect(dependencyCache.UserAgent).To(Equal("some-buildpack-id/some-buildpack-version"))
Expect(dependencyCache.Mappings).To(Equal(map[string]string{}))
})

it("uses default timeout values", func() {
dependencyCache, err := libpak.NewDependencyCache(ctx.Buildpack.Info.ID, ctx.Buildpack.Info.Version, ctx.Buildpack.Path, ctx.Platform.Bindings, log.NewDiscardLogger())
Expect(err).NotTo(HaveOccurred())
Expect(dependencyCache.HttpClientTimeouts.DialerTimeout).To(Equal(6 * time.Second))
Expect(dependencyCache.HttpClientTimeouts.DialerKeepAlive).To(Equal(60 * time.Second))
Expect(dependencyCache.HttpClientTimeouts.TLSHandshakeTimeout).To(Equal(5 * time.Second))
Expect(dependencyCache.HttpClientTimeouts.ResponseHeaderTimeout).To(Equal(5 * time.Second))
Expect(dependencyCache.HttpClientTimeouts.ExpectContinueTimeout).To(Equal(1 * time.Second))
Expect(dependencyCache.HTTPClientTimeouts.DialerTimeout).To(Equal(6 * time.Second))
Expect(dependencyCache.HTTPClientTimeouts.DialerKeepAlive).To(Equal(60 * time.Second))
Expect(dependencyCache.HTTPClientTimeouts.TLSHandshakeTimeout).To(Equal(5 * time.Second))
Expect(dependencyCache.HTTPClientTimeouts.ResponseHeaderTimeout).To(Equal(5 * time.Second))
Expect(dependencyCache.HTTPClientTimeouts.ExpectContinueTimeout).To(Equal(1 * time.Second))
})

context("custom timeout setttings", func() {
Expand All @@ -85,11 +85,11 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
it("uses custom timeout values", func() {
dependencyCache, err := libpak.NewDependencyCache(ctx.Buildpack.Info.ID, ctx.Buildpack.Info.Version, ctx.Buildpack.Path, ctx.Platform.Bindings, log.NewDiscardLogger())
Expect(err).NotTo(HaveOccurred())
Expect(dependencyCache.HttpClientTimeouts.DialerTimeout).To(Equal(7 * time.Second))
Expect(dependencyCache.HttpClientTimeouts.DialerKeepAlive).To(Equal(50 * time.Second))
Expect(dependencyCache.HttpClientTimeouts.TLSHandshakeTimeout).To(Equal(2 * time.Second))
Expect(dependencyCache.HttpClientTimeouts.ResponseHeaderTimeout).To(Equal(3 * time.Second))
Expect(dependencyCache.HttpClientTimeouts.ExpectContinueTimeout).To(Equal(2 * time.Second))
Expect(dependencyCache.HTTPClientTimeouts.DialerTimeout).To(Equal(7 * time.Second))
Expect(dependencyCache.HTTPClientTimeouts.DialerKeepAlive).To(Equal(50 * time.Second))
Expect(dependencyCache.HTTPClientTimeouts.TLSHandshakeTimeout).To(Equal(2 * time.Second))
Expect(dependencyCache.HTTPClientTimeouts.ResponseHeaderTimeout).To(Equal(3 * time.Second))
Expect(dependencyCache.HTTPClientTimeouts.ExpectContinueTimeout).To(Equal(2 * time.Second))
})
})

Expand Down
2 changes: 0 additions & 2 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func testFormatter(t *testing.T, context spec.G, it spec.S) {
)

context("DependenciesFormatter", func() {

it("formats contents", func() {
deps := []libpak.BuildModuleDependency{
{
Expand All @@ -48,6 +47,5 @@ func testFormatter(t *testing.T, context spec.G, it spec.S) {
Expect(libpak.DependenciesFormatter(deps).String()).
To(Equal("[(test-id-1, 1.1.1, [test-stack-1]) (test-id-2, 2.2.2, [test-stack-2])]"))
})

})
}
Loading

0 comments on commit e571619

Please sign in to comment.