summaryrefslogtreecommitdiff
path: root/internal/exec/exec_test.go
blob: 4dc650541ffddc33712761bd598ea529af35fcbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package exec

import (
	"testing"
)

func TestRun(t *testing.T) {
	tests := []struct {
		name           string
		cmd            string
		args           []string
		wantStdout     string
		wantStderr     string
		wantExitCode   int
		wantErr        bool
	}{
		{
			name:         "success",
			cmd:          "echo",
			args:         []string{"hello world"},
			wantStdout:   "hello world\n",
			wantStderr:   "",
			wantExitCode: 0,
			wantErr:      false,
		},
		{
			name:         "fail-exit-code",
			cmd:          "ls",
			args:         []string{"/non-existent-directory-12345"},
			wantStdout:   "",
			wantStderr:   "", // ls stderr varies by OS, but should not be empty usually.
			wantExitCode: 2,  // Typical for ls non-existent
			wantErr:      false,
		},
		{
			name:         "fail-binary-not-found",
			cmd:          "non-existent-command-12345",
			args:         []string{},
			wantStdout:   "",
			wantStderr:   "",
			wantExitCode: -1,
			wantErr:      true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			stdout, stderr, exitCode, err := Run(tt.cmd, tt.args...)

			if (err != nil) != tt.wantErr {
				t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
				return
			}

			if exitCode != tt.wantExitCode {
				t.Errorf("Run() exitCode = %v, want %v", exitCode, tt.wantExitCode)
			}

			if tt.name == "success" && stdout != tt.wantStdout {
				t.Errorf("Run() stdout = %q, want %q", stdout, tt.wantStdout)
			}
			
			// For ls error, we just check that stderr is not empty since exact text varies
			if tt.name == "fail-exit-code" && stderr == "" {
				t.Errorf("Run() stderr = %q, want non-empty", stderr)
			}
		})
	}
}