6
6
)
7
7
8
8
type execArgs struct {
9
- fork bool
9
+ gomplate bool
10
10
argPrefixes []string
11
11
}
12
12
@@ -16,98 +16,89 @@ func TestRun(t *testing.T) {
16
16
args []string
17
17
execReturns error
18
18
whichReturns string
19
- wantExecArgs [] execArgs
19
+ wantExecArgs execArgs
20
20
wantErr error
21
21
}{
22
22
{
23
23
name : "executable not dex" ,
24
24
args : []string {"tuna" , "fish" },
25
- wantExecArgs : [] execArgs {{ fork : false , argPrefixes : []string {"tuna" , "fish" } }},
25
+ wantExecArgs : execArgs {gomplate : false , argPrefixes : []string {"tuna" , "fish" }},
26
26
},
27
27
{
28
28
name : "executable is full path to dex" ,
29
29
args : []string {"/usr/local/bin/dex" , "marshmallow" , "zelda" },
30
30
whichReturns : "/usr/local/bin/dex" ,
31
- wantExecArgs : [] execArgs {{ fork : false , argPrefixes : []string {"/usr/local/bin/dex" , "marshmallow" , "zelda" } }},
31
+ wantExecArgs : execArgs {gomplate : false , argPrefixes : []string {"/usr/local/bin/dex" , "marshmallow" , "zelda" }},
32
32
},
33
33
{
34
34
name : "command is not serve" ,
35
35
args : []string {"dex" , "marshmallow" , "zelda" },
36
- wantExecArgs : [] execArgs {{ fork : false , argPrefixes : []string {"dex" , "marshmallow" , "zelda" } }},
36
+ wantExecArgs : execArgs {gomplate : false , argPrefixes : []string {"dex" , "marshmallow" , "zelda" }},
37
37
},
38
38
{
39
39
name : "no templates" ,
40
40
args : []string {"dex" , "serve" , "config.yaml.not-a-template" },
41
- wantExecArgs : [] execArgs {{ fork : false , argPrefixes : []string {"dex" , "serve" , "config.yaml.not-a-template" } }},
41
+ wantExecArgs : execArgs {gomplate : false , argPrefixes : []string {"dex" , "serve" , "config.yaml.not-a-template" }},
42
42
},
43
43
{
44
44
name : "no templates" ,
45
45
args : []string {"dex" , "serve" , "config.yaml.not-a-template" },
46
- wantExecArgs : [] execArgs {{ fork : false , argPrefixes : []string {"dex" , "serve" , "config.yaml.not-a-template" } }},
46
+ wantExecArgs : execArgs {gomplate : false , argPrefixes : []string {"dex" , "serve" , "config.yaml.not-a-template" }},
47
47
},
48
48
{
49
- name : ".tpl template" ,
50
- args : []string {"dex" , "serve" , "config.tpl" },
51
- wantExecArgs : []execArgs {
52
- {fork : true , argPrefixes : []string {"gomplate" , "-f" , "config.tpl" , "-o" , "/tmp/dex.config.yaml-" }},
53
- {fork : false , argPrefixes : []string {"dex" , "serve" , "/tmp/dex.config.yaml-" }},
54
- },
49
+ name : ".tpl template" ,
50
+ args : []string {"dex" , "serve" , "config.tpl" },
51
+ wantExecArgs : execArgs {gomplate : true , argPrefixes : []string {"dex" , "serve" , "/tmp/dex.config.yaml-" }},
55
52
},
56
53
{
57
- name : ".tmpl template" ,
58
- args : []string {"dex" , "serve" , "config.tmpl" },
59
- wantExecArgs : []execArgs {
60
- {fork : true , argPrefixes : []string {"gomplate" , "-f" , "config.tmpl" , "-o" , "/tmp/dex.config.yaml-" }},
61
- {fork : false , argPrefixes : []string {"dex" , "serve" , "/tmp/dex.config.yaml-" }},
62
- },
54
+ name : ".tmpl template" ,
55
+ args : []string {"dex" , "serve" , "config.tmpl" },
56
+ wantExecArgs : execArgs {gomplate : true , argPrefixes : []string {"dex" , "serve" , "/tmp/dex.config.yaml-" }},
63
57
},
64
58
{
65
- name : ".yaml template" ,
66
- args : []string {"dex" , "serve" , "some/path/config.yaml" },
67
- wantExecArgs : []execArgs {
68
- {fork : true , argPrefixes : []string {"gomplate" , "-f" , "some/path/config.yaml" , "-o" , "/tmp/dex.config.yaml-" }},
69
- {fork : false , argPrefixes : []string {"dex" , "serve" , "/tmp/dex.config.yaml-" }},
70
- },
59
+ name : ".yaml template" ,
60
+ args : []string {"dex" , "serve" , "some/path/config.yaml" },
61
+ wantExecArgs : execArgs {gomplate : true , argPrefixes : []string {"dex" , "serve" , "/tmp/dex.config.yaml-" }},
71
62
},
72
63
}
73
64
for _ , test := range tests {
74
65
t .Run (test .name , func (t * testing.T ) {
75
- var gotExecForks []bool
76
- var gotExecArgs [][] string
77
- fakeExec := func ( fork bool , args ... string ) error {
78
- gotExecForks = append ( gotExecForks , fork )
79
- gotExecArgs = append (gotExecArgs , args )
66
+ var gotExecArgs []string
67
+ var runsGomplate bool
68
+
69
+ fakeExec := func ( args ... string ) error {
70
+ gotExecArgs = append (args , gotExecArgs ... )
80
71
return test .execReturns
81
72
}
82
73
83
74
fakeWhich := func (_ string ) string { return test .whichReturns }
84
75
85
- gotErr := run (test .args , fakeExec , fakeWhich )
76
+ fakeGomplate := func (file string ) (string , error ) {
77
+ runsGomplate = true
78
+ return "/tmp/dex.config.yaml-" , nil
79
+ }
80
+
81
+ gotErr := run (test .args , fakeExec , fakeWhich , fakeGomplate )
86
82
if (test .wantErr == nil ) != (gotErr == nil ) {
87
83
t .Errorf ("wanted error %s, got %s" , test .wantErr , gotErr )
88
84
}
89
- if ! execArgsMatch (test .wantExecArgs , gotExecForks , gotExecArgs ) {
90
- t .Errorf ("wanted exec args %+v, got %+v %+v" , test .wantExecArgs , gotExecForks , gotExecArgs )
85
+
86
+ if ! execArgsMatch (test .wantExecArgs , runsGomplate , gotExecArgs ) {
87
+ t .Errorf ("wanted exec args %+v (running gomplate: %+v), got %+v (running gomplate: %+v)" ,
88
+ test .wantExecArgs .argPrefixes , test .wantExecArgs .gomplate , gotExecArgs , runsGomplate )
91
89
}
92
90
})
93
91
}
94
92
}
95
93
96
- func execArgsMatch (wantExecArgs [] execArgs , gotForks [] bool , gotExecArgs [] []string ) bool {
97
- if len ( wantExecArgs ) != len ( gotForks ) {
94
+ func execArgsMatch (wantExecArgs execArgs , gomplate bool , gotExecArgs []string ) bool {
95
+ if wantExecArgs . gomplate != gomplate {
98
96
return false
99
97
}
100
-
101
- for i := range wantExecArgs {
102
- if wantExecArgs [i ].fork != gotForks [i ] {
98
+ for i := range wantExecArgs .argPrefixes {
99
+ if ! strings .HasPrefix (gotExecArgs [i ], wantExecArgs .argPrefixes [i ]) {
103
100
return false
104
101
}
105
- for j := range wantExecArgs [i ].argPrefixes {
106
- if ! strings .HasPrefix (gotExecArgs [i ][j ], wantExecArgs [i ].argPrefixes [j ]) {
107
- return false
108
- }
109
- }
110
102
}
111
-
112
103
return true
113
104
}
0 commit comments