mukan-sdk/testutil/cmdtest/system.go
Mukan Erkin Törük 20afb5db80
Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
initial: sovereign Mukan Network fork
2026-05-11 03:18:24 +03:00

120 lines
3.3 KiB
Go

// Package cmdtest contains a framework for testing cobra Commands within Go unit tests.
package cmdtest
import (
"bytes"
"context"
"io"
"github.com/spf13/cobra"
)
// System is a system under test.
type System struct {
commands []*cobra.Command
}
// NewSystem returns a new System.
func NewSystem() *System {
// We aren't doing any special initialization yet,
// but let's encourage a constructor to make it simpler
// to update later, if needed.
return new(System)
}
// AddCommands sets commands to be available to the Run family of methods on s.
func (s *System) AddCommands(cmds ...*cobra.Command) {
s.commands = append(s.commands, cmds...)
}
// RunResult is the stdout and stderr resulting from a call to a System's Run family of methods,
// and any error that was returned.
type RunResult struct {
Stdout, Stderr bytes.Buffer
Err error
}
// Run calls s.RunC with context.Background().
func (s *System) Run(args ...string) RunResult {
return s.RunC(context.Background(), args...)
}
// RunC calls s.RunWithInput with an empty stdin.
func (s *System) RunC(ctx context.Context, args ...string) RunResult {
return s.RunWithInputC(ctx, bytes.NewReader(nil), args...)
}
// RunWithInput calls s.RunWithInputC with context.Background().
func (s *System) RunWithInput(in io.Reader, args ...string) RunResult {
return s.RunWithInputC(context.Background(), in, args...)
}
// RunWithInputC executes a new root command with subcommands
// that were set in s.AddCommands().
// The command's stdin is set to the in argument.
// RunWithInputC returns a RunResult wrapping stdout, stderr, and any returned error.
func (s *System) RunWithInputC(ctx context.Context, in io.Reader, args ...string) RunResult {
rootCmd := &cobra.Command{}
rootCmd.AddCommand(s.commands...)
rootCmd.SetIn(in)
var res RunResult
rootCmd.SetOut(&res.Stdout)
rootCmd.SetErr(&res.Stderr)
rootCmd.SetArgs(args)
res.Err = rootCmd.ExecuteContext(ctx)
return res
}
// MustRun calls s.Run, but also calls t.FailNow if RunResult.Err is not nil.
func (s *System) MustRun(t TestingT, args ...string) RunResult {
t.Helper()
return s.MustRunC(t, context.Background(), args...)
}
// MustRunC calls s.RunWithInput, but also calls t.FailNow if RunResult.Err is not nil.
func (s *System) MustRunC(t TestingT, ctx context.Context, args ...string) RunResult {
t.Helper()
return s.MustRunWithInputC(t, ctx, bytes.NewReader(nil), args...)
}
// MustRunWithInput calls s.RunWithInput, but also calls t.FailNow if RunResult.Err is not nil.
func (s *System) MustRunWithInput(t TestingT, in io.Reader, args ...string) RunResult {
t.Helper()
return s.MustRunWithInputC(t, context.Background(), in, args...)
}
// MustRunWithInputC calls s.RunWithInputC, but also calls t.FailNow if RunResult.Err is not nil.
func (s *System) MustRunWithInputC(t TestingT, ctx context.Context, in io.Reader, args ...string) RunResult {
t.Helper()
res := s.RunWithInputC(ctx, in, args...)
if res.Err != nil {
t.Logf("Error executing %v: %v", args, res.Err)
t.Logf("Stdout: %q", res.Stdout.String())
t.Logf("Stderr: %q", res.Stderr.String())
t.FailNow()
}
return res
}
// TestingT is a subset of testing.TB,
// containing only what the (*System).Must methods use.
//
// This simplifies using other testing wrappers,
// such as testify suite, etc.
type TestingT interface {
Helper()
Logf(format string, args ...any)
FailNow()
}