Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 18b5f50

Browse files
committed
Cleanup when commands are executed in sh or not
1 parent cafdd6f commit 18b5f50

File tree

3 files changed

+33
-42
lines changed

3 files changed

+33
-42
lines changed

ci/integration/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func init() {
4646
// write session tokens to the given container runner
4747
func headlessLogin(ctx context.Context, t *testing.T, runner *tcli.ContainerRunner) {
4848
creds := login(ctx, t)
49-
cmd := exec.CommandContext(ctx, "mkdir -p ~/.config/coder && cat > ~/.config/coder/session")
49+
cmd := exec.CommandContext(ctx, "sh", "-c", "mkdir -p ~/.config/coder && cat > ~/.config/coder/session")
5050

5151
// !IMPORTANT: be careful that this does not appear in logs
5252
cmd.Stdin = strings.NewReader(creds.token)

ci/tcli/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// Package tcli provides a framework for CLI integration testing.
2-
// Execute commands on the raw host of inside docker container.
2+
// Execute commands on the raw host or inside a docker container.
33
// Define custom Assertion types to extend test functionality.
44
package tcli

ci/tcli/tcli.go

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -102,31 +102,48 @@ func (r *ContainerRunner) Close() error {
102102
return nil
103103
}
104104

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+
105132
// HostRunner executes command tests on the host, outside of a container
106133
type HostRunner struct{}
107134

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".
109137
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)
122139

123140
return &Assertable{
124141
cmd: cmd,
125142
tname: command,
126143
}
127144
}
128145

129-
// RunCmd executes the given command on the host
146+
// RunCmd executes the given *exec.Cmd on the host
130147
func (r *HostRunner) RunCmd(cmd *exec.Cmd) *Assertable {
131148
return &Assertable{
132149
cmd: cmd,
@@ -145,32 +162,6 @@ type Assertable struct {
145162
tname string
146163
}
147164

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-
174165
// Assert runs the Assertable and
175166
func (a Assertable) Assert(t *testing.T, option ...Assertion) {
176167
slog.Helper()

0 commit comments

Comments
 (0)