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 3, 2024
1 parent 421ae3a commit 616c5d2
Show file tree
Hide file tree
Showing 5 changed files with 822 additions and 0 deletions.
216 changes: 216 additions & 0 deletions internal/cmd/base/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package base_test

import (
"context"
"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(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, 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()

ctx := context.Background()
cmd := cli.NewRootCommand(
&state.State{},
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter))

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()

ctx := context.Background()
cmd := cli.NewRootCommand(
&state.State{},
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter))

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()

ctx := context.Background()
cmd := cli.NewRootCommand(
&state.State{},
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter))

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()

ctx := context.Background()
cmd := cli.NewRootCommand(
&state.State{},
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter))

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()

ctx := context.Background()
cmd := cli.NewRootCommand(
&state.State{},
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter))

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()

ctx := context.Background()
cmd := cli.NewRootCommand(
&state.State{},
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

cmd.AddCommand(fakeCreateCmd.CobraCommand(
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter))

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)
}
101 changes: 101 additions & 0 deletions internal/cmd/base/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package base_test

import (
"context"
"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(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, resource interface{}) error {
cmd.Println("Deleting fake resource")
commandCalled = true
return nil
},

Fetch: func(ctx context.Context, client hcapi2.Client, 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()

ctx := context.Background()
cmd := cli.NewRootCommand(
&state.State{},
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

cmd.AddCommand(fakeDeleteCmd.CobraCommand(
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter))

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()

ctx := context.Background()
cmd := cli.NewRootCommand(
&state.State{},
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter)
fx.ExpectEnsureToken()

cmd.AddCommand(fakeDeleteCmd.CobraCommand(
ctx,
fx.Client,
fx.TokenEnsurer,
fx.ActionWaiter))

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 @@ -3,6 +3,7 @@ package base
import (
"context"
"fmt"
"os"
"reflect"
"strings"

Expand Down Expand Up @@ -57,6 +58,13 @@ func (dc *DescribeCmd) CobraCommand(
func (dc *DescribeCmd) Run(ctx context.Context, client hcapi2.Client, 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(ctx, client, cmd, idOrName)
if err != nil {
Expand Down
Loading

0 comments on commit 616c5d2

Please sign in to comment.