Skip to content

Commit

Permalink
Merge pull request #993 from Clivern/feature/debug-env
Browse files Browse the repository at this point in the history
Provide debug output of env vars used

Signed-off-by: David Freilich <dfreilich@vmware.com>
  • Loading branch information
dfreilich authored Dec 22, 2020
2 parents edce72d + da89bca commit b94eca8
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,15 @@ func (b *Builder) Save(logger logging.Logger, creatorMetadata CreatorMetadata) e
return errors.Wrap(err, "adding stack.tar layer")
}

if len(b.env) > 0 {
logger.Debugf("Provided Environment Variables\n %s", style.Map(b.env, " ", "\n"))
}

envTar, err := b.envLayer(tmpDir, b.env)
if err != nil {
return err
}

if err := b.image.AddLayer(envTar); err != nil {
return errors.Wrap(err, "adding env layer")
}
Expand Down
23 changes: 23 additions & 0 deletions internal/style/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package style

import (
"fmt"
"sort"
"strings"

"github.com/heroku/color"
)
Expand All @@ -13,6 +15,27 @@ var Symbol = func(value string) string {
return "'" + value + "'"
}

var Map = func(value map[string]string, prefix, separator string) string {
result := ""

var keys []string

for key := range value {
keys = append(keys, key)
}

sort.Strings(keys)

for _, key := range keys {
result += fmt.Sprintf("%s%s=%s%s", prefix, key, value[key], separator)
}

if color.Enabled() {
return Key(strings.TrimSpace(result))
}
return "'" + strings.TrimSpace(result) + "'"
}

var SymbolF = func(format string, a ...interface{}) string {
if color.Enabled() {
return Key(format, a...)
Expand Down
88 changes: 88 additions & 0 deletions internal/style/style_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package style_test

import (
"testing"

"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpacks/pack/internal/style"
h "github.com/buildpacks/pack/testhelpers"
)

func TestStyle(t *testing.T) {
color.Disable(true)
defer color.Disable(false)
spec.Run(t, "testStyle", testStyle, spec.Parallel(), spec.Report(report.Terminal{}))
}

func testStyle(t *testing.T, when spec.G, it spec.S) {
when("#Symbol", func() {
it("It should return the expected value", func() {
h.AssertEq(t, style.Symbol("Symbol"), "'Symbol'")
})

it("It should return an empty string", func() {
h.AssertEq(t, style.Symbol(""), "''")
})

it("It should return the expected value while color enabled", func() {
color.Disable(false)
defer color.Disable(true)
h.AssertEq(t, style.Symbol("Symbol"), "\x1b[94mSymbol\x1b[0m")
})

it("It should return an empty string while color enabled", func() {
color.Disable(false)
defer color.Disable(true)
h.AssertEq(t, style.Symbol(""), "\x1b[94m\x1b[0m")
})
})

when("#SymbolF", func() {
it("It should return the expected value", func() {
h.AssertEq(t, style.SymbolF("values %s %d", "hello", 1), "'values hello 1'")
})

it("It should return an empty string", func() {
h.AssertEq(t, style.SymbolF(""), "''")
})

it("It should return the expected value while color enabled", func() {
color.Disable(false)
defer color.Disable(true)
h.AssertEq(t, style.SymbolF("values %s %d", "hello", 1), "\x1b[94mvalues hello 1\x1b[0m")
})

it("It should return an empty string while color enabled", func() {
color.Disable(false)
defer color.Disable(true)
h.AssertEq(t, style.SymbolF(""), "\x1b[94m\x1b[0m")
})
})

when("#Map", func() {
it("It should return a string with all key value pairs", func() {
h.AssertEq(t, style.Map(map[string]string{"FOO": "foo", "BAR": "bar"}, "", " "), "'BAR=bar FOO=foo'")
h.AssertEq(t, style.Map(map[string]string{"BAR": "bar", "FOO": "foo"}, " ", "\n"), "'BAR=bar\n FOO=foo'")
})

it("It should return a string with all key value pairs while color enabled", func() {
color.Disable(false)
defer color.Disable(true)
h.AssertEq(t, style.Map(map[string]string{"FOO": "foo", "BAR": "bar"}, "", " "), "\x1b[94mBAR=bar FOO=foo\x1b[0m")
h.AssertEq(t, style.Map(map[string]string{"BAR": "bar", "FOO": "foo"}, " ", "\n"), "\x1b[94mBAR=bar\n FOO=foo\x1b[0m")
})

it("It should return an empty string", func() {
h.AssertEq(t, style.Map(map[string]string{}, "", " "), "''")
})

it("It should return an empty string while color enabled", func() {
color.Disable(false)
defer color.Disable(true)
h.AssertEq(t, style.Map(map[string]string{}, "", " "), "\x1b[94m\x1b[0m")
})
})
}

0 comments on commit b94eca8

Please sign in to comment.