67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
package compose
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRunnerPassesEachArgumentWithoutShellSplitting(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
runner := NewRunner(writeExecutable(t, "#!/bin/sh\nprintf '<%s>\\n' \"$@\"\ncat\n"))
|
|
result, err := runner.Run(context.Background(), []string{"compose", "--project-directory", "/tmp/a project with spaces", "config"}, strings.NewReader("stdin value\n"))
|
|
if err != nil {
|
|
t.Fatalf("Run() error = %v", err)
|
|
}
|
|
want := "<compose>\n<--project-directory>\n</tmp/a project with spaces>\n<config>\nstdin value\n"
|
|
if result.Stdout != want {
|
|
t.Errorf("stdout = %q, want %q", result.Stdout, want)
|
|
}
|
|
if result.ExitCode != 0 {
|
|
t.Errorf("ExitCode = %d, want 0", result.ExitCode)
|
|
}
|
|
}
|
|
|
|
func TestRunnerReturnsTheChildExitCode(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
runner := NewRunner(writeExecutable(t, "#!/bin/sh\necho unavailable >&2\nexit 42\n"))
|
|
result, err := runner.Run(context.Background(), []string{"compose", "ps"}, nil)
|
|
if err == nil {
|
|
t.Fatal("Run() error = nil, want child exit error")
|
|
}
|
|
if result.ExitCode != 42 {
|
|
t.Errorf("ExitCode = %d, want 42", result.ExitCode)
|
|
}
|
|
if result.Stderr != "unavailable\n" {
|
|
t.Errorf("stderr = %q, want unavailable output", result.Stderr)
|
|
}
|
|
}
|
|
|
|
func TestRunnerReportsMissingDocker(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
runner := NewRunner(filepath.Join(t.TempDir(), "docker-does-not-exist"))
|
|
result, err := runner.Run(context.Background(), []string{"compose", "version"}, nil)
|
|
if !errors.Is(err, exec.ErrNotFound) {
|
|
t.Fatalf("Run() error = %v, want exec.ErrNotFound", err)
|
|
}
|
|
if result.ExitCode != 127 {
|
|
t.Errorf("ExitCode = %d, want 127", result.ExitCode)
|
|
}
|
|
}
|
|
|
|
func writeExecutable(t *testing.T, contents string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "fake-docker")
|
|
if err := os.WriteFile(path, []byte(contents), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|