|
| 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 | + // Subcommands declares the subcommands under which the contract |
| 41 | + // surface lives — for CLIs (typically cobra-based) where flags like |
| 42 | + // --since and --until are attached to data-producing subcommands |
| 43 | + // rather than the root binary. Examples: crono's `biometrics`, |
| 44 | + // `exercises`, `nutrition`, `servings`, `notes` each accept their |
| 45 | + // own --since/--until. |
| 46 | + // |
| 47 | + // Empty means the surface is on the root binary; section bundles |
| 48 | + // invoke the binary directly. Non-empty means each section's |
| 49 | + // RunContract iterates the list and verifies the contract once per |
| 50 | + // subcommand via t.Run, so a regression in any single subcommand |
| 51 | + // surfaces as a named subtest failure rather than masking the rest. |
| 52 | + // |
| 53 | + // The Runner itself does not look at this field; section bundles |
| 54 | + // (e.g. compat/dates) read it and dispatch via WithSubcommand. |
| 55 | + Subcommands []string |
| 56 | + |
| 57 | + // subcommand, when non-empty, is prepended to args on every Run |
| 58 | + // call. Set via WithSubcommand; section bundles use it to dispatch |
| 59 | + // per-subcommand. Callers do not need to set it directly — set |
| 60 | + // Subcommands instead and let the bundle compose the dispatch. |
| 61 | + subcommand string |
| 62 | +} |
| 63 | + |
| 64 | +// Result captures everything observable about one CLI invocation. All |
| 65 | +// compat-test assertions operate on these three fields. |
| 66 | +type Result struct { |
| 67 | + Stdout []byte |
| 68 | + Stderr []byte |
| 69 | + ExitCode int |
| 70 | +} |
| 71 | + |
| 72 | +// StdoutString returns Stdout as a string. |
| 73 | +func (r Result) StdoutString() string { return string(r.Stdout) } |
| 74 | + |
| 75 | +// StderrString returns Stderr as a string. |
| 76 | +func (r Result) StderrString() string { return string(r.Stderr) } |
| 77 | + |
| 78 | +// Run invokes the binary with the given args and returns its observable |
| 79 | +// output. A non-zero exit code is NOT returned as an error — compat tests |
| 80 | +// frequently assert on non-zero exits, so the caller decides what counts |
| 81 | +// as a failure. ctx cancellation, process-start failure, and timeouts are |
| 82 | +// returned as errors. |
| 83 | +func (r Runner) Run(ctx context.Context, args ...string) (Result, error) { |
| 84 | + if r.Binary == "" { |
| 85 | + return Result{}, errors.New("compat: Runner.Binary is empty") |
| 86 | + } |
| 87 | + timeout := r.Timeout |
| 88 | + if timeout == 0 { |
| 89 | + timeout = 10 * time.Second |
| 90 | + } |
| 91 | + runCtx, cancel := context.WithTimeout(ctx, timeout) |
| 92 | + defer cancel() |
| 93 | + |
| 94 | + fullArgs := args |
| 95 | + if r.subcommand != "" { |
| 96 | + fullArgs = append([]string{r.subcommand}, args...) |
| 97 | + } |
| 98 | + cmd := exec.CommandContext(runCtx, r.Binary, fullArgs...) |
| 99 | + // Default to an empty env so tests are hermetic. Callers opt into |
| 100 | + // passing TZ, HOME, etc. via Runner.Env. |
| 101 | + if r.Env != nil { |
| 102 | + cmd.Env = append([]string(nil), r.Env...) |
| 103 | + } else { |
| 104 | + cmd.Env = []string{} |
| 105 | + } |
| 106 | + |
| 107 | + var stdout, stderr bytes.Buffer |
| 108 | + cmd.Stdout = &stdout |
| 109 | + cmd.Stderr = &stderr |
| 110 | + |
| 111 | + err := cmd.Run() |
| 112 | + res := Result{Stdout: stdout.Bytes(), Stderr: stderr.Bytes()} |
| 113 | + if err == nil { |
| 114 | + res.ExitCode = 0 |
| 115 | + return res, nil |
| 116 | + } |
| 117 | + // Timeout check first. exec.CommandContext kills the process when |
| 118 | + // the deadline expires, which surfaces as an *exec.ExitError on the |
| 119 | + // signal path. The package contract promises a non-nil error on |
| 120 | + // timeout, so we must detect that case before falling through to |
| 121 | + // the ExitError handler — otherwise a hung CLI looks like a clean |
| 122 | + // non-zero exit to the caller. |
| 123 | + if errors.Is(runCtx.Err(), context.DeadlineExceeded) { |
| 124 | + return res, fmt.Errorf("compat: %s timed out after %s", r.Binary, timeout) |
| 125 | + } |
| 126 | + var exitErr *exec.ExitError |
| 127 | + if errors.As(err, &exitErr) { |
| 128 | + res.ExitCode = exitErr.ExitCode() |
| 129 | + return res, nil |
| 130 | + } |
| 131 | + return res, fmt.Errorf("compat: failed to run %s: %w", r.Binary, err) |
| 132 | +} |
| 133 | + |
| 134 | +// MustRun is the testing-helper equivalent of Run: it fails the test on |
| 135 | +// any non-exit-code error (timeout, missing binary, etc.) and returns the |
| 136 | +// Result on success. |
| 137 | +func (r Runner) MustRun(t *testing.T, args ...string) Result { |
| 138 | + t.Helper() |
| 139 | + res, err := r.Run(context.Background(), args...) |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("compat: %v", err) |
| 142 | + } |
| 143 | + return res |
| 144 | +} |
| 145 | + |
| 146 | +// WithEnv returns a copy of r with environment variable KEY=VALUE pairs |
| 147 | +// appended. Useful for setting TZ on a per-test basis without mutating |
| 148 | +// the receiver. |
| 149 | +func (r Runner) WithEnv(kv ...string) Runner { |
| 150 | + out := r |
| 151 | + out.Env = append(append([]string(nil), r.Env...), kv...) |
| 152 | + return out |
| 153 | +} |
| 154 | + |
| 155 | +// WithSubcommand returns a copy of r whose Run prepends sub as the |
| 156 | +// first command-line argument. Section bundles use this internally to |
| 157 | +// dispatch per-subcommand when Runner.Subcommands is non-empty; |
| 158 | +// integrators normally set Subcommands and let the bundle do it. |
| 159 | +// |
| 160 | +// Calling WithSubcommand again replaces (not stacks) the previous |
| 161 | +// value; nested subcommand paths are out of scope for the current |
| 162 | +// contract. |
| 163 | +func (r Runner) WithSubcommand(sub string) Runner { |
| 164 | + out := r |
| 165 | + out.subcommand = sub |
| 166 | + return out |
| 167 | +} |
| 168 | + |
| 169 | +// Subcommand returns the subcommand that Run will prepend to args, or |
| 170 | +// the empty string if none is set. Section bundles use this in subtest |
| 171 | +// names so failures point at the offending subcommand. |
| 172 | +func (r Runner) Subcommand() string { return r.subcommand } |
0 commit comments