diff --git a/internal/command/command_test.go b/internal/command/command_test.go new file mode 100644 index 000000000..e66e4484f --- /dev/null +++ b/internal/command/command_test.go @@ -0,0 +1,21 @@ +package command_test + +import ( + "testing" + + "github.com/onflow/flow-cli/internal/command" + "github.com/spf13/cobra" +) + +func TestAddToParent(t *testing.T) { + cmd := &cobra.Command{} + c := &command.Command{ + Cmd: cmd, + } + + c.AddToParent(cmd) + + if c.Cmd.Run == nil { + t.Errorf("Run function was not initialized") + } +} \ No newline at end of file diff --git a/internal/command/flags.go b/internal/command/flags.go index c29be414f..9ec63651d 100644 --- a/internal/command/flags.go +++ b/internal/command/flags.go @@ -53,6 +53,14 @@ func InitFlags(cmd *cobra.Command) { "Filter result values by property name", ) + cmd.PersistentFlags().StringVarP( + &Flags.Format, + "format", + "", + Flags.Format, + "Format result values", + ) + cmd.PersistentFlags().StringVarP( &Flags.Host, "host", @@ -117,6 +125,7 @@ func InitFlags(cmd *cobra.Command) { "Approve any prompts", ) + cmd.PersistentFlags().BoolVarP( &Flags.SkipVersionCheck, "skip-version-check", diff --git a/internal/command/flags_test.go b/internal/command/flags_test.go new file mode 100644 index 000000000..1eb52c2db --- /dev/null +++ b/internal/command/flags_test.go @@ -0,0 +1,41 @@ +package command_test + +import ( + "fmt" + "strconv" + "strings" + "testing" + + "github.com/onflow/flow-cli/internal/command" + "github.com/spf13/cobra" +) + +func TestInitFlags(t *testing.T) { + cmd := &cobra.Command{} + command.InitFlags(cmd) + + flags := []struct { + name string + expected string + }{ + {"filter", command.Flags.Filter}, + {"format", command.Flags.Format}, + {"save", command.Flags.Save}, + {"host", command.Flags.Host}, + {"network-key", command.Flags.HostNetworkKey}, + {"network", command.Flags.Network}, + {"log", command.Flags.Log}, + {"yes", strconv.FormatBool(command.Flags.Yes)}, + {"config-path", fmt.Sprintf("[%s]",strings.Join(command.Flags.ConfigPaths, ","))}, + {"skip-version-check", strconv.FormatBool(command.Flags.SkipVersionCheck)}, + } + + for _, flag := range flags { + f := cmd.PersistentFlags().Lookup(flag.name) + if f == nil { + t.Errorf("Flag %s was not initialized", flag.name) + } else if f.DefValue != flag.expected { + t.Errorf("Flag %s was not initialized with correct default value. Value: %s, Expected: %s", flag.name, f.Value.String(), flag.expected) + } + } +} \ No newline at end of file