Skip to content

Commit

Permalink
test: add tests for --quiet flag
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Jan 4, 2024
1 parent 57ab6a4 commit 6c84163
Show file tree
Hide file tree
Showing 5 changed files with 630 additions and 0 deletions.
155 changes: 155 additions & 0 deletions internal/cmd/base/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package base_test

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"

"github.com/hetznercloud/cli/internal/cli"
"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/testutil"
)

type fakeResource struct {
ID int `json:"id"`
Name string `json:"name"`
}

var commandCalled bool

var fakeCreateCmd = base.CreateCmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
return &cobra.Command{
Use: "create",
}
},
Run: func(s state.State, cmd *cobra.Command, strings []string) (any, any, error) {
cmd.Println("Creating fake resource")
commandCalled = true

resource := &fakeResource{
ID: 123,
Name: "test",
}

return resource, util.Wrap("resource", resource), nil
},
}

func TestCreate(t *testing.T) {
commandCalled = false

fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := cli.NewRootCommand(fx.State())
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(fx.State()))

out, errOut, err := fx.Run(cmd, []string{"create"})

assert.Equal(t, true, commandCalled)
assert.NoError(t, err)
assert.Equal(t, "Creating fake resource\n", out)
assert.Empty(t, errOut)
}

func TestCreateJSON(t *testing.T) {
commandCalled = false

fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := cli.NewRootCommand(fx.State())
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(fx.State()))

out, errOut, err := fx.Run(cmd, []string{"create", "-o=json"})

assert.Equal(t, true, commandCalled)
assert.NoError(t, err)
assert.JSONEq(t, `{"resource": {"id": 123, "name": "test"}}`, out)
assert.Equal(t, "Creating fake resource\n", errOut)
}

func TestCreateYAML(t *testing.T) {
commandCalled = false

fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := cli.NewRootCommand(fx.State())
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(fx.State()))

out, errOut, err := fx.Run(cmd, []string{"create", "-o=yaml"})

assert.Equal(t, true, commandCalled)
assert.NoError(t, err)
assert.YAMLEq(t, `{"resource": {"id": 123, "name": "test"}}`, out)
assert.Equal(t, "Creating fake resource\n", errOut)
}

func TestCreateQuiet(t *testing.T) {
commandCalled = false

fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := cli.NewRootCommand(fx.State())
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(fx.State()))

out, errOut, err := fx.Run(cmd, []string{"create", "--quiet"})

assert.Equal(t, true, commandCalled)
assert.NoError(t, err)
assert.Empty(t, out)
assert.Empty(t, errOut)
}

func TestCreateJSONQuiet(t *testing.T) {
commandCalled = false

fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := cli.NewRootCommand(fx.State())
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(fx.State()))

out, errOut, err := fx.Run(cmd, []string{"create", "-o=json", "--quiet"})

assert.Equal(t, true, commandCalled)
assert.NoError(t, err)
assert.JSONEq(t, `{"resource": {"id": 123, "name": "test"}}`, out)
assert.Empty(t, errOut)
}

func TestCreateYAMLQuiet(t *testing.T) {
commandCalled = false

fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := cli.NewRootCommand(fx.State())
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(fx.State()))

out, errOut, err := fx.Run(cmd, []string{"create", "-o=yaml", "--quiet"})

assert.Equal(t, true, commandCalled)
assert.NoError(t, err)
assert.YAMLEq(t, `{"resource": {"id": 123, "name": "test"}}`, out)
assert.Empty(t, errOut)
}
80 changes: 80 additions & 0 deletions internal/cmd/base/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package base_test

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"

"github.com/hetznercloud/cli/internal/cli"
"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/testutil"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var fakeDeleteCmd = base.DeleteCmd{
ResourceNameSingular: "Fake resource",
Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error {
cmd.Println("Deleting fake resource")
commandCalled = true
return nil
},

Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
cmd.Println("Fetching fake resource")

resource := &fakeResource{
ID: 123,
Name: "test",
}

return resource, nil, nil
},

NameSuggestions: func(client hcapi2.Client) func() []string {
return nil
},
}

func TestDelete(t *testing.T) {
commandCalled = false

fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := cli.NewRootCommand(fx.State())
fx.ExpectEnsureToken()

cmd.AddCommand(fakeDeleteCmd.CobraCommand(fx.State()))

out, errOut, err := fx.Run(cmd, []string{"delete", "123"})

assert.Equal(t, true, commandCalled)
assert.NoError(t, err)
assert.Equal(t, `Fetching fake resource
Deleting fake resource
Fake resource 123 deleted
`, out)
assert.Empty(t, errOut)
}

func TestDeleteQuiet(t *testing.T) {
commandCalled = false

fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := cli.NewRootCommand(fx.State())
fx.ExpectEnsureToken()

cmd.AddCommand(fakeDeleteCmd.CobraCommand(fx.State()))

out, errOut, err := fx.Run(cmd, []string{"delete", "123", "--quiet"})

assert.Equal(t, true, commandCalled)
assert.NoError(t, err)
assert.Empty(t, out)
assert.Empty(t, errOut)
}
8 changes: 8 additions & 0 deletions internal/cmd/base/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package base

import (
"fmt"
"os"
"reflect"
"strings"

Expand Down Expand Up @@ -54,6 +55,13 @@ func (dc *DescribeCmd) CobraCommand(s state.State) *cobra.Command {
func (dc *DescribeCmd) Run(s state.State, cmd *cobra.Command, args []string) error {
outputFlags := output.FlagsForCommand(cmd)

quiet, _ := cmd.Flags().GetBool("quiet")

isSchema := outputFlags.IsSet("json") || outputFlags.IsSet("yaml")
if isSchema && !quiet {
cmd.SetOut(os.Stderr)
}

idOrName := args[0]
resource, schema, err := dc.Fetch(s, cmd, idOrName)
if err != nil {
Expand Down
Loading

0 comments on commit 6c84163

Please sign in to comment.