From 074a74ec38826d2eaeca8076e20697219777a43b Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 1 Feb 2019 15:17:09 +0100 Subject: [PATCH 1/2] make sure 'only' completely ignores post-processor before this commit, if one would put a 'only' inside the post-processor definition, the post process could be skipped --- command/build_test.go | 9 +++++++-- command/test-fixtures/build-only/template.json | 10 ++++++++++ packer/core.go | 13 ++++++++----- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/command/build_test.go b/command/build_test.go index 5e56d81f1..648ffdfe5 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -37,6 +37,10 @@ func TestBuildOnlyFileCommaFlags(t *testing.T) { if fileExists("cherry.txt") { t.Error("Expected NOT to find cherry.txt") } + + if !fileExists("tomato.txt") { + t.Error("Expected to find tomato.txt") + } } func TestBuildStdin(t *testing.T) { @@ -114,12 +118,12 @@ func TestBuildExceptFileCommaFlags(t *testing.T) { fatalCommand(t, c.Meta) } - for _, f := range []string{"chocolate.txt", "apple.txt", "peach.txt"} { + for _, f := range []string{"chocolate.txt", "apple.txt"} { if fileExists(f) { t.Errorf("Expected NOT to find %s", f) } } - for _, f := range []string{"vanilla.txt", "cherry.txt", "pear.txt"} { + for _, f := range []string{"vanilla.txt", "cherry.txt", "pear.txt", "peach.txt"} { if !fileExists(f) { t.Errorf("Expected to find %s", f) } @@ -169,4 +173,5 @@ func cleanup() { os.RemoveAll("apple.txt") os.RemoveAll("peach.txt") os.RemoveAll("pear.txt") + os.RemoveAll("tomato.txt") } diff --git a/command/test-fixtures/build-only/template.json b/command/test-fixtures/build-only/template.json index af09b6cbf..9ca20adca 100644 --- a/command/test-fixtures/build-only/template.json +++ b/command/test-fixtures/build-only/template.json @@ -38,6 +38,16 @@ "type": "shell-local", "inline": [ "touch pear.txt" ] } + ], + [ + { + "only": [ + "vanilla" + ], + "name": "tomato", + "type": "shell-local", + "inline": [ "touch tomato.txt" ] + } ] ] } \ No newline at end of file diff --git a/packer/core.go b/packer/core.go index feec80d24..d62d85564 100644 --- a/packer/core.go +++ b/packer/core.go @@ -181,12 +181,15 @@ func (c *Core) Build(n string) (Build, error) { current := make([]coreBuildPostProcessor, 0, len(rawPs)) for _, rawP := range rawPs { // If we skip, ignore - rawP.OnlyExcept.Except = append(rawP.OnlyExcept.Except, c.except...) - if rawP.OnlyExcept.Skip(rawName) { - continue + foundExcept := false + excepts := append(rawP.OnlyExcept.Except, c.except...) + for _, except := range excepts { + if except == rawName || except == rawP.Name { + foundExcept = true + } } - if rawP.OnlyExcept.Skip(rawP.Name) { - break + if foundExcept { + continue } // Get the post-processor From dd3e2c255c485eb9bff0fbe4248ffa18f76d4b56 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 1 Feb 2019 15:50:06 +0100 Subject: [PATCH 2/2] respect a 'only' defined in a post-processor --- command/build_test.go | 6 +++--- packer/core.go | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/command/build_test.go b/command/build_test.go index 648ffdfe5..12f9709d2 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -108,7 +108,7 @@ func TestBuildExceptFileCommaFlags(t *testing.T) { } args := []string{ - "-except=chocolate,apple", + "-except=chocolate,vanilla", filepath.Join(testFixture("build-only"), "template.json"), } @@ -118,12 +118,12 @@ func TestBuildExceptFileCommaFlags(t *testing.T) { fatalCommand(t, c.Meta) } - for _, f := range []string{"chocolate.txt", "apple.txt"} { + for _, f := range []string{"chocolate.txt", "vanilla.txt", "tomato.txt"} { if fileExists(f) { t.Errorf("Expected NOT to find %s", f) } } - for _, f := range []string{"vanilla.txt", "cherry.txt", "pear.txt", "peach.txt"} { + for _, f := range []string{"apple.txt", "cherry.txt", "pear.txt", "peach.txt"} { if !fileExists(f) { t.Errorf("Expected to find %s", f) } diff --git a/packer/core.go b/packer/core.go index d62d85564..a059f90c9 100644 --- a/packer/core.go +++ b/packer/core.go @@ -180,11 +180,13 @@ func (c *Core) Build(n string) (Build, error) { for _, rawPs := range c.Template.PostProcessors { current := make([]coreBuildPostProcessor, 0, len(rawPs)) for _, rawP := range rawPs { - // If we skip, ignore + if rawP.Skip(rawName) { + continue + } + // -except skips post-processor & build foundExcept := false - excepts := append(rawP.OnlyExcept.Except, c.except...) - for _, except := range excepts { - if except == rawName || except == rawP.Name { + for _, except := range c.except { + if except == rawP.Name { foundExcept = true } }