From b501a9eb461c41cd2d1b214e0464bec6a7486c63 Mon Sep 17 00:00:00 2001 From: adity1raut Date: Wed, 7 Jan 2026 17:47:23 +0530 Subject: [PATCH] test:Add unit tests for command execution functions in executables_test.go Signed-off-by: adity1raut --- util/executables_test.go | 72 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 util/executables_test.go diff --git a/util/executables_test.go b/util/executables_test.go new file mode 100644 index 0000000..c4fdf6c --- /dev/null +++ b/util/executables_test.go @@ -0,0 +1,72 @@ +package util + +import ( + "bytes" + "os" + "path/filepath" + "runtime" + "testing" +) + + +func createFakeCmd(t *testing.T, script string) string { + dir := t.TempDir() + name := "fakecmd" + + if runtime.GOOS == "windows" { + name += ".bat" + script = "@echo off\r\n" + script + } else { + script = "#!/bin/sh\n" + script + } + + path := filepath.Join(dir, name) + err := os.WriteFile(path, []byte(script), 0755) + if err != nil { + t.Fatal(err) + } + return path +} + + +func TestRunCommandCustomIOSuccess(t *testing.T) { + fake := createFakeCmd(t, "echo hello") + ExecutablePaths = map[string]string{"test": fake} + + var out bytes.Buffer + var errOut bytes.Buffer + + err := RunCommandCustomIO("test", &out, &errOut, true) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } +} + +func TestRunCommandFailure(t *testing.T) { + fake := createFakeCmd(t, "exit 1") + ExecutablePaths = map[string]string{"test": fake} + + err := RunCommand("test") + if err == nil { + t.Fatalf("expected error, got nil") + } +} + +func TestRunCommandWithoutPrint(t *testing.T) { + fake := createFakeCmd(t, "echo silent") + ExecutablePaths = map[string]string{"test": fake} + + err := RunCommandWithoutPrint("test") + if err != nil { + t.Fatalf("expected no error, got %v", err) + } +} + +func TestMissingExecutablePath(t *testing.T) { + ExecutablePaths = map[string]string{} + + err := RunCommand("missing") + if err == nil { + t.Fatalf("expected error for missing executable path") + } +}