@@ -5,47 +5,64 @@ package task
55
66import (
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\n world\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