Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 17 additions & 44 deletions action_test.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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"),
Expand All @@ -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{}),
)
Expand Down
9 changes: 5 additions & 4 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -57,5 +58,5 @@ func TestBatchToA(t *testing.T) {
},
}
actual := b.ToA().Invoke(Context{})
assertEqual(t, expected, actual)
assert.Equal(t, expected, actual)
}
2 changes: 1 addition & 1 deletion carapace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
5 changes: 3 additions & 2 deletions defaultActions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand All @@ -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", "",
Expand Down
3 changes: 2 additions & 1 deletion diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package carapace
import (
"testing"

"github.com/carapace-sh/carapace/pkg/assert"
"github.com/carapace-sh/carapace/pkg/style"
)

Expand All @@ -16,7 +17,7 @@ func TestDiff(t *testing.T) {
"added",
)

assertEqual(t,
assert.Equal(t,
Diff(original, new).Invoke(NewContext()),
ActionStyledValues(
"removed", style.Red,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
5 changes: 5 additions & 0 deletions invokedAction.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package carapace

import (
"encoding/json"
"net/url"
"strings"

Expand All @@ -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}
}
Expand Down
56 changes: 30 additions & 26 deletions internal/assert/assert.go → pkg/assert/assert.go
Original file line number Diff line number Diff line change
@@ -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))

Expand Down
2 changes: 1 addition & 1 deletion pkg/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
Loading