From 2d49f0cb4ab1996bc203c9a91454021a375e522c Mon Sep 17 00:00:00 2001 From: yxxhero Date: Sat, 15 Nov 2025 11:16:08 +0800 Subject: [PATCH 1/2] Refactor environment variable expansion in PrepareCommands and update tests Signed-off-by: yxxhero --- internal/plugin/subprocess_commands.go | 11 ++++++----- internal/plugin/subprocess_commands_test.go | 13 ++++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/internal/plugin/subprocess_commands.go b/internal/plugin/subprocess_commands.go index e21ec2bab..9a57ed891 100644 --- a/internal/plugin/subprocess_commands.go +++ b/internal/plugin/subprocess_commands.go @@ -82,15 +82,16 @@ func PrepareCommands(cmds []PlatformCommand, expandArgs bool, extraArgs []string if len(cmdParts) == 0 || cmdParts[0] == "" { return "", nil, fmt.Errorf("no plugin command is applicable") } - - main := os.Expand(cmdParts[0], func(key string) string { + envMappingFunc := func(key string) string { return env[key] - }) + } + + main := os.Expand(cmdParts[0], envMappingFunc) baseArgs := []string{} if len(cmdParts) > 1 { for _, cmdPart := range cmdParts[1:] { if expandArgs { - baseArgs = append(baseArgs, os.ExpandEnv(cmdPart)) + baseArgs = append(baseArgs, os.Expand(cmdPart, envMappingFunc)) } else { baseArgs = append(baseArgs, cmdPart) } @@ -99,7 +100,7 @@ func PrepareCommands(cmds []PlatformCommand, expandArgs bool, extraArgs []string for _, arg := range args { if expandArgs { - baseArgs = append(baseArgs, os.ExpandEnv(arg)) + baseArgs = append(baseArgs, os.Expand(arg, envMappingFunc)) } else { baseArgs = append(baseArgs, arg) } diff --git a/internal/plugin/subprocess_commands_test.go b/internal/plugin/subprocess_commands_test.go index c1eba7a55..a916a176f 100644 --- a/internal/plugin/subprocess_commands_test.go +++ b/internal/plugin/subprocess_commands_test.go @@ -16,6 +16,7 @@ limitations under the License. package plugin import ( + "os" "reflect" "runtime" "testing" @@ -224,16 +225,18 @@ func TestPrepareCommandsNoCommands(t *testing.T) { } func TestPrepareCommandsExpand(t *testing.T) { - t.Setenv("TEST", "test") + t.Setenv("TESTX", "testx") cmdMain := "sh" - cmdArgs := []string{"-c", "echo \"${TEST}\""} + cmdArgs := []string{"-c", "echo \"${TESTX}${TESTY}\""} cmds := []PlatformCommand{ {OperatingSystem: "", Architecture: "", Command: cmdMain, Args: cmdArgs}, } - expectedArgs := []string{"-c", "echo \"test\""} + expectedArgs := []string{"-c", "echo \"testxtesty\""} + + env := parseEnv(os.Environ()) + env["TESTY"] = "testy" - env := map[string]string{} cmd, args, err := PrepareCommands(cmds, true, []string{}, env) if err != nil { t.Fatal(err) @@ -253,8 +256,8 @@ func TestPrepareCommandsNoExpand(t *testing.T) { cmds := []PlatformCommand{ {OperatingSystem: "", Architecture: "", Command: cmdMain, Args: cmdArgs}, } + env := parseEnv(os.Environ()) - env := map[string]string{} cmd, args, err := PrepareCommands(cmds, false, []string{}, env) if err != nil { t.Fatal(err) From 8c870248602e231be274b68b4cfd0f4b8387aa53 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Sun, 16 Nov 2025 10:05:27 +0800 Subject: [PATCH 2/2] update tests Signed-off-by: yxxhero --- internal/plugin/subprocess_commands_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/plugin/subprocess_commands_test.go b/internal/plugin/subprocess_commands_test.go index a916a176f..8e9c1663e 100644 --- a/internal/plugin/subprocess_commands_test.go +++ b/internal/plugin/subprocess_commands_test.go @@ -16,7 +16,6 @@ limitations under the License. package plugin import ( - "os" "reflect" "runtime" "testing" @@ -225,7 +224,6 @@ func TestPrepareCommandsNoCommands(t *testing.T) { } func TestPrepareCommandsExpand(t *testing.T) { - t.Setenv("TESTX", "testx") cmdMain := "sh" cmdArgs := []string{"-c", "echo \"${TESTX}${TESTY}\""} cmds := []PlatformCommand{ @@ -234,8 +232,10 @@ func TestPrepareCommandsExpand(t *testing.T) { expectedArgs := []string{"-c", "echo \"testxtesty\""} - env := parseEnv(os.Environ()) - env["TESTY"] = "testy" + env := map[string]string{ + "TESTX": "testx", + "TESTY": "testy", + } cmd, args, err := PrepareCommands(cmds, true, []string{}, env) if err != nil { @@ -250,13 +250,15 @@ func TestPrepareCommandsExpand(t *testing.T) { } func TestPrepareCommandsNoExpand(t *testing.T) { - t.Setenv("TEST", "test") cmdMain := "sh" cmdArgs := []string{"-c", "echo \"${TEST}\""} cmds := []PlatformCommand{ {OperatingSystem: "", Architecture: "", Command: cmdMain, Args: cmdArgs}, } - env := parseEnv(os.Environ()) + + env := map[string]string{ + "TEST": "test", + } cmd, args, err := PrepareCommands(cmds, false, []string{}, env) if err != nil {