From f21b2c8e9bbacf15d5a8ae7c2c5a8ae6c5504096 Mon Sep 17 00:00:00 2001 From: Anurag Sharma Date: Thu, 6 Mar 2025 17:24:41 +0530 Subject: [PATCH] test(provisioner): add test for shebang within inline script --- provisioner/shell/provisioner_test.go | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/provisioner/shell/provisioner_test.go b/provisioner/shell/provisioner_test.go index 40da9b5ea..f50a27d12 100644 --- a/provisioner/shell/provisioner_test.go +++ b/provisioner/shell/provisioner_test.go @@ -98,6 +98,50 @@ func TestProvisionerPrepare_InlineShebang(t *testing.T) { } } +func TestProvisionerPrepare_ShebangWithinInline(t *testing.T) { + config := testConfig() + script := `#!/bin/bash + echo "Hello, World!" + ` + config["inline"] = []interface{}{script, "foo", "bar"} + + p := new(Provisioner) + err := p.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + if p.config.InlineShebang != "/bin/bash" { + t.Fatalf("bad value: %s", p.config.InlineShebang) + } + + // Test with InlineShebang, it should override the shebang in the script + config["inline_shebang"] = "foo" + p = new(Provisioner) + err = p.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + if p.config.InlineShebang != "foo" { + t.Fatalf("bad value: %s", p.config.InlineShebang) + } + + // Test with a inline that does not have a shebang + config["inline"] = []interface{}{"foo", "bar"} + delete(config, "inline_shebang") + p = new(Provisioner) + err = p.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + if p.config.InlineShebang != "/bin/sh -e" { + t.Fatalf("bad value: %s", p.config.InlineShebang) + } + +} + func TestProvisionerPrepare_InvalidKey(t *testing.T) { var p Provisioner config := testConfig()