Repository: rsteube/carapace
Pull request #702 (2023-01-22 16:43:17)

updated modifier tests


Patch:
diff --git a/example/cmd/modifier.go b/example/cmd/modifier.go
index 63fb9ae..03ac0e5 100644
--- a/example/cmd/modifier.go
+++ b/example/cmd/modifier.go
@@ -23,9 +23,18 @@ func init() {
 
 	carapace.Gen(modifierCmd).FlagCompletion(carapace.ActionMap{
 		"batch": carapace.Batch(
-			carapace.ActionValues("A", "B"),
-			carapace.ActionValues("C", "D"),
-			carapace.ActionValues("E", "F"),
+			carapace.ActionValuesDescribed(
+				"A", "description of A",
+				"B", "description of first B",
+			),
+			carapace.ActionValuesDescribed(
+				"B", "description of second B",
+				"C", "description of first C",
+			),
+			carapace.ActionValuesDescribed(
+				"C", "description of second C",
+				"D", "description of D",
+			),
 		).ToA(),
 		"timeout": carapace.ActionMultiParts(":", func(c carapace.Context) carapace.Action {
 			switch len(c.Parts) {
diff --git a/pkg/sandbox/sandbox.go b/pkg/sandbox/sandbox.go
index e9798f2..5c695c1 100644
--- a/pkg/sandbox/sandbox.go
+++ b/pkg/sandbox/sandbox.go
@@ -169,7 +169,7 @@ func (r run) Expect(expected carapace.Action) {
 	})
 }
 
-// Command executes the command generated by given function for sandbox tests.
+// Command executes the command generated by given function.
 func Command(t *testing.T, cmdF func() *cobra.Command) (f func(func(s *Sandbox))) {
 	return func(f func(s *Sandbox)) {
 		s := newSandbox(t, cmdF)
@@ -178,7 +178,7 @@ func Command(t *testing.T, cmdF func() *cobra.Command) (f func(func(s *Sandbox))
 	}
 }
 
-// Run invokes `go run` on given package for sandbox tests.
+// Run invokes `go run` on given package.
 func Package(t *testing.T, pkg string) (f func(func(s *Sandbox))) {
 	return Command(t, func() *cobra.Command {
 		cmd := &cobra.Command{DisableFlagParsing: true}
@@ -202,3 +202,14 @@ func Package(t *testing.T, pkg string) (f func(func(s *Sandbox))) {
 		return cmd
 	})
 }
+
+// Action executes a a command with the action return by given function as first positional argument.
+func Action(t *testing.T, actionF func() carapace.Action) (f func(func(s *Sandbox))) {
+	return Command(t, func() *cobra.Command {
+		cmd := &cobra.Command{DisableFlagParsing: true}
+		cmd.CompletionOptions.DisableDefaultCmd = true
+
+		carapace.Gen(cmd).PositionalCompletion(actionF())
+		return cmd
+	})
+}


Test patch:
diff --git a/example/cmd/modifier_test.go b/example/cmd/modifier_test.go
index 1621d47..bbde9d8 100644
--- a/example/cmd/modifier_test.go
+++ b/example/cmd/modifier_test.go
@@ -5,9 +5,23 @@ import (
 
 	"github.com/rsteube/carapace"
 	"github.com/rsteube/carapace/pkg/sandbox"
+	"github.com/rsteube/carapace/pkg/style"
 )
 
-func TestModifier(t *testing.T) {
+func TestBatch(t *testing.T) {
+	sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
+		s.Run("modifier", "--batch", "").
+			Expect(carapace.ActionValuesDescribed(
+				"A", "description of A",
+				"B", "description of second B",
+				"C", "description of second C",
+				"D", "description of D",
+			).
+				Usage("Batch()"))
+	})
+}
+
+func TestTimeout(t *testing.T) {
 	sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
 		s.Run("modifier", "--timeout", "1s:").
 			Expect(carapace.ActionValues("within timeout").
@@ -20,3 +34,27 @@ func TestModifier(t *testing.T) {
 				Usage("Timeout()"))
 	})
 }
+
+func TestUsage(t *testing.T) {
+	sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
+		s.Run("modifier", "--usage", "").
+			Expect(carapace.ActionValues().
+				Usage("explicit flag usage"))
+	})
+}
+
+func TestChdir(t *testing.T) {
+	sandbox.Action(t, func() carapace.Action {
+		return carapace.ActionFiles().Chdir("subdir")
+	})(func(s *sandbox.Sandbox) {
+		s.Files("subdir/file1.txt", "")
+
+		s.Run("").Expect(
+			carapace.ActionValues("file1.txt").
+				StyleF(func(s string, sc style.Context) string {
+					return style.ForPath("subdir/file1.txt", sc)
+				}).
+				NoSpace('/').
+				Tag("files"))
+	})
+}