1
1
package parser
2
2
3
3
import (
4
+ "context"
5
+ "fmt"
4
6
"reflect"
5
7
"testing"
6
8
9
+ "github.com/nxtcoder17/go.pkgs/log"
10
+ fn "github.com/nxtcoder17/runfile/functions"
7
11
"github.com/nxtcoder17/runfile/types"
8
12
)
9
13
14
+ func testParseCommandJsonEqual (t * testing.T , got , want * types.ParsedCommandJson ) {
15
+ if got == nil && want != nil || got != nil && want == nil {
16
+ t .Errorf ("parseCommand(),\n [.command] \n \t got = %v\n \t want = %v" , got , want )
17
+ return
18
+ }
19
+
20
+ // t.Log("first", first, "err", err, "secondErr", secondErr, "condition", secondErr != (err != nil))
21
+
22
+ if ! reflect .DeepEqual (got .Command , want .Command ) {
23
+ t .Errorf ("parseCommand(),\n [.command] \n \t got = %v\n \t want = %v" , fn .DefaultIfNil (got .Command , "" ), fn .DefaultIfNil (want .Command , "" ))
24
+ return
25
+ }
26
+
27
+ if fmt .Sprint (got .Env ) != fmt .Sprint (want .Env ) {
28
+ t .Errorf ("parseCommand(),\n [.env] \n \t got = %+v\n \t want = %+v" , got .Env , want .Env )
29
+ return
30
+ }
31
+ }
32
+
10
33
func Test_parseCommand (t * testing.T ) {
11
34
type args struct {
12
35
prf * types.ParsedRunfile
36
+ taskEnv map [string ]string
13
37
command any
14
38
}
15
39
tests := []struct {
@@ -18,18 +42,84 @@ func Test_parseCommand(t *testing.T) {
18
42
want * types.ParsedCommandJson
19
43
wantErr bool
20
44
}{
21
- // TODO: Add test cases.
45
+ {
46
+ name : "1. must pass with only command" ,
47
+ args : args {
48
+ prf : & types.ParsedRunfile {},
49
+ taskEnv : map [string ]string {},
50
+ command : "echo hi hello" ,
51
+ },
52
+ want : & types.ParsedCommandJson {
53
+ Command : fn .New ("echo hi hello" ),
54
+ Run : nil ,
55
+ Env : map [string ]string {},
56
+ If : nil ,
57
+ },
58
+ wantErr : false ,
59
+ },
60
+ {
61
+ name : "2. must fail with only run command with run target not found" ,
62
+ args : args {
63
+ prf : & types.ParsedRunfile {},
64
+ taskEnv : map [string ]string {},
65
+ command : map [string ]any {
66
+ "run" : "build" ,
67
+ },
68
+ },
69
+ want : nil ,
70
+ wantErr : true ,
71
+ },
72
+ {
73
+ name : "3. must pass with run command, and target exists in runfile tasks" ,
74
+ args : args {
75
+ prf : & types.ParsedRunfile {
76
+ Tasks : map [string ]types.Task {
77
+ "build" : {
78
+ Commands : []any {
79
+ "echo from build" ,
80
+ },
81
+ },
82
+ },
83
+ },
84
+ taskEnv : map [string ]string {},
85
+ command : map [string ]any {
86
+ "run" : "build" ,
87
+ "env" : map [string ]string {
88
+ "k1" : "v1" ,
89
+ },
90
+ },
91
+ },
92
+ want : & types.ParsedCommandJson {
93
+ Command : nil ,
94
+ Run : fn .New ("build" ),
95
+ Env : map [string ]string {
96
+ "k1" : "v1" ,
97
+ },
98
+ If : nil ,
99
+ },
100
+ wantErr : false ,
101
+ },
22
102
}
23
- for _ , tt := range tests {
103
+
104
+ for i := range tests {
105
+ tt := tests [i ]
24
106
t .Run (tt .name , func (t * testing.T ) {
25
- got , err := parseCommand (tt .args .prf , tt .args .command )
26
- if (err != nil ) != tt .wantErr {
107
+ ctx := types.Context {
108
+ Context : context .TODO (),
109
+ Logger : log .New (),
110
+ }
111
+
112
+ got , err := parseCommand (ctx , tt .args .prf , tt .args .taskEnv , tt .args .command )
113
+ if tt .wantErr != (err != nil ) {
27
114
t .Errorf ("parseCommand() error = %v, wantErr %v" , err , tt .wantErr )
28
115
return
29
116
}
30
- if ! reflect .DeepEqual (got , tt .want ) {
31
- t .Errorf ("parseCommand() = %v, want %v" , got , tt .want )
117
+
118
+ if tt .wantErr {
119
+ return
32
120
}
121
+
122
+ testParseCommandJsonEqual (t , got , tt .want )
33
123
})
34
124
}
35
125
}
0 commit comments