Repository: helm/helm
File: internal/plugin/subprocess_commands_test.go (Go)

Code before:
}

func TestPrepareCommandsNoMatch(t *testing.T) {
	cmds := []PlatformCommand{
		{OperatingSystem: "no-os", Architecture: "no-arch", Command: "sh", Args: []string{"-c", "echo \"test\""}},
		{OperatingSystem: runtime.GOOS, Architecture: "no-arch", Command: "sh", Args: []string{"-c", "echo \"test\""}},
		{OperatingSystem: "no-os", Architecture: runtime.GOARCH, Command: "sh", Args: []string{"-c", "echo \"test\""}},
	}

	env := map[string]string{}
	if _, _, err := PrepareCommands(cmds, true, []string{}, env); err == nil {
		t.Fatalf("Expected error to be returned")
	}
}

func TestPrepareCommandsNoCommands(t *testing.T) {
	cmds := []PlatformCommand{}

	env := map[string]string{}
	if _, _, err := PrepareCommands(cmds, true, []string{}, env); err == nil {
		t.Fatalf("Expected error to be returned")
	}
}

func TestPrepareCommandsExpand(t *testing.T) {
	t.Setenv("TESTX", "testx")
	cmdMain := "sh"
	cmdArgs := []string{"-c", "echo \"${TESTX}${TESTY}\""}
	cmds := []PlatformCommand{
		{OperatingSystem: "", Architecture: "", Command: cmdMain, Args: cmdArgs},
	}

	expectedArgs := []string{"-c", "echo \"testxtesty\""}

	env := parseEnv(os.Environ())
	env["TESTY"] = "testy"

	cmd, args, err := PrepareCommands(cmds, true, []string{}, env)
	if err != nil {
		t.Fatal(err)
	}
	if cmd != cmdMain {
		t.Fatalf("Expected %q, got %q", cmdMain, cmd)
	}
	if !reflect.DeepEqual(args, expectedArgs) {
		t.Fatalf("Expected %v, got %v", expectedArgs, args)
	}
}

func TestPrepareCommandsNoExpand(t *testing.T) {
	t.Setenv("TEST", "test")

Review comment (refactor):
minor: Can we just move this logic to be included below?:

```
env["TESTX"] = "testx"
```

(as the usage of `parseEnv` is just causing this to round-trip through the process env vars)

Code after:

func TestPrepareCommandsNoMatch(t *testing.T) {
	cmds := []PlatformCommand{
		{OperatingSystem: "no-os", Architecture: "no-arch", Command: "sh", Args: []string{"-c", "echo \"test\""}},
		{OperatingSystem: runtime.GOOS, Architecture: "no-arch", Command: "sh", Args: []string{"-c", "echo \"test\""}},
		{OperatingSystem: "no-os", Architecture: runtime.GOARCH, Command: "sh", Args: []string{"-c", "echo \"test\""}},
	}

	env := map[string]string{}
	if _, _, err := PrepareCommands(cmds, true, []string{}, env); err == nil {
		t.Fatalf("Expected error to be returned")
	}
}

func TestPrepareCommandsNoCommands(t *testing.T) {
	cmds := []PlatformCommand{}

	env := map[string]string{}
	if _, _, err := PrepareCommands(cmds, true, []string{}, env); err == nil {
		t.Fatalf("Expected error to be returned")
	}
}

func TestPrepareCommandsExpand(t *testing.T) {
	cmdMain := "sh"
	cmdArgs := []string{"-c", "echo \"${TESTX}${TESTY}\""}
	cmds := []PlatformCommand{
		{OperatingSystem: "", Architecture: "", Command: cmdMain, Args: cmdArgs},
	}

	expectedArgs := []string{"-c", "echo \"testxtesty\""}

	env := map[string]string{
		"TESTX": "testx",
		"TESTY": "testy",
	}

	cmd, args, err := PrepareCommands(cmds, true, []string{}, env)
	if err != nil {
		t.Fatal(err)
	}
	if cmd != cmdMain {
		t.Fatalf("Expected %q, got %q", cmdMain, cmd)
	}
	if !reflect.DeepEqual(args, expectedArgs) {
		t.Fatalf("Expected %v, got %v", expectedArgs, args)
	}
}

func TestPrepareCommandsNoExpand(t *testing.T) {
	cmdMain := "sh"