2023-05-19 17:24:18 +03:00
|
|
|
// Package testrun contains helper functionality for executing CLI tests.
|
|
|
|
package testrun
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/csv"
|
2023-01-01 06:17:44 +03:00
|
|
|
"encoding/json"
|
2020-08-06 20:58:47 +03:00
|
|
|
"os"
|
2023-03-19 09:18:54 +03:00
|
|
|
"path/filepath"
|
2020-08-06 20:58:47 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli"
|
|
|
|
"github.com/neilotoole/sq/cli/config"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/cli/config/yamlstore"
|
|
|
|
"github.com/neilotoole/sq/cli/run"
|
2023-11-22 00:49:52 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/ioz"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
2024-01-15 04:45:34 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lgt"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
2024-01-25 09:29:55 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/files"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
2024-01-15 04:45:34 +03:00
|
|
|
"github.com/neilotoole/sq/testh"
|
|
|
|
"github.com/neilotoole/sq/testh/tu"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
// TestRun is a helper for testing sq commands.
|
|
|
|
type TestRun struct {
|
2023-06-13 19:06:18 +03:00
|
|
|
T testing.TB
|
|
|
|
Context context.Context
|
2023-11-19 03:05:48 +03:00
|
|
|
mu *sync.Mutex
|
2023-06-13 19:06:18 +03:00
|
|
|
Run *run.Run
|
2023-11-19 03:05:48 +03:00
|
|
|
|
|
|
|
// Out contains the output written to stdout after TestRun.Exec is invoked.
|
|
|
|
Out *bytes.Buffer
|
|
|
|
|
|
|
|
// ErrOut contains the output written to stderr after TestRun.Exec is invoked.
|
|
|
|
ErrOut *bytes.Buffer
|
|
|
|
used bool
|
2023-05-19 17:24:18 +03:00
|
|
|
|
|
|
|
// When true, out and errOut are not logged.
|
|
|
|
hushOutput bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new run instance for testing sq commands.
|
|
|
|
// If from is non-nil, its config is used. This allows sequential
|
|
|
|
// commands to use the same config.
|
2023-11-19 03:05:48 +03:00
|
|
|
//
|
|
|
|
// You can also use TestRun.Reset to reuse a TestRun instance.
|
2023-06-13 19:06:18 +03:00
|
|
|
func New(ctx context.Context, t testing.TB, from *TestRun) *TestRun {
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
2023-06-18 09:05:09 +03:00
|
|
|
if !lg.InContext(ctx) {
|
2024-01-15 04:45:34 +03:00
|
|
|
// FIXME: Get rid of this abomination
|
|
|
|
ctx = lg.NewContext(ctx, lgt.New(t))
|
2023-06-18 09:05:09 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
tr := &TestRun{T: t, Context: ctx, mu: &sync.Mutex{}}
|
2023-05-19 17:24:18 +03:00
|
|
|
|
|
|
|
var cfgStore config.Store
|
2024-01-15 04:45:34 +03:00
|
|
|
var cacheDir string
|
2023-05-19 17:24:18 +03:00
|
|
|
if from != nil {
|
|
|
|
cfgStore = from.Run.ConfigStore
|
2024-01-15 04:45:34 +03:00
|
|
|
cacheDir = from.Run.Files.CacheDir()
|
2023-06-22 08:48:58 +03:00
|
|
|
tr.hushOutput = from.hushOutput
|
2023-05-19 17:24:18 +03:00
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
tr.Run, tr.Out, tr.ErrOut = newRun(ctx, t, cfgStore, cacheDir)
|
|
|
|
o := options.Merge(options.FromContext(ctx), tr.Run.Config.Options)
|
|
|
|
tr.Context = options.NewContext(ctx, o)
|
2023-05-19 17:24:18 +03:00
|
|
|
return tr
|
|
|
|
}
|
|
|
|
|
|
|
|
// newRun returns a Run for testing, along
|
2020-08-06 20:58:47 +03:00
|
|
|
// with buffers for out and errOut (instead of the
|
2023-05-19 17:24:18 +03:00
|
|
|
// ru writing to stdout and stderr). The contents of
|
2020-08-06 20:58:47 +03:00
|
|
|
// these buffers can be written to t.Log() if desired.
|
2023-03-19 09:18:54 +03:00
|
|
|
//
|
|
|
|
// If cfgStore is nil, a new one is created in a temp dir.
|
2024-01-15 04:45:34 +03:00
|
|
|
func newRun(ctx context.Context, t testing.TB,
|
|
|
|
cfgStore config.Store, cacheDir string,
|
|
|
|
) (ru *run.Run, out, errOut *bytes.Buffer) {
|
2020-08-06 20:58:47 +03:00
|
|
|
out = &bytes.Buffer{}
|
|
|
|
errOut = &bytes.Buffer{}
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
optsReg := &options.Registry{}
|
|
|
|
cli.RegisterDefaultOpts(optsReg)
|
|
|
|
|
2023-03-19 09:18:54 +03:00
|
|
|
var cfg *config.Config
|
|
|
|
var err error
|
|
|
|
if cfgStore == nil {
|
|
|
|
var cfgDir string
|
|
|
|
cfgDir, err = os.MkdirTemp("", "sq_test")
|
|
|
|
require.NoError(t, err)
|
2023-05-01 06:59:34 +03:00
|
|
|
cfgStore = &yamlstore.Store{
|
|
|
|
Path: filepath.Join(cfgDir, "sq.yml"),
|
|
|
|
OptionsRegistry: optsReg,
|
|
|
|
}
|
2023-03-19 09:18:54 +03:00
|
|
|
cfg = config.New()
|
2023-04-22 16:37:07 +03:00
|
|
|
require.NoError(t, cfgStore.Save(ctx, cfg))
|
2023-03-19 09:18:54 +03:00
|
|
|
} else {
|
2023-05-01 06:59:34 +03:00
|
|
|
cfg, err = cfgStore.Load(ctx)
|
2023-03-19 09:18:54 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
ru = &run.Run{
|
2023-04-26 18:16:42 +03:00
|
|
|
Stdin: os.Stdin,
|
|
|
|
Out: out,
|
|
|
|
ErrOut: errOut,
|
|
|
|
Config: cfg,
|
|
|
|
ConfigStore: cfgStore,
|
|
|
|
OptionsRegistry: optsReg,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
// The Files instance needs unique dirs for temp and cache because
|
|
|
|
// the test runs may execute in parallel inside the same test binary
|
|
|
|
// process, thus breaking the pid-based lockfile mechanism.
|
|
|
|
|
|
|
|
// If cacheDir was supplied, use that one, because it's probably the
|
|
|
|
// cache dir from a previous run, that we want to reuse. If not supplied,
|
|
|
|
// create a unique cache dir for this run.
|
|
|
|
// The Files instance generally needs unique dirs for temp and cache because
|
|
|
|
// the test runs may execute in parallel inside the same test binary
|
|
|
|
// process, thus breaking the pid-based lockfile mechanism.
|
|
|
|
if cacheDir == "" {
|
|
|
|
cacheDir = tu.CacheDir(t, false)
|
|
|
|
}
|
|
|
|
|
2024-01-25 09:29:55 +03:00
|
|
|
ru.Files, err = files.New(
|
2024-01-15 04:45:34 +03:00
|
|
|
ctx,
|
|
|
|
ru.OptionsRegistry,
|
|
|
|
testh.TempLockFunc(t),
|
|
|
|
tu.TempDir(t, false),
|
|
|
|
cacheDir,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-06-13 19:06:18 +03:00
|
|
|
require.NoError(t, cli.FinishRunInit(ctx, ru))
|
2023-05-19 17:24:18 +03:00
|
|
|
return ru, out, errOut
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// Reset resets tr to a clean slate. Note that a new TestRun instance
|
|
|
|
// is created behind the scenes, so any references to the previous
|
|
|
|
// TestRun's fields are now invalid.
|
|
|
|
//
|
|
|
|
// See also: testrun.New.
|
|
|
|
func (tr *TestRun) Reset() *TestRun {
|
|
|
|
tr2 := New(tr.Context, tr.T, tr)
|
|
|
|
*tr = *tr2
|
|
|
|
return tr
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
// Add adds srcs to tr.Run.Config.Collection. If the collection
|
2020-08-06 20:58:47 +03:00
|
|
|
// does not already have an active source, the first element
|
2023-03-19 09:18:54 +03:00
|
|
|
// of srcs is used as the active source.
|
2023-06-13 19:06:18 +03:00
|
|
|
//
|
|
|
|
// REVISIT: Why not use *source.Source instead of the value?
|
2023-05-19 17:24:18 +03:00
|
|
|
func (tr *TestRun) Add(srcs ...source.Source) *TestRun {
|
|
|
|
tr.mu.Lock()
|
|
|
|
defer tr.mu.Unlock()
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
if len(srcs) == 0 {
|
2023-05-19 17:24:18 +03:00
|
|
|
return tr
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
coll := tr.Run.Config.Collection
|
|
|
|
hasActive := tr.Run.Config.Collection.Active() != nil
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
for _, src := range srcs {
|
|
|
|
src := src
|
2023-05-19 17:24:18 +03:00
|
|
|
require.NoError(tr.T, coll.Add(&src))
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !hasActive {
|
2023-04-19 08:28:09 +03:00
|
|
|
_, err := coll.SetActive(srcs[0].Handle, false)
|
2023-05-19 17:24:18 +03:00
|
|
|
require.NoError(tr.T, err)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-06-13 19:06:18 +03:00
|
|
|
err := tr.Run.ConfigStore.Save(tr.Context, tr.Run.Config)
|
|
|
|
require.NoError(tr.T, err)
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
return tr
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-01-01 06:17:44 +03:00
|
|
|
// Exec executes the sq command specified by args. If the first
|
2020-08-06 20:58:47 +03:00
|
|
|
// element of args is not "sq", that value is prepended to the
|
2023-11-19 03:05:48 +03:00
|
|
|
// args for execution. This method may only be invoked once on
|
|
|
|
// this TestRun instance, unless TestRun.Reset is called.
|
2023-05-19 17:24:18 +03:00
|
|
|
// The backing Run will also be closed. If an error
|
2023-01-01 06:17:44 +03:00
|
|
|
// occurs on the client side during execution, that error is returned.
|
2023-05-19 17:24:18 +03:00
|
|
|
// Either tr.Out or tr.ErrOut will be filled, according to what the
|
2023-01-01 06:17:44 +03:00
|
|
|
// CLI outputs.
|
2023-05-19 17:24:18 +03:00
|
|
|
func (tr *TestRun) Exec(args ...string) error {
|
|
|
|
tr.mu.Lock()
|
|
|
|
defer tr.mu.Unlock()
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
return tr.doExec(args)
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
func (tr *TestRun) doExec(args []string) error {
|
|
|
|
defer func() { tr.used = true }()
|
2023-01-01 06:17:44 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
require.False(tr.T, tr.used, "TestRun instance must only be used once")
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-06-13 19:06:18 +03:00
|
|
|
ctx, cancelFn := context.WithCancel(tr.Context)
|
2023-05-19 17:24:18 +03:00
|
|
|
tr.T.Cleanup(cancelFn)
|
2023-01-01 06:17:44 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
execErr := cli.ExecuteWith(ctx, tr.Run, args)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
if !tr.hushOutput {
|
|
|
|
// We log the CLI's output now (before calling ru.Close) because
|
2020-08-06 20:58:47 +03:00
|
|
|
// it reads better in testing's output that way.
|
2023-05-19 17:24:18 +03:00
|
|
|
if tr.Out.Len() > 0 {
|
|
|
|
tr.T.Log(strings.TrimSuffix(tr.Out.String(), "\n"))
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2023-05-19 17:24:18 +03:00
|
|
|
if tr.ErrOut.Len() > 0 {
|
|
|
|
tr.T.Log(strings.TrimSuffix(tr.ErrOut.String(), "\n"))
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
closeErr := tr.Run.Close()
|
2020-08-06 20:58:47 +03:00
|
|
|
if execErr != nil {
|
|
|
|
// We return the ExecuteWith err first
|
|
|
|
return execErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the closeErr (hopefully is nil)
|
|
|
|
return closeErr
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
// Bind marshals tr.Out to v (as JSON), failing the test on any error.
|
|
|
|
func (tr *TestRun) Bind(v any) *TestRun {
|
|
|
|
tr.mu.Lock()
|
|
|
|
defer tr.mu.Unlock()
|
2023-01-01 06:17:44 +03:00
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
err := json.Unmarshal(tr.Out.Bytes(), v)
|
|
|
|
require.NoError(tr.T, err)
|
|
|
|
return tr
|
|
|
|
}
|
|
|
|
|
|
|
|
// BindYAML marshals tr.Out to v (as YAML), failing the test on any error.
|
|
|
|
func (tr *TestRun) BindYAML(v any) *TestRun {
|
|
|
|
tr.mu.Lock()
|
|
|
|
defer tr.mu.Unlock()
|
|
|
|
|
|
|
|
err := ioz.UnmarshallYAML(tr.Out.Bytes(), v)
|
2023-05-19 17:24:18 +03:00
|
|
|
require.NoError(tr.T, err)
|
|
|
|
return tr
|
2023-01-01 06:17:44 +03:00
|
|
|
}
|
|
|
|
|
2023-07-08 18:21:27 +03:00
|
|
|
// BindMap is a convenience method for binding tr.Out to a map
|
|
|
|
// (assuming tr.Out is JSON).
|
2023-05-19 17:24:18 +03:00
|
|
|
func (tr *TestRun) BindMap() map[string]any {
|
2023-01-01 06:17:44 +03:00
|
|
|
m := map[string]any{}
|
2023-05-19 17:24:18 +03:00
|
|
|
tr.Bind(&m)
|
2023-01-01 06:17:44 +03:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-07-08 18:21:27 +03:00
|
|
|
// BindSliceMap is a convenience method for binding tr.Out
|
|
|
|
// to a slice of map (assuming tr.Out is JSON).
|
|
|
|
func (tr *TestRun) BindSliceMap() []map[string]any {
|
|
|
|
var a []map[string]any
|
|
|
|
tr.Bind(&a)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// BindCSV reads CSV from tr.Out and returns all records,
|
2023-01-01 06:17:44 +03:00
|
|
|
// failing the testing on any problem. Obviously the Exec call
|
|
|
|
// should have specified "--csv".
|
2023-07-08 18:21:27 +03:00
|
|
|
func (tr *TestRun) BindCSV() [][]string {
|
2023-05-19 17:24:18 +03:00
|
|
|
tr.mu.Lock()
|
|
|
|
defer tr.mu.Unlock()
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
recs, err := csv.NewReader(tr.Out).ReadAll()
|
|
|
|
require.NoError(tr.T, err)
|
2020-08-06 20:58:47 +03:00
|
|
|
return recs
|
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// OutString returns the contents of tr.Out as a string,
|
|
|
|
// with the final trailing newline removed.
|
|
|
|
func (tr *TestRun) OutString() string {
|
|
|
|
return strings.TrimSuffix(tr.Out.String(), "\n")
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
// Hush suppresses the printing of output collected in out
|
2023-06-18 04:28:11 +03:00
|
|
|
// and errOut to t.Log. Set to true for tests
|
2020-08-06 20:58:47 +03:00
|
|
|
// that output excessive content, binary files, etc.
|
2023-05-19 17:24:18 +03:00
|
|
|
func (tr *TestRun) Hush() *TestRun {
|
|
|
|
tr.hushOutput = true
|
|
|
|
return tr
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|