|
| 1 | +// Package compat is the cross-CLI conformance test library for the |
| 2 | +// quantcli export-CLI contract. Each *-export-cli imports the subpackages |
| 3 | +// (e.g. compat/dates) and runs them against its own built binary in CI. |
| 4 | +// |
| 5 | +// The library is deliberately black-box: it shells out to the binary under |
| 6 | +// test and asserts on stdout, stderr, and exit code. It never imports a |
| 7 | +// CLI's internal packages. Adding a new contract test means adding a new |
| 8 | +// subpackage here; every exporter then picks it up by adding a one-line |
| 9 | +// test entry point. |
| 10 | +// |
| 11 | +// See CONTRACT.md in the parent repository for the surface this library |
| 12 | +// pins down. |
| 13 | +package compat |
| 14 | + |
| 15 | +import ( |
| 16 | + "bytes" |
| 17 | + "context" |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "os/exec" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | +) |
| 24 | + |
| 25 | +// Runner invokes a single *-export-cli binary in a controlled environment. |
| 26 | +// A zero-value Runner is not usable; Binary must be set to an absolute path. |
| 27 | +type Runner struct { |
| 28 | + // Binary is the absolute path to the export-cli binary under test. |
| 29 | + Binary string |
| 30 | + |
| 31 | + // Env is the environment passed to the binary. If nil, an empty |
| 32 | + // environment is used. Tests should set this explicitly so behavior |
| 33 | + // does not depend on whatever happens to be in the CI runner's |
| 34 | + // environment (notably PATH, HOME, TZ, and any *_TOKEN credentials). |
| 35 | + Env []string |
| 36 | + |
| 37 | + // Timeout is the per-invocation timeout. Zero means 10 seconds. |
| 38 | + Timeout time.Duration |
| 39 | +} |
| 40 | + |
| 41 | +// Result captures everything observable about one CLI invocation. All |
| 42 | +// compat-test assertions operate on these three fields. |
| 43 | +type Result struct { |
| 44 | + Stdout []byte |
| 45 | + Stderr []byte |
| 46 | + ExitCode int |
| 47 | +} |
| 48 | + |
| 49 | +// StdoutString returns Stdout as a string. |
| 50 | +func (r Result) StdoutString() string { return string(r.Stdout) } |
| 51 | + |
| 52 | +// StderrString returns Stderr as a string. |
| 53 | +func (r Result) StderrString() string { return string(r.Stderr) } |
| 54 | + |
| 55 | +// Run invokes the binary with the given args and returns its observable |
| 56 | +// output. A non-zero exit code is NOT returned as an error — compat tests |
| 57 | +// frequently assert on non-zero exits, so the caller decides what counts |
| 58 | +// as a failure. ctx cancellation, process-start failure, and timeouts are |
| 59 | +// returned as errors. |
| 60 | +func (r Runner) Run(ctx context.Context, args ...string) (Result, error) { |
| 61 | + if r.Binary == "" { |
| 62 | + return Result{}, errors.New("compat: Runner.Binary is empty") |
| 63 | + } |
| 64 | + timeout := r.Timeout |
| 65 | + if timeout == 0 { |
| 66 | + timeout = 10 * time.Second |
| 67 | + } |
| 68 | + runCtx, cancel := context.WithTimeout(ctx, timeout) |
| 69 | + defer cancel() |
| 70 | + |
| 71 | + cmd := exec.CommandContext(runCtx, r.Binary, args...) |
| 72 | + // Default to an empty env so tests are hermetic. Callers opt into |
| 73 | + // passing TZ, HOME, etc. via Runner.Env. |
| 74 | + if r.Env != nil { |
| 75 | + cmd.Env = append([]string(nil), r.Env...) |
| 76 | + } else { |
| 77 | + cmd.Env = []string{} |
| 78 | + } |
| 79 | + |
| 80 | + var stdout, stderr bytes.Buffer |
| 81 | + cmd.Stdout = &stdout |
| 82 | + cmd.Stderr = &stderr |
| 83 | + |
| 84 | + err := cmd.Run() |
| 85 | + res := Result{Stdout: stdout.Bytes(), Stderr: stderr.Bytes()} |
| 86 | + if err == nil { |
| 87 | + res.ExitCode = 0 |
| 88 | + return res, nil |
| 89 | + } |
| 90 | + var exitErr *exec.ExitError |
| 91 | + if errors.As(err, &exitErr) { |
| 92 | + res.ExitCode = exitErr.ExitCode() |
| 93 | + return res, nil |
| 94 | + } |
| 95 | + if errors.Is(runCtx.Err(), context.DeadlineExceeded) { |
| 96 | + return res, fmt.Errorf("compat: %s timed out after %s", r.Binary, timeout) |
| 97 | + } |
| 98 | + return res, fmt.Errorf("compat: failed to run %s: %w", r.Binary, err) |
| 99 | +} |
| 100 | + |
| 101 | +// MustRun is the testing-helper equivalent of Run: it fails the test on |
| 102 | +// any non-exit-code error (timeout, missing binary, etc.) and returns the |
| 103 | +// Result on success. |
| 104 | +func (r Runner) MustRun(t *testing.T, args ...string) Result { |
| 105 | + t.Helper() |
| 106 | + res, err := r.Run(context.Background(), args...) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("compat: %v", err) |
| 109 | + } |
| 110 | + return res |
| 111 | +} |
| 112 | + |
| 113 | +// WithEnv returns a copy of r with environment variable KEY=VALUE pairs |
| 114 | +// appended. Useful for setting TZ on a per-test basis without mutating |
| 115 | +// the receiver. |
| 116 | +func (r Runner) WithEnv(kv ...string) Runner { |
| 117 | + out := r |
| 118 | + out.Env = append(append([]string(nil), r.Env...), kv...) |
| 119 | + return out |
| 120 | +} |
0 commit comments