diff --git a/action_test.go b/action_test.go index d1706e795..3430a18a0 100644 --- a/action_test.go +++ b/action_test.go @@ -1,44 +1,17 @@ package carapace import ( - "encoding/json" "fmt" "os" - "sort" "testing" "time" - "github.com/carapace-sh/carapace/internal/assert" "github.com/carapace-sh/carapace/internal/common" + "github.com/carapace-sh/carapace/pkg/assert" "github.com/carapace-sh/carapace/pkg/style" "github.com/carapace-sh/carapace/pkg/uid" ) -func assertEqual(t *testing.T, expected, actual InvokedAction) { - sort.Sort(common.ByValue(expected.action.rawValues)) - sort.Sort(common.ByValue(actual.action.rawValues)) - - e, _ := json.MarshalIndent(expected.action.rawValues, "", " ") - a, _ := json.MarshalIndent(actual.action.rawValues, "", " ") - assert.Equal(t, string(e), string(a)) - - eMeta, _ := json.MarshalIndent(expected.action.meta, "", " ") - aMeta, _ := json.MarshalIndent(actual.action.meta, "", " ") - assert.Equal(t, string(eMeta), string(aMeta)) -} - -func assertNotEqual(t *testing.T, expected, actual InvokedAction) { - sort.Sort(common.ByValue(expected.action.rawValues)) - sort.Sort(common.ByValue(actual.action.rawValues)) - - e, _ := json.MarshalIndent(expected.action.rawValues, "", " ") - a, _ := json.MarshalIndent(actual.action.rawValues, "", " ") - - if string(e) == string(a) { - t.Errorf("should differ:\n%v", a) - } -} - func TestActionCallback(t *testing.T) { a := ActionCallback(func(c Context) Action { return ActionCallback(func(c Context) Action { @@ -53,7 +26,7 @@ func TestActionCallback(t *testing.T) { }, } actual := a.Invoke(Context{}) - assertEqual(t, expected, actual) + assert.Equal(t, expected, actual) } func TestCache(t *testing.T) { @@ -65,11 +38,11 @@ func TestCache(t *testing.T) { a1 := f().Invoke(Context{}) a2 := f().Invoke(Context{}) - assertEqual(t, a1, a2) + assert.Equal(t, a1, a2) time.Sleep(16 * time.Millisecond) a3 := f().Invoke(Context{}) - assertNotEqual(t, a1, a3) + assert.NotEqual(t, a1, a3) } func TestSkipCache(t *testing.T) { @@ -111,7 +84,7 @@ func TestNoSpace(t *testing.T) { } func TestActionDirectories(t *testing.T) { - assertEqual(t, + assert.Equal(t, ActionStyledValues( "example/", style.Of(style.Blue, style.Bold), "example-nonposix/", style.Of(style.Blue, style.Bold), @@ -130,7 +103,7 @@ func TestActionDirectories(t *testing.T) { ActionDirectories().Invoke(Context{Value: ""}).Filter("vendor/"), ) - assertEqual(t, + assert.Equal(t, ActionStyledValues( "example/", style.Of(style.Blue, style.Bold), "example-nonposix/", style.Of(style.Blue, style.Bold), @@ -149,7 +122,7 @@ func TestActionDirectories(t *testing.T) { ActionDirectories().Invoke(Context{Value: "./"}).Filter("./vendor/"), ) - assertEqual(t, + assert.Equal(t, ActionStyledValues( "cmd/", style.Of(style.Blue, style.Bold), ).NoSpace('/').Tag("directories").Invoke(Context{}).Prefix("example/").UidF(uid.Map( @@ -158,7 +131,7 @@ func TestActionDirectories(t *testing.T) { ActionDirectories().Invoke(Context{Value: "example/"}), ) - assertEqual(t, + assert.Equal(t, ActionStyledValues( "cmd/", style.Of(style.Blue, style.Bold), ).NoSpace('/').Tag("directories").Invoke(Context{}).Prefix("example/").UidF(uid.Map( @@ -169,7 +142,7 @@ func TestActionDirectories(t *testing.T) { } func TestActionFiles(t *testing.T) { - assertEqual(t, + assert.Equal(t, ActionStyledValues( "README.md", style.Default, "example/", style.Of(style.Blue, style.Bold), @@ -190,7 +163,7 @@ func TestActionFiles(t *testing.T) { ActionFiles(".md").Invoke(Context{Value: ""}).Filter("vendor/"), ) - assertEqual(t, + assert.Equal(t, ActionStyledValues( "README.md", style.Default, "cmd/", style.Of(style.Blue, style.Bold), @@ -209,17 +182,17 @@ func TestActionFiles(t *testing.T) { func TestActionFilesChdir(t *testing.T) { oldWd, _ := os.Getwd() - assertEqual(t, + assert.Equal(t, ActionMessage(fmt.Sprintf("stat %v: no such file or directory", wd("nonexistent"))).Invoke(Context{}), ActionFiles(".md").Chdir("nonexistent").Invoke(Context{}), ) - assertEqual(t, + assert.Equal(t, ActionMessage(fmt.Sprintf("not a directory: %v/go.mod", wd(""))).Invoke(Context{}), ActionFiles(".md").Chdir("go.mod").Invoke(Context{Value: ""}), ) - assertEqual(t, + assert.Equal(t, ActionStyledValues( "action.go", style.Default, "snippet.go", style.Default, @@ -239,14 +212,14 @@ func TestActionMessage(t *testing.T) { expected := ActionValues() expected.meta.Messages.Add("example message") - assertEqual(t, + assert.Equal(t, expected.Invoke(Context{}), ActionMessage("example message").Invoke(Context{Value: "docs/"}), ) } func TestActionMessageSuppress(t *testing.T) { - assertEqual(t, + assert.Equal(t, Batch( ActionMessage("example message").Suppress("example"), ActionValues("test"), @@ -258,12 +231,12 @@ func TestActionMessageSuppress(t *testing.T) { func TestActionExecCommand(t *testing.T) { context := NewContext() context.Value = "docs/" - assertEqual(t, + assert.Equal(t, ActionMessage("go unknown: unknown command").Invoke(NewContext()).Prefix("docs/"), ActionExecCommand("go", "unknown")(func(output []byte) Action { return ActionValues() }).Invoke(context), ) - assertEqual(t, + assert.Equal(t, ActionValues("module github.com/carapace-sh/carapace\n").Invoke(Context{}), ActionExecCommand("head", "-n1", "go.mod")(func(output []byte) Action { return ActionValues(string(output)) }).Invoke(Context{}), ) diff --git a/batch_test.go b/batch_test.go index fbb2482be..12b0c33cc 100644 --- a/batch_test.go +++ b/batch_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/carapace-sh/carapace/internal/common" + "github.com/carapace-sh/carapace/pkg/assert" ) func TestBatch(t *testing.T) { @@ -18,7 +19,7 @@ func TestBatch(t *testing.T) { }, } actual := b.Invoke(Context{}).Merge() - assertEqual(t, expected, actual) + assert.Equal(t, expected, actual) } func TestBatchSingle(t *testing.T) { @@ -31,7 +32,7 @@ func TestBatchSingle(t *testing.T) { }, } actual := b.Invoke(Context{}).Merge() - assertEqual(t, expected, actual) + assert.Equal(t, expected, actual) } func TestBatchNone(t *testing.T) { @@ -42,7 +43,7 @@ func TestBatchNone(t *testing.T) { }, } actual := b.Invoke(Context{}).Merge() - assertEqual(t, expected, actual) + assert.Equal(t, expected, actual) } func TestBatchToA(t *testing.T) { @@ -57,5 +58,5 @@ func TestBatchToA(t *testing.T) { }, } actual := b.ToA().Invoke(Context{}) - assertEqual(t, expected, actual) + assert.Equal(t, expected, actual) } diff --git a/carapace_test.go b/carapace_test.go index 23ac28f20..30db40f06 100644 --- a/carapace_test.go +++ b/carapace_test.go @@ -6,7 +6,7 @@ import ( "strings" "testing" - "github.com/carapace-sh/carapace/internal/assert" + "github.com/carapace-sh/carapace/pkg/assert" "github.com/carapace-sh/carapace/pkg/uid" "github.com/spf13/cobra" ) diff --git a/defaultActions_test.go b/defaultActions_test.go index 7afe311b6..8b78a47a6 100644 --- a/defaultActions_test.go +++ b/defaultActions_test.go @@ -4,6 +4,7 @@ import ( "strings" "testing" + "github.com/carapace-sh/carapace/pkg/assert" "github.com/carapace-sh/carapace/pkg/uid" "github.com/spf13/cobra" ) @@ -30,7 +31,7 @@ func TestActionImport(t *testing.T) { } ] }` - assertEqual(t, ActionValues("positional1", "p1").Tag("first").Invoke(Context{}), ActionImport([]byte(s)).Invoke(Context{})) + assert.Equal(t, ActionValues("positional1", "p1").Tag("first").Invoke(Context{}), ActionImport([]byte(s)).Invoke(Context{})) } func TestActionFlags(t *testing.T) { @@ -40,7 +41,7 @@ func TestActionFlags(t *testing.T) { cmd.Flag("alpha").Changed = true a := actionFlags(cmd).Invoke(Context{Value: "-a"}) - assertEqual( + assert.Equal( t, ActionValuesDescribed( "b", "", diff --git a/diff_test.go b/diff_test.go index 950776630..61cc7fea4 100644 --- a/diff_test.go +++ b/diff_test.go @@ -3,6 +3,7 @@ package carapace import ( "testing" + "github.com/carapace-sh/carapace/pkg/assert" "github.com/carapace-sh/carapace/pkg/style" ) @@ -16,7 +17,7 @@ func TestDiff(t *testing.T) { "added", ) - assertEqual(t, + assert.Equal(t, Diff(original, new).Invoke(NewContext()), ActionStyledValues( "removed", style.Red, diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index df0bd9541..3c70c089b 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -92,7 +92,7 @@ - [pflag](./carapace/standalone/pflag.md) - [Sandbox](./carapace/sandbox.md) - [ClearCache](./carapace/clearCache.md) - - [Env](./carapace/keep.md) + - [Env](./carapace/env.md) - [Files](./carapace/files.md) - [Keep](./carapace/keep.md) - [NewContext](./carapace/newContext.md) diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go index 956096f38..fc27b5598 100644 --- a/example/cmd/root_test.go +++ b/example/cmd/root_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/carapace-sh/carapace" - "github.com/carapace-sh/carapace/internal/assert" + "github.com/carapace-sh/carapace/pkg/assert" "github.com/carapace-sh/carapace/pkg/sandbox" "github.com/carapace-sh/carapace/pkg/style" ) diff --git a/invokedAction.go b/invokedAction.go index b3a31e407..6839c3d1a 100644 --- a/invokedAction.go +++ b/invokedAction.go @@ -1,6 +1,7 @@ package carapace import ( + "encoding/json" "net/url" "strings" @@ -15,6 +16,10 @@ type InvokedAction struct { action Action } +func (ia InvokedAction) MarshalJSON() ([]byte, error) { + return json.Marshal(ia.export()) +} + func (ia InvokedAction) export() export.Export { return export.Export{Meta: ia.action.meta, Values: ia.action.rawValues} } diff --git a/internal/assert/assert.go b/pkg/assert/assert.go similarity index 53% rename from internal/assert/assert.go rename to pkg/assert/assert.go index 654065140..4e2bb444d 100644 --- a/internal/assert/assert.go +++ b/pkg/assert/assert.go @@ -1,47 +1,51 @@ -// Package assert provides test helpers package assert import ( + "encoding/json" "fmt" "path/filepath" "runtime" "strings" + "testing" "github.com/carapace-sh/carapace/third_party/github.com/hexops/gotextdiff" "github.com/carapace-sh/carapace/third_party/github.com/hexops/gotextdiff/myers" "github.com/carapace-sh/carapace/third_party/github.com/hexops/gotextdiff/span" ) -type T interface { - Cleanup(func()) - Error(args ...interface{}) - Errorf(format string, args ...interface{}) - Fail() - FailNow() - Failed() bool - Fatal(args ...interface{}) - Fatalf(format string, args ...interface{}) - Helper() - Log(args ...interface{}) - Logf(format string, args ...interface{}) - Name() string - Setenv(key, value string) - Skip(args ...interface{}) - SkipNow() - Skipf(format string, args ...interface{}) - Skipped() bool - TempDir() string -} +func compare(t *testing.T, expected, actual interface{}, equal bool) { + var sExpected, sActual string + var ok bool + if sExpected, ok = expected.(string); !ok { + m, err := json.MarshalIndent(expected, "", " ") + if err != nil { + t.Error(err.Error()) + } + sExpected = string(m) + } + if sActual, ok = actual.(string); !ok { + m, err := json.MarshalIndent(actual, "", " ") + if err != nil { + t.Error(err.Error()) + } + sActual = string(m) + } -// Equal calls t.Error if given strings are not equal. -func Equal(t T, expected string, actual string) { - if expected != actual { + if sExpected != sActual == equal { _, file, line, _ := runtime.Caller(2) - t.Errorf("%v:%v:\n%v", filepath.Base(file), line, Diff(expected, actual)) + t.Errorf("%v:%v:\n%v", filepath.Base(file), line, diff(sExpected, sActual)) } } -func Diff(expected, actual string) string { +func Equal(t *testing.T, expected, actual interface{}) { + compare(t, expected, actual, true) +} + +func NotEqual(t *testing.T, expected, actual interface{}) { + compare(t, expected, actual, false) +} + +func diff(expected, actual string) string { edits := myers.ComputeEdits(span.URIFromPath(""), expected, actual) diff := fmt.Sprint(gotextdiff.ToUnified("expected", "actual", expected, edits)) diff --git a/pkg/sandbox/sandbox.go b/pkg/sandbox/sandbox.go index 213af9df4..0133feac3 100644 --- a/pkg/sandbox/sandbox.go +++ b/pkg/sandbox/sandbox.go @@ -10,10 +10,10 @@ import ( "testing" "github.com/carapace-sh/carapace" - "github.com/carapace-sh/carapace/internal/assert" "github.com/carapace-sh/carapace/internal/common" "github.com/carapace-sh/carapace/internal/env" "github.com/carapace-sh/carapace/internal/export" + "github.com/carapace-sh/carapace/pkg/assert" "github.com/spf13/cobra" ) diff --git a/pkg/uid/uid_test.go b/pkg/uid/uid_test.go index c92d38769..1ecda3776 100644 --- a/pkg/uid/uid_test.go +++ b/pkg/uid/uid_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/carapace-sh/carapace/internal/assert" + "github.com/carapace-sh/carapace/pkg/assert" "github.com/spf13/cobra" ) diff --git a/storage_test.go b/storage_test.go index 067c17282..ed070f8ad 100644 --- a/storage_test.go +++ b/storage_test.go @@ -3,6 +3,7 @@ package carapace import ( "testing" + "github.com/carapace-sh/carapace/pkg/assert" "github.com/spf13/cobra" ) @@ -16,7 +17,7 @@ func TestGetFlag(t *testing.T) { "flag": ActionValues("a", "b"), }) - assertEqual(t, ActionValues("a", "b").Invoke(Context{}), storage.getFlag(subcmd, "flag").Invoke(Context{})) + assert.Equal(t, ActionValues("a", "b").Invoke(Context{}), storage.getFlag(subcmd, "flag").Invoke(Context{})) } func TestGetPositional(t *testing.T) { @@ -31,9 +32,9 @@ func TestGetPositional(t *testing.T) { ActionValues("pos", "any"), ) - assertEqual(t, ActionValues("pos", "1").Invoke(Context{}), storage.getPositional(cmd, 0).Invoke(Context{})) - assertEqual(t, ActionValues("pos", "2").Invoke(Context{}), storage.getPositional(cmd, 1).Invoke(Context{})) - assertEqual(t, ActionValues("pos", "any").Invoke(Context{}), storage.getPositional(cmd, 2).Invoke(Context{})) + assert.Equal(t, ActionValues("pos", "1").Invoke(Context{}), storage.getPositional(cmd, 0).Invoke(Context{})) + assert.Equal(t, ActionValues("pos", "2").Invoke(Context{}), storage.getPositional(cmd, 1).Invoke(Context{})) + assert.Equal(t, ActionValues("pos", "any").Invoke(Context{}), storage.getPositional(cmd, 2).Invoke(Context{})) } func TestGetDash(t *testing.T) { @@ -50,9 +51,9 @@ func TestGetDash(t *testing.T) { _ = cmd.Flags().Parse([]string{"--", ""}) - assertEqual(t, ActionValues("dash", "1").Invoke(Context{}), storage.getPositional(cmd, 0).Invoke(Context{})) - assertEqual(t, ActionValues("dash", "2").Invoke(Context{}), storage.getPositional(cmd, 1).Invoke(Context{})) - assertEqual(t, ActionValues("dash", "any").Invoke(Context{}), storage.getPositional(cmd, 2).Invoke(Context{})) + assert.Equal(t, ActionValues("dash", "1").Invoke(Context{}), storage.getPositional(cmd, 0).Invoke(Context{})) + assert.Equal(t, ActionValues("dash", "2").Invoke(Context{}), storage.getPositional(cmd, 1).Invoke(Context{})) + assert.Equal(t, ActionValues("dash", "any").Invoke(Context{}), storage.getPositional(cmd, 2).Invoke(Context{})) } func TestCheck(t *testing.T) {