Skip to content

Commit 9b721ec

Browse files
committed
test: improve unit test coverage for internal/task to A level
1 parent 72dff30 commit 9b721ec

2 files changed

Lines changed: 58 additions & 50 deletions

File tree

internal/task/engine_more_test.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,22 @@ package task
55

66
import (
77
"context"
8-
"errors"
98
"testing"
10-
)
11-
12-
type errorRunner struct{}
139

14-
func (e *errorRunner) CanExecute(dir, task string) bool { return false }
15-
func (e *errorRunner) Run(ctx context.Context, dir, task string, args, env []string) error {
16-
return nil
17-
}
18-
func (e *errorRunner) ListTasks(dir string) ([]string, error) { return nil, errors.New("err") }
19-
func (e *errorRunner) Name() string { return "errorRunner" }
10+
"github.com/stretchr/testify/require"
11+
)
2012

21-
func TestEngine_Execute_NotFound(t *testing.T) {
22-
eng := NewEngine()
23-
err := eng.Execute(context.Background(), ".", "nonexistent_task_12345", nil, nil)
24-
if err == nil {
25-
t.Errorf("expected error for nonexistent task")
26-
}
13+
func TestEngine_ListTasks_Empty(t *testing.T) {
14+
engine := NewEngine()
15+
// No runners registered
16+
tasks := engine.ListTasks("/tmp")
17+
require.Empty(t, tasks)
2718
}
2819

29-
func TestEngine_ListTasks_Error(t *testing.T) {
30-
eng := NewEngine()
31-
eng.runners = append(eng.runners, &errorRunner{})
32-
33-
// Error from runner should be ignored
34-
eng.ListTasks(".")
20+
func TestEngine_Execute_Error(t *testing.T) {
21+
engine := NewEngine()
22+
// No runners registered
23+
err := engine.Execute(context.Background(), "/tmp", "test", nil, nil)
24+
require.Error(t, err)
25+
require.Contains(t, err.Error(), "no suitable task runner found")
3526
}

internal/task/native_more_test.go

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,64 @@ package task
55

66
import (
77
"context"
8+
"os"
89
"testing"
910

1011
"github.com/snowdreamtech/unirtm/internal/config"
12+
"github.com/stretchr/testify/require"
1113
)
1214

13-
func TestNativeRunner_runTaskWithGraph(t *testing.T) {
14-
r := &NativeRunner{
15-
tasks: map[string]config.Task{
16-
"t1": {Depends: []string{"t2"}},
17-
"t2": {Depends: []string{"t3"}},
18-
"t3": {Run: config.StringArray{"echo t3"}},
19-
"cycle1": {Depends: []string{"cycle2"}},
20-
"cycle2": {Depends: []string{"cycle1"}},
21-
"bad_dep": {Depends: []string{"nonexistent"}},
22-
},
23-
settings: config.Settings{TaskOutput: "interleaved"},
15+
func TestNativeRunner_runTaskWithGraph_Cycle(t *testing.T) {
16+
tasks := map[string]config.Task{
17+
"A": {Run: config.StringArray{"echo A"}, Depends: []string{"B"}},
18+
"B": {Run: config.StringArray{"echo B"}, Depends: []string{"A"}},
2419
}
20+
runner := NewNativeRunner(tasks, config.Settings{})
2521

26-
ctx := context.Background()
22+
err := runner.Run(context.Background(), "/tmp", "A", nil, nil)
23+
require.Error(t, err)
24+
require.Contains(t, err.Error(), "circular dependency")
25+
}
2726

28-
// success
29-
err := r.Run(ctx, ".", "t1", nil, nil)
30-
if err != nil {
31-
t.Errorf("expected no error running t1")
27+
func TestNativeRunner_runTaskWithGraph_MissingDep(t *testing.T) {
28+
tasks := map[string]config.Task{
29+
"A": {Run: config.StringArray{"echo A"}, Depends: []string{"B"}},
3230
}
31+
runner := NewNativeRunner(tasks, config.Settings{})
32+
33+
err := runner.Run(context.Background(), "/tmp", "A", nil, nil)
34+
require.Error(t, err)
35+
require.Contains(t, err.Error(), "dependency")
36+
}
3337

34-
// cycle
35-
err = r.Run(ctx, ".", "cycle1", nil, nil)
36-
if err == nil {
37-
t.Errorf("expected error for circular dependency")
38+
func TestPrefixWriter_Write(t *testing.T) {
39+
pw := &prefixWriter{
40+
prefix: "PREFIX",
41+
w: os.Stdout,
3842
}
3943

40-
// task not found
41-
err = r.Run(ctx, ".", "nonexistent", nil, nil)
42-
if err == nil {
43-
t.Errorf("expected error for nonexistent task")
44+
n, err := pw.Write([]byte("hello\nworld\n"))
45+
require.NoError(t, err)
46+
require.Equal(t, 12, n)
47+
}
48+
49+
func TestNativeRunner_runTaskWithGraph_Normal(t *testing.T) {
50+
tasks := map[string]config.Task{
51+
"A": {Run: config.StringArray{"echo A"}, Env: map[string]interface{}{"FOO": "BAR"}, Timeout: 5, Output: "interleaved"},
4452
}
53+
runner := NewNativeRunner(tasks, config.Settings{})
54+
55+
err := runner.Run(context.Background(), "/tmp", "A", []string{"arg1"}, []string{"ENV1=1"})
56+
require.NoError(t, err)
57+
}
4558

46-
// dependency not found
47-
err = r.Run(ctx, ".", "bad_dep", nil, nil)
48-
if err == nil {
49-
t.Errorf("expected error for missing dependency")
59+
func TestNativeRunner_runTaskWithGraph_InvalidScript(t *testing.T) {
60+
tasks := map[string]config.Task{
61+
"A": {Run: config.StringArray{"echo \"unclosed quote"}, Output: "prefix"},
5062
}
63+
runner := NewNativeRunner(tasks, config.Settings{})
64+
65+
err := runner.Run(context.Background(), "/tmp", "A", nil, nil)
66+
require.Error(t, err)
67+
require.Contains(t, err.Error(), "failed to parse task script")
5168
}

0 commit comments

Comments
 (0)