Skip to content

Commit

Permalink
Merge branch 'main' into helper-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Mikusa authored Sep 20, 2021
2 parents 41d5305 + 7b0da50 commit 95486d6
Show file tree
Hide file tree
Showing 21 changed files with 640 additions and 197 deletions.
7 changes: 0 additions & 7 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: daily
labels:
- semver:patch
- type:dependency-upgrade
- package-ecosystem: gomod
directory: /
schedule:
Expand Down
2 changes: 1 addition & 1 deletion .github/pipeline-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.1
1.10.5
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
restore-keys: ${{ runner.os }}-go-
- uses: actions/setup-go@v2
with:
go-version: "1.15"
go-version: "1.16"
- name: Install richgo
run: |
#!/usr/bin/env bash
Expand All @@ -37,7 +37,7 @@ jobs:
"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.3
RICHGO_VERSION: 0.3.6
- name: Run Tests
run: |
#!/usr/bin/env bash
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/update-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
steps:
- uses: actions/setup-go@v2
with:
go-version: "1.15"
go-version: "1.16"
- name: Install octo
run: |
#!/usr/bin/env bash
Expand Down Expand Up @@ -64,6 +64,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.JAVA_GITHUB_TOKEN }}
- uses: peter-evans/create-pull-request@v3
with:
author: ${{ secrets.JAVA_GITHUB_USERNAME }} <${{ secrets.JAVA_GITHUB_USERNAME }}@users.noreply.github.com>
body: |-
Bumps pipeline from `${{ steps.pipeline.outputs.old-version }}` to `${{ steps.pipeline.outputs.new-version }}`.
Expand Down
18 changes: 15 additions & 3 deletions bard/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,25 @@ func NewLoggerWithOptions(writer io.Writer, options ...Option) Logger {
func NewLogger(writer io.Writer) Logger {
var options []Option

if _, ok := os.LookupEnv("BP_DEBUG"); ok {
options = append(options, WithDebug(writer))
}
// check for presence and value of log level environment variable
options = LogLevel(options, writer)

return NewLoggerWithOptions(writer, options...)
}

func LogLevel(options []Option, writer io.Writer) []Option {

// Check for older log level env variable
_, dbSet := os.LookupEnv("BP_DEBUG")

// Then check for common buildpack log level env variable - if either are set to DEBUG/true, enable Debug Writer
if level, ok := os.LookupEnv("BP_LOG_LEVEL"); (ok && strings.ToLower(level) == "debug") || dbSet {

options = append(options, WithDebug(writer))
}
return options
}

// Body formats using the default formats for its operands and logs a message to the configured body writer. Spaces
// are added between operands when neither is a string.
func (l Logger) Body(a ...interface{}) {
Expand Down
15 changes: 15 additions & 0 deletions bard/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ func testLogger(t *testing.T, context spec.G, it spec.S) {
})
})

context("with BP_LOG_LEVEL set to DEBUG", func() {
it.Before(func() {
Expect(os.Setenv("BP_LOG_LEVEL", "DEBUG")).To(Succeed())
l = bard.NewLogger(b)
})

it.After(func() {
Expect(os.Unsetenv("BP_LOG_LEVEL")).To(Succeed())
})

it("configures debug", func() {
Expect(l.IsDebugEnabled()).To(BeTrue())
})
})

