Skip to content

Commit

Permalink
add initial unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthpun committed Jan 10, 2024
1 parent fd943af commit 0cd19d2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/command/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package command_test

import (
"testing"

"github.com/onflow/flow-cli/internal/command"

Check failure on line 6 in internal/command/command_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/onflow/flow-cli (goimports)
"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")
}
}
9 changes: 9 additions & 0 deletions internal/command/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -117,6 +125,7 @@ func InitFlags(cmd *cobra.Command) {
"Approve any prompts",
)


Check failure on line 128 in internal/command/flags.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/onflow/flow-cli (goimports)
cmd.PersistentFlags().BoolVarP(
&Flags.SkipVersionCheck,
"skip-version-check",
Expand Down
41 changes: 41 additions & 0 deletions internal/command/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package command_test

import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/onflow/flow-cli/internal/command"

Check failure on line 9 in internal/command/flags_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/onflow/flow-cli (goimports)
"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)
}
}
}

0 comments on commit 0cd19d2

Please sign in to comment.