Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to create a clio.Application that runs test assertions instead of cmd.RunE #37

Merged
merged 8 commits into from
Jan 31, 2024
85 changes: 85 additions & 0 deletions testutils/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package testutils
wagoodman marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/anchore/clio"
"github.com/google/go-cmp/cmp"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"sync"
"testing"
)

// TODO: is this needed given WrapForTesting? We need to think about
// how clio is handling global state.
wagoodman marked this conversation as resolved.
Show resolved Hide resolved
// in particular, the testing pattern can cause initializers to be called more than once.
func NewForTesting(t *testing.T, cfg *clio.SetupConfig, assertions ...AssertionFunc) clio.Application {
wagoodman marked this conversation as resolved.
Show resolved Hide resolved
a := clio.New(*cfg)

var asserter assertionClosure = func(cmd *cobra.Command, args []string, cfgs ...any) {
for _, assertion := range assertions {
assertion(t, cmd, args, cfgs...)
}
}

return &testApplication{
a,
asserter,
sync.Once{},
}
}

func WrapForTesting(t *testing.T, a clio.Application, assertions ...AssertionFunc) clio.Application {
var asserter assertionClosure = func(cmd *cobra.Command, args []string, cfgs ...any) {
for _, assertion := range assertions {
assertion(t, cmd, args, cfgs...)
}
}

return &testApplication{
a,
asserter,
sync.Once{},
}
}

type AssertionFunc func(t *testing.T, cmd *cobra.Command, args []string, cfgs ...any)

func OptionsEquals(opts any) AssertionFunc {
return func(t *testing.T, cmd *cobra.Command, args []string, cfgs ...any) {
assert.Equal(t, len(cfgs), 1)
if d := cmp.Diff(opts, cfgs[0]); d != "" {
t.Errorf("mismatched options (+got -want):\n%s", d)
}
}
}

type assertionClosure func(cmd *cobra.Command, args []string, cfgs ...any)

type testApplication struct {
clio.Application
assertion assertionClosure
initOnce sync.Once
}

func (a *testApplication) SetupCommand(cmd *cobra.Command, cfgs ...any) *cobra.Command {
cmd.RunE = func(cmd *cobra.Command, args []string) error {
a.assertion(cmd, args, cfgs...)
return nil
}
return a.Application.SetupCommand(cmd, cfgs...)
}

func (a *testApplication) SetupRootCommand(cmd *cobra.Command, cfgs ...any) *cobra.Command {
cmd.RunE = func(cmd *cobra.Command, args []string) error {
a.assertion(cmd, args, cfgs...)
return nil
}
return a.Application.SetupRootCommand(cmd, cfgs...)
}

/*
// TODO: WISHLIST:
1. Pass in an io.Reader that's wired up to the config file, _or_ (almost as good) pass in a test fixture path
2. Set env vars by passing map[string]string
3. Helper to pass in a fully populated options struct and assert that it's correct
*/
Loading