Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions util/executables_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}