Skip to content

Commit fd2a68c

Browse files
committed
increases coverage; indicates ShellCompDirectiveNoFileComp
Signed-off-by: thediveo <[email protected]>
1 parent 48048be commit fd2a68c

File tree

6 files changed

+27
-9
lines changed

6 files changed

+27
-9
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![GitHub](https://img.shields.io/github/license/thediveo/enumflag)](https://img.shields.io/github/license/thediveo/enumflag)
44
![build and test](https://github.com/thediveo/enumflag/workflows/build%20and%20test/badge.svg?branch=master)
55
[![Go Report Card](https://goreportcard.com/badge/github.com/thediveo/enumflag/v2)](https://goreportcard.com/report/github.com/thediveo/enumflag/v2)
6-
![Coverage](https://img.shields.io/badge/Coverage-97.3%25-brightgreen)
6+
![Coverage](https://img.shields.io/badge/Coverage-100.0%25-brightgreen)
77

88
`enumflag/v2` is a Golang package which supplements the Golang CLI flag packages
99
[spf13/cobra](https://github.com/spf13/cobra) and

Diff for: flag_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package enumflag
1717
import (
1818
. "github.com/onsi/ginkgo/v2"
1919
. "github.com/onsi/gomega"
20+
"github.com/spf13/cobra"
2021
)
2122

2223
var _ = Describe("flag", func() {
@@ -81,4 +82,16 @@ var _ = Describe("flag", func() {
8182

8283
})
8384

85+
It("returns completors", func() {
86+
cmd := &cobra.Command{}
87+
foomodes := []FooModeTest{fmBar, fmFoo}
88+
val := NewSlice(&foomodes, "modes", FooModeIdentifiersTest, EnumCaseInsensitive)
89+
cmd.PersistentFlags().Var(val, "mode", "blahblah")
90+
Expect(val.RegisterCompletion(cmd, "mode", Help[FooModeTest]{
91+
fmFoo: "gives a foo",
92+
fmBar: "gives a bar",
93+
fmBaz: "gives a baz",
94+
})).To(Succeed())
95+
})
96+
8497
})

Diff for: test/enumflag-testing/main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package main
1616

1717
import (
18+
"io"
1819
"os"
1920

2021
"github.com/spf13/cobra"
@@ -39,11 +40,12 @@ var FooModeNames = map[FooMode][]string{
3940
Baz: {"baz"},
4041
}
4142

42-
func newRootCmd() *cobra.Command {
43+
func newRootCmd(w io.Writer) *cobra.Command {
4344
rootCmd := &cobra.Command{
4445
Use: Name,
4546
Run: func(*cobra.Command, []string) {},
4647
}
48+
rootCmd.SetOutput(w)
4749

4850
testCmd := &cobra.Command{
4951
Use: "test the canary",
@@ -68,9 +70,10 @@ func main() {
6870
// Cobra automatically adds a "__complete" command to our root command
6971
// behind the scenes, unless we specify one explicitly. It also adds a
7072
// "complete" sub command if we're adding at least one sub command.
71-
if err := newRootCmd().Execute(); err != nil {
73+
if err := newRootCmd(stdout).Execute(); err != nil {
7274
osExit(1)
7375
}
7476
}
7577

7678
var osExit = os.Exit
79+
var stdout io.Writer = os.Stdout

Diff for: test/enumflag-testing/main_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"bytes"
19+
"io"
1920
"os"
2021

2122
"github.com/spf13/cobra"
@@ -30,9 +31,8 @@ var _ = Describe("enumflag-testing canary", func() {
3031
var out *bytes.Buffer
3132

3233
BeforeEach(func() {
33-
rootCmd = newRootCmd()
3434
out = &bytes.Buffer{}
35-
rootCmd.SetOutput(out)
35+
rootCmd = newRootCmd(out)
3636
})
3737

3838
It("has a hidden __complete command", func() {
@@ -55,12 +55,14 @@ var _ = Describe("enumflag-testing canary", func() {
5555

5656
It("reaches 100% :p", func() {
5757
exitCode := -1
58-
defer func(old func(int), oldargs []string) {
58+
defer func(old func(int), oldargs []string, out io.Writer) {
5959
osExit = old
6060
os.Args = oldargs
61-
}(osExit, os.Args)
61+
stdout = out
62+
}(osExit, os.Args, os.Stdout)
6263
osExit = func(code int) { exitCode = code }
6364
os.Args = []string{os.Args[0], "froobz"}
65+
stdout = &bytes.Buffer{}
6466
main()
6567
Expect(exitCode).To(Equal(1))
6668
})

Diff for: value_scalar.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ func (s *enumScalar[E]) NewCompletor(enums EnumIdentifiers[E], help Help[E]) Com
8484
}
8585
}
8686
return func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
87-
return completions, cobra.ShellCompDirectiveDefault
87+
return completions, cobra.ShellCompDirectiveNoFileComp
8888
}
8989
}

Diff for: value_slice.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ func (s *enumSlice[E]) NewCompletor(enums EnumIdentifiers[E], help Help[E]) Comp
110110
}
111111
filteredCompletions = append(filteredCompletions, prefix+completion)
112112
}
113-
return filteredCompletions, cobra.ShellCompDirectiveDefault
113+
return filteredCompletions, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
114114
}
115115
}

0 commit comments

Comments
 (0)