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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package askcli
import (
"bytes"
"context"
"errors"
"io"
"os/exec"
"reflect"
"strings"
"testing"
)
func TestExecutorTaskArgs(t *testing.T) {
exec_ := NewExecutor("ask")
args, err := exec_.taskArgs("/tmp/work/hexai", []string{"list", "limit:1"})
if err != nil {
t.Fatalf("taskArgs returned error: %v", err)
}
want := []string{"project:hexai", "+agent", "list", "limit:1"}
if !reflect.DeepEqual(args, want) {
t.Fatalf("task args = %v, want %v", args, want)
}
}
func TestExecutorRun_InjectsProjectFilterAndAgentTag(t *testing.T) {
var gotName string
var gotArgs []string
exec_ := Executor{
commandName: "ask",
findBinary: func() (string, error) { return "/usr/bin/task", nil },
detectRepoRoot: func(context.Context) (string, error) { return "/tmp/work/hexai", nil },
runCommand: func(_ context.Context, name string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
gotName = name
gotArgs = append([]string(nil), args...)
return nil
},
}
exitCode, err := exec_.Run(context.Background(), []string{"list", "limit:1"}, strings.NewReader("in"), &bytes.Buffer{}, &bytes.Buffer{})
if err != nil {
t.Fatalf("Run returned error: %v", err)
}
if exitCode != 0 {
t.Fatalf("exitCode = %d, want 0", exitCode)
}
if gotName != "/usr/bin/task" {
t.Fatalf("task binary = %q, want /usr/bin/task", gotName)
}
wantArgs := []string{"project:hexai", "+agent", "list", "limit:1"}
if !reflect.DeepEqual(gotArgs, wantArgs) {
t.Fatalf("task args = %v, want %v", gotArgs, wantArgs)
}
}
func TestExecutorRun_OutsideGitRepo_IsActionable(t *testing.T) {
exec_ := Executor{
commandName: "ask",
findBinary: func() (string, error) { return "/usr/bin/task", nil },
detectRepoRoot: func(context.Context) (string, error) { return "", errors.New("git failed") },
runCommand: func(context.Context, string, []string, io.Reader, io.Writer, io.Writer) error {
t.Fatal("runCommand should not be called when repo detection fails")
return nil
},
}
exitCode, err := exec_.Run(context.Background(), []string{"list"}, strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{})
if exitCode != 1 {
t.Fatalf("exitCode = %d, want 1", exitCode)
}
if err == nil || !strings.Contains(err.Error(), "must be run inside a git repository") {
t.Fatalf("expected actionable git-repo error, got %v", err)
}
}
func TestExecutorRun_PreservesTaskwarriorExitCode(t *testing.T) {
exec_ := Executor{
commandName: "ask",
findBinary: func() (string, error) { return "/usr/bin/task", nil },
detectRepoRoot: func(context.Context) (string, error) { return "/tmp/work/hexai", nil },
runCommand: func(context.Context, string, []string, io.Reader, io.Writer, io.Writer) error {
return exec.Command("sh", "-c", "exit 7").Run()
},
}
exitCode, err := exec_.Run(context.Background(), []string{"list"}, strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{})
if err != nil {
t.Fatalf("expected nil error for subprocess exit, got %v", err)
}
if exitCode != 7 {
t.Fatalf("exitCode = %d, want 7", exitCode)
}
}
func TestExecutorRun_PreservesStdoutAndStderr(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
exec_ := Executor{
commandName: "ask",
findBinary: func() (string, error) { return "/usr/bin/task", nil },
detectRepoRoot: func(context.Context) (string, error) { return "/tmp/work/hexai", nil },
runCommand: func(_ context.Context, name string, args []string, stdin io.Reader, out, errOut io.Writer) error {
_, _ = io.WriteString(out, "task stdout")
_, _ = io.WriteString(errOut, "task stderr")
return nil
},
}
exitCode, err := exec_.Run(context.Background(), []string{"list"}, strings.NewReader(""), &stdout, &stderr)
if err != nil {
t.Fatalf("Run returned error: %v", err)
}
if exitCode != 0 {
t.Fatalf("exitCode = %d, want 0", exitCode)
}
if stdout.String() != "task stdout" {
t.Fatalf("stdout = %q, want %q", stdout.String(), "task stdout")
}
if stderr.String() != "task stderr" {
t.Fatalf("stderr = %q, want %q", stderr.String(), "task stderr")
}
}
func TestExecutorRun_TaskLookupFailure_IsActionable(t *testing.T) {
exec_ := Executor{
commandName: "ask",
findBinary: func() (string, error) { return "", errors.New("not found") },
}
exitCode, err := exec_.Run(context.Background(), []string{"list"}, strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{})
if exitCode != 1 {
t.Fatalf("exitCode = %d, want 1", exitCode)
}
if err == nil || !strings.Contains(err.Error(), "task binary lookup failed") {
t.Fatalf("expected actionable task lookup error, got %v", err)
}
}
func TestExecutorRun_EmptyRepoName_IsActionable(t *testing.T) {
exec_ := Executor{
commandName: "ask",
findBinary: func() (string, error) { return "/usr/bin/task", nil },
detectRepoRoot: func(context.Context) (string, error) { return "/", nil },
}
exitCode, err := exec_.Run(context.Background(), []string{"list"}, strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{})
if exitCode != 1 {
t.Fatalf("exitCode = %d, want 1", exitCode)
}
if err == nil || !strings.Contains(err.Error(), "could not derive project name") {
t.Fatalf("expected actionable project-name error, got %v", err)
}
}
|