diff --git a/internal/builder/builder.go b/internal/builder/builder.go index a6b7273ad9..f0afe01690 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -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") } diff --git a/internal/style/style.go b/internal/style/style.go index c14797bcde..0ce59a5bca 100644 --- a/internal/style/style.go +++ b/internal/style/style.go @@ -2,6 +2,8 @@ package style import ( "fmt" + "sort" + "strings" "github.com/heroku/color" ) @@ -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...) diff --git a/internal/style/style_test.go b/internal/style/style_test.go new file mode 100644 index 0000000000..e8a2685325 --- /dev/null +++ b/internal/style/style_test.go @@ -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") + }) + }) +}