From 0cd19d24d8f942629568a4e9cd73ab653bc3bb79 Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 10 Jan 2024 10:14:56 -0800 Subject: [PATCH 1/4] add initial unit tests --- internal/command/command_test.go | 21 ++++++++++++++++ internal/command/flags.go | 9 +++++++ internal/command/flags_test.go | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 internal/command/command_test.go create mode 100644 internal/command/flags_test.go 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 From 82c787621223c64ef1f306b165e07a93e152f8c7 Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 17 Jan 2024 09:28:58 -0800 Subject: [PATCH 2/4] update imports --- internal/command/command_test.go | 21 ++++++------ internal/command/flags.go | 1 - internal/command/flags_test.go | 55 ++++++++++++++++---------------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/internal/command/command_test.go b/internal/command/command_test.go index e66e4484f..b79a29491 100644 --- a/internal/command/command_test.go +++ b/internal/command/command_test.go @@ -3,19 +3,20 @@ package command_test import ( "testing" - "github.com/onflow/flow-cli/internal/command" "github.com/spf13/cobra" + + "github.com/onflow/flow-cli/internal/command" ) func TestAddToParent(t *testing.T) { - cmd := &cobra.Command{} - c := &command.Command{ - Cmd: cmd, - } + cmd := &cobra.Command{} + c := &command.Command{ + Cmd: cmd, + } - c.AddToParent(cmd) + c.AddToParent(cmd) - if c.Cmd.Run == nil { - t.Errorf("Run function was not initialized") - } -} \ No newline at end of file + if c.Cmd.Run == nil { + t.Errorf("Run function was not initialized") + } +} diff --git a/internal/command/flags.go b/internal/command/flags.go index 9ec63651d..33ecc0a9f 100644 --- a/internal/command/flags.go +++ b/internal/command/flags.go @@ -125,7 +125,6 @@ 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 index 1eb52c2db..ae8a645a3 100644 --- a/internal/command/flags_test.go +++ b/internal/command/flags_test.go @@ -6,36 +6,37 @@ import ( "strings" "testing" - "github.com/onflow/flow-cli/internal/command" "github.com/spf13/cobra" + + "github.com/onflow/flow-cli/internal/command" ) func TestInitFlags(t *testing.T) { - cmd := &cobra.Command{} - command.InitFlags(cmd) + 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)}, - } + 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 + 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) + } + } +} From 06b75a1ecadb6a7a51303f1cb9f7f0d281808afd Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 17 Jan 2024 11:30:52 -0800 Subject: [PATCH 3/4] update --- internal/command/command_test.go | 22 ------------------- .../command/{flags.go => global_flags.go} | 0 .../{flags_test.go => global_flags_test.go} | 0 3 files changed, 22 deletions(-) delete mode 100644 internal/command/command_test.go rename internal/command/{flags.go => global_flags.go} (100%) rename internal/command/{flags_test.go => global_flags_test.go} (100%) diff --git a/internal/command/command_test.go b/internal/command/command_test.go deleted file mode 100644 index b79a29491..000000000 --- a/internal/command/command_test.go +++ /dev/null @@ -1,22 +0,0 @@ -package command_test - -import ( - "testing" - - "github.com/spf13/cobra" - - "github.com/onflow/flow-cli/internal/command" -) - -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") - } -} diff --git a/internal/command/flags.go b/internal/command/global_flags.go similarity index 100% rename from internal/command/flags.go rename to internal/command/global_flags.go diff --git a/internal/command/flags_test.go b/internal/command/global_flags_test.go similarity index 100% rename from internal/command/flags_test.go rename to internal/command/global_flags_test.go From 1c2f2a2418004f6fb4b3ea97a17acc33a4dd395f Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 17 Jan 2024 12:14:32 -0800 Subject: [PATCH 4/4] license --- internal/command/global_flags_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/command/global_flags_test.go b/internal/command/global_flags_test.go index ae8a645a3..f57c568eb 100644 --- a/internal/command/global_flags_test.go +++ b/internal/command/global_flags_test.go @@ -1,3 +1,21 @@ +/* + * Flow CLI + * + * Copyright 2019 Dapper Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package command_test import (