context("with debug disabled", func() {
it.Before(func() {
l = bard.NewLoggerWithOptions(b)
Expand Down
5 changes: 4 additions & 1 deletion build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})

it("handles error from Builder", func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), []byte(`[buildpack]
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), []byte(`
api = "0.6"
[buildpack]
name = "test-name"
version = "test-version"`),
0644)).To(Succeed())
Expand Down
19 changes: 9 additions & 10 deletions buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/buildpacks/libcnb"
"github.com/heroku/color"
"github.com/mattn/go-shellwords"

"github.com/paketo-buildpacks/libpak/bard"
)
Expand Down Expand Up @@ -85,9 +84,9 @@ type BuildpackDependency struct {
Licenses []BuildpackDependencyLicense `toml:"licenses"`
}

// AsBuildpackPlanEntry renders the dependency as a BuildpackPlanEntry.
func (b BuildpackDependency) AsBuildpackPlanEntry() libcnb.BuildpackPlanEntry {
return libcnb.BuildpackPlanEntry{
// AsBOMEntry renders a bill of materials entry describing the dependency.
func (b BuildpackDependency) AsBOMEntry() libcnb.BOMEntry {
return libcnb.BOMEntry{
Name: b.ID,
Metadata: map[string]interface{}{
"name": b.Name,
Expand Down Expand Up @@ -278,15 +277,11 @@ func NewConfigurationResolver(buildpack libcnb.Buildpack, logger *bard.Logger) (

for _, c := range md.Configurations {
s, _ := cr.Resolve(c.Name)
p, err := shellwords.Parse(s)
if err != nil {
return ConfigurationResolver{}, fmt.Errorf("unable to parse value\n%w", err)
}

e := configurationEntry{
Name: c.Name,
Description: c.Description,
Value: strings.Join(p, " "),
Value: s,
}

if l := len(e.Name); l > nameLength {
Expand Down Expand Up @@ -442,8 +437,12 @@ func (d *DependencyResolver) Resolve(id string, version string) (BuildpackDepend
}

func (DependencyResolver) contains(candidates []string, value string) bool {
if len(candidates) == 0 {
return true
}

for _, c := range candidates {
if c == value {
if c == value || c == "*" {
return true
}
}
Expand Down
66 changes: 64 additions & 2 deletions buildpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
Expect = NewWithT(t).Expect
)

it("renders dependency as an BuildpackPlanEntry", func() {
it("renders dependency as a BOMEntry", func() {
dependency := libpak.BuildpackDependency{
ID: "test-id",
Name: "test-name",
Expand All @@ -49,7 +49,7 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
},
}

Expect(dependency.AsBuildpackPlanEntry()).To(Equal(libcnb.BuildpackPlanEntry{
Expect(dependency.AsBOMEntry()).To(Equal(libcnb.BOMEntry{
Name: dependency.ID,
Metadata: map[string]interface{}{
"name": dependency.Name,
Expand Down Expand Up @@ -286,6 +286,68 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
}))
})

it("filters by stack and supports the wildcard stack", func() {
resolver.Dependencies = []libpak.BuildpackDependency{
{
ID: "test-id",
Name: "test-name",
Version: "1.0",
URI: "test-uri",
SHA256: "test-sha256",
Stacks: []string{"test-stack-1", "test-stack-2"},
},
{
ID: "test-id",
Name: "test-name",
Version: "1.0",
URI: "test-uri",
SHA256: "test-sha256",
Stacks: []string{"*"},
},
}
resolver.StackID = "test-stack-3"

Expect(resolver.Resolve("test-id", "1.0")).To(Equal(libpak.BuildpackDependency{
ID: "test-id",
Name: "test-name",
Version: "1.0",
URI: "test-uri",
SHA256: "test-sha256",
Stacks: []string{"*"},
}))
})

it("filters by stack and treats no stacks as the wildcard stack", func() {
resolver.Dependencies = []libpak.BuildpackDependency{
{
ID: "test-id",
Name: "test-name",
Version: "1.0",
URI: "test-uri",
SHA256: "test-sha256",
Stacks: []string{"test-stack-1", "test-stack-2"},
},
{
ID: "test-id",
Name: "test-name",
Version: "1.0",
URI: "test-uri",
SHA256: "test-sha256",
Stacks: []string{},
},
}
resolver.StackID = "test-stack-3"

Expect(resolver.Resolve("test-id", "1.0")).To(Equal(libpak.BuildpackDependency{
ID: "test-id",
Name: "test-name",
Version: "1.0",
URI: "test-uri",
SHA256: "test-sha256",
Stacks: []string{},
}))
})

it("returns the best dependency", func() {
resolver.Dependencies = []libpak.BuildpackDependency{
{
Expand Down
38 changes: 38 additions & 0 deletions dependency_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -187,6 +188,43 @@ func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...Reque
}

func (d DependencyCache) download(uri string, destination string, mods ...RequestModifierFunc) error {
url, err := url.Parse(uri)
if err != nil {
return fmt.Errorf("unable to parse URI %s\n%w", uri, err)
}

if url.Scheme == "file" {
return d.downloadFile(url.Path, destination, mods...)
}

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

func (d DependencyCache) downloadFile(source string, destination string, mods ...RequestModifierFunc) error {
if err := os.MkdirAll(filepath.Dir(destination), 0755); err != nil {
return fmt.Errorf("unable to make directory %s\n%w", filepath.Dir(destination), err)
}

out, err := os.OpenFile(destination, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("unable to open destination file %s\n%w", destination, err)
}
defer out.Close()

input, err := os.Open(source)
if err != nil {
return fmt.Errorf("unable to open source file %s\n%w", source, err)
}
defer out.Close()

if _, err := io.Copy(out, input); err != nil {
return fmt.Errorf("unable to copy from %s to %s\n%w", source, destination, err)
}

return nil
}

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 Down
22 changes: 21 additions & 1 deletion dependency_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
Expect(ioutil.ReadAll(a)).To(Equal([]byte("test-fixture")))
})

context("uri is overridden", func() {
context("uri is overridden HTTP", func() {
it.Before(func() {
dependencyCache.Mappings = map[string]string{
dependency.SHA256: fmt.Sprintf("%s/override-path", server.URL()),
Expand All @@ -243,6 +243,26 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
})
})

context("uri is overridden FILE", func() {
it.Before(func() {
sourcePath, err := ioutil.TempDir("", "dependency-source-path")
Expect(err).NotTo(HaveOccurred())
sourceFile := filepath.Join(sourcePath, "source-file")
Expect(ioutil.WriteFile(sourceFile, []byte("test-fixture"), 0644)).ToNot(HaveOccurred())

dependencyCache.Mappings = map[string]string{
dependency.SHA256: fmt.Sprintf("file://%s", sourceFile),
}
})

it("downloads from override filesystem", func() {
a, err := dependencyCache.Artifact(dependency)
Expect(err).NotTo(HaveOccurred())

Expect(ioutil.ReadAll(a)).To(Equal([]byte("test-fixture")))
})
})

it("fails with invalid SHA256", func() {
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "invalid-fixture"))

Expand Down
5 changes: 4 additions & 1 deletion detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
})

it("handles error from Detector", func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), []byte(`[buildpack]
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), []byte(`
api = "0.6"
[buildpack]
name = "test-name"
version = "test-version"`),
0644)).To(Succeed())
Expand Down
15 changes: 7 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ module github.com/paketo-buildpacks/libpak
go 1.15

require (
github.com/Masterminds/semver/v3 v3.1.0
github.com/buildpacks/libcnb v1.18.1
github.com/creack/pty v1.1.11
github.com/Masterminds/semver/v3 v3.1.1
github.com/buildpacks/libcnb v1.22.0
github.com/creack/pty v1.1.15
github.com/heroku/color v0.0.6
github.com/imdario/mergo v0.3.11
github.com/mattn/go-shellwords v1.0.10
github.com/onsi/gomega v1.10.3
github.com/pelletier/go-toml v1.8.1
github.com/imdario/mergo v0.3.12
github.com/onsi/gomega v1.16.0
github.com/pelletier/go-toml v1.9.4
github.com/sclevine/spec v1.4.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.0
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
)
Loading

0 comments on commit 95486d6

Please sign in to comment.