2020-08-06 20:58:47 +03:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/neilotoole/slogt"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli"
|
|
|
|
"github.com/neilotoole/sq/cli/config"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
// newTestRunCtx returns a RunContext for testing, along
|
|
|
|
// with buffers for out and errOut (instead of the
|
|
|
|
// rc writing to stdout and stderr). The contents of
|
|
|
|
// these buffers can be written to t.Log() if desired.
|
2023-04-19 08:28:09 +03:00
|
|
|
// The srcs args are added to rc.Config.Collection.
|
2023-03-19 09:18:54 +03:00
|
|
|
//
|
|
|
|
// If cfgStore is nil, a new one is created in a temp dir.
|
|
|
|
func newTestRunCtx(t testing.TB, cfgStore config.Store) (rc *cli.RunContext, out, errOut *bytes.Buffer) {
|
2020-08-06 20:58:47 +03:00
|
|
|
out = &bytes.Buffer{}
|
|
|
|
errOut = &bytes.Buffer{}
|
|
|
|
|
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)
|
|
|
|
cfgStore = &config.YAMLFileStore{Path: filepath.Join(cfgDir, "sq.yml")}
|
|
|
|
cfg = config.New()
|
|
|
|
require.NoError(t, cfgStore.Save(cfg))
|
|
|
|
} else {
|
|
|
|
cfg, err = cfgStore.Load()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
rc = &cli.RunContext{
|
|
|
|
Stdin: os.Stdin,
|
|
|
|
Out: out,
|
|
|
|
ErrOut: errOut,
|
2023-04-02 22:49:45 +03:00
|
|
|
Log: slogt.New(t),
|
2023-03-19 09:18:54 +03:00
|
|
|
Config: cfg,
|
|
|
|
ConfigStore: cfgStore,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc, out, errOut
|
|
|
|
}
|
|
|
|
|
|
|
|
// run is a helper for testing sq commands.
|
2023-01-01 06:17:44 +03:00
|
|
|
type Run struct {
|
2020-08-06 20:58:47 +03:00
|
|
|
t *testing.T
|
|
|
|
mu sync.Mutex
|
|
|
|
rc *cli.RunContext
|
|
|
|
out *bytes.Buffer
|
|
|
|
errOut *bytes.Buffer
|
|
|
|
used bool
|
|
|
|
|
|
|
|
// When true, out and errOut are not logged.
|
|
|
|
hushOutput bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// newRun returns a new run instance for testing sq commands.
|
2023-03-19 09:18:54 +03:00
|
|
|
// If from is non-nil, its config is used. This allows sequential
|
|
|
|
// commands to use the same config.
|
|
|
|
func newRun(t *testing.T, from *Run) *Run {
|
2023-01-01 06:17:44 +03:00
|
|
|
ru := &Run{t: t}
|
2023-03-19 09:18:54 +03:00
|
|
|
var cfgStore config.Store
|
|
|
|
if from != nil {
|
|
|
|
cfgStore = from.rc.ConfigStore
|
|
|
|
}
|
|
|
|
ru.rc, ru.out, ru.errOut = newTestRunCtx(t, cfgStore)
|
2020-08-06 20:58:47 +03:00
|
|
|
return ru
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// add adds srcs to ru.rc.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-01-01 06:17:44 +03:00
|
|
|
func (ru *Run) add(srcs ...source.Source) *Run {
|
2020-08-06 20:58:47 +03:00
|
|
|
ru.mu.Lock()
|
|
|
|
defer ru.mu.Unlock()
|
|
|
|
|
|
|
|
if len(srcs) == 0 {
|
|
|
|
return ru
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
coll := ru.rc.Config.Collection
|
|
|
|
hasActive := ru.rc.Config.Collection.Active() != nil
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
for _, src := range srcs {
|
|
|
|
src := src
|
2023-04-19 08:28:09 +03:00
|
|
|
require.NoError(ru.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)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(ru.t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ru
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// args for execution. This method may only be invoked once.
|
2023-01-01 06:17:44 +03:00
|
|
|
// The backing RunContext will also be closed. If an error
|
|
|
|
// occurs on the client side during execution, that error is returned.
|
|
|
|
// Either ru.out or ru.errOut will be filled, according to what the
|
|
|
|
// CLI outputs.
|
|
|
|
func (ru *Run) Exec(args ...string) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
ru.mu.Lock()
|
|
|
|
defer ru.mu.Unlock()
|
|
|
|
|
2023-01-01 06:17:44 +03:00
|
|
|
return ru.doExec(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ru *Run) doExec(args []string) error {
|
|
|
|
defer func() { ru.used = true }()
|
|
|
|
|
|
|
|
require.False(ru.t, ru.used, "Run instance must only be used once")
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-01-01 06:17:44 +03:00
|
|
|
ctx, cancelFn := context.WithCancel(context.Background())
|
|
|
|
ru.t.Cleanup(cancelFn)
|
|
|
|
|
|
|
|
execErr := cli.ExecuteWith(ctx, ru.rc, args)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
if !ru.hushOutput {
|
2023-01-01 06:17:44 +03:00
|
|
|
// We log the CLI's output now (before calling rc.Close) because
|
2020-08-06 20:58:47 +03:00
|
|
|
// it reads better in testing's output that way.
|
|
|
|
if ru.out.Len() > 0 {
|
|
|
|
ru.t.Log(strings.TrimSuffix(ru.out.String(), "\n"))
|
|
|
|
}
|
|
|
|
if ru.errOut.Len() > 0 {
|
|
|
|
ru.t.Log(strings.TrimSuffix(ru.errOut.String(), "\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closeErr := ru.rc.Close()
|
|
|
|
if execErr != nil {
|
|
|
|
// We return the ExecuteWith err first
|
|
|
|
return execErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the closeErr (hopefully is nil)
|
|
|
|
return closeErr
|
|
|
|
}
|
|
|
|
|
2023-01-01 06:17:44 +03:00
|
|
|
// Bind marshals Run.Out to v (as JSON), failing the test on any error.
|
|
|
|
func (ru *Run) Bind(v any) *Run {
|
|
|
|
ru.mu.Lock()
|
|
|
|
defer ru.mu.Unlock()
|
|
|
|
|
|
|
|
err := json.Unmarshal(ru.out.Bytes(), &v)
|
|
|
|
require.NoError(ru.t, err)
|
|
|
|
return ru
|
|
|
|
}
|
|
|
|
|
|
|
|
// BindMap is a convenience method for binding ru.Out to a map.
|
|
|
|
func (ru *Run) BindMap() map[string]any {
|
|
|
|
m := map[string]any{}
|
|
|
|
ru.Bind(&m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// mustReadCSV reads CSV from ru.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".
|
|
|
|
func (ru *Run) mustReadCSV() [][]string {
|
2020-08-06 20:58:47 +03:00
|
|
|
ru.mu.Lock()
|
|
|
|
defer ru.mu.Unlock()
|
|
|
|
|
|
|
|
recs, err := csv.NewReader(ru.out).ReadAll()
|
|
|
|
require.NoError(ru.t, err)
|
|
|
|
return recs
|
|
|
|
}
|
|
|
|
|
|
|
|
// hush suppresses the printing of output collected in out
|
2023-04-19 08:28:09 +03:00
|
|
|
// and errOut to t.Log. Collection to true for tests
|
2020-08-06 20:58:47 +03:00
|
|
|
// that output excessive content, binary files, etc.
|
2023-01-01 06:17:44 +03:00
|
|
|
func (ru *Run) hush() *Run {
|
2020-08-06 20:58:47 +03:00
|
|
|
ru.hushOutput = true
|
|
|
|
return ru
|
|
|
|
}
|