@@ -102,31 +102,48 @@ func (r *ContainerRunner) Close() error {
102
102
return nil
103
103
}
104
104
105
+ // Run executes the given command in the runtime container with reasonable defaults.
106
+ // "command" is executed in a shell as an argument to "sh -c".
107
+ func (r * ContainerRunner ) Run (ctx context.Context , command string ) * Assertable {
108
+ cmd := exec .CommandContext (ctx ,
109
+ "docker" , "exec" , "-i" , r .name ,
110
+ "sh" , "-c" , command ,
111
+ )
112
+
113
+ return & Assertable {
114
+ cmd : cmd ,
115
+ tname : command ,
116
+ }
117
+ }
118
+
119
+ // RunCmd lifts the given *exec.Cmd into the runtime container
120
+ func (r * ContainerRunner ) RunCmd (cmd * exec.Cmd ) * Assertable {
121
+ path , _ := exec .LookPath ("docker" )
122
+ cmd .Path = path
123
+ command := strings .Join (cmd .Args , " " )
124
+ cmd .Args = append ([]string {"docker" , "exec" , "-i" , r .name }, cmd .Args ... )
125
+
126
+ return & Assertable {
127
+ cmd : cmd ,
128
+ tname : command ,
129
+ }
130
+ }
131
+
105
132
// HostRunner executes command tests on the host, outside of a container
106
133
type HostRunner struct {}
107
134
108
- // Run executes the given command on the host
135
+ // Run executes the given command on the host.
136
+ // "command" is executed in a shell as an argument to "sh -c".
109
137
func (r * HostRunner ) Run (ctx context.Context , command string ) * Assertable {
110
- var (
111
- args []string
112
- path string
113
- parts = strings .Split (command , " " )
114
- )
115
- if len (parts ) > 0 {
116
- path = parts [0 ]
117
- }
118
- if len (parts ) > 1 {
119
- args = parts [1 :]
120
- }
121
- cmd := exec .CommandContext (ctx , path , args ... )
138
+ cmd := exec .CommandContext (ctx , "sh" , "-c" , command )
122
139
123
140
return & Assertable {
124
141
cmd : cmd ,
125
142
tname : command ,
126
143
}
127
144
}
128
145
129
- // RunCmd executes the given command on the host
146
+ // RunCmd executes the given *exec.Cmd on the host
130
147
func (r * HostRunner ) RunCmd (cmd * exec.Cmd ) * Assertable {
131
148
return & Assertable {
132
149
cmd : cmd ,
@@ -145,32 +162,6 @@ type Assertable struct {
145
162
tname string
146
163
}
147
164
148
- // Run executes the given command in the runtime container with reasonable defaults
149
- func (r * ContainerRunner ) Run (ctx context.Context , command string ) * Assertable {
150
- cmd := exec .CommandContext (ctx ,
151
- "docker" , "exec" , "-i" , r .name ,
152
- "sh" , "-c" , command ,
153
- )
154
-
155
- return & Assertable {
156
- cmd : cmd ,
157
- tname : command ,
158
- }
159
- }
160
-
161
- // RunCmd lifts the given *exec.Cmd into the runtime container
162
- func (r * ContainerRunner ) RunCmd (cmd * exec.Cmd ) * Assertable {
163
- path , _ := exec .LookPath ("docker" )
164
- cmd .Path = path
165
- command := strings .Join (cmd .Args , " " )
166
- cmd .Args = []string {"docker" , "exec" , "-i" , r .name , "sh" , "-c" , command }
167
-
168
- return & Assertable {
169
- cmd : cmd ,
170
- tname : command ,
171
- }
172
- }
173
-
174
165
// Assert runs the Assertable and
175
166
func (a Assertable ) Assert (t * testing.T , option ... Assertion ) {
176
167
slog .Helper ()
0 commit comments