Skip to content

Commit

Permalink
update the test file
Browse files Browse the repository at this point in the history
  • Loading branch information
xwa153 committed Sep 13, 2023
1 parent 73119d9 commit 9f00bb8
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 58 deletions.
2 changes: 1 addition & 1 deletion command/resource/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *cmd) Run(args []string) int {
}
} else {
if len(args) < 2 {
c.UI.Error("Must specify two arguments: resource type and resource name")
c.UI.Error("Your argument format is incorrect: Must specify two arguments: resource type and resource name")
return 1
}
var err error
Expand Down
77 changes: 46 additions & 31 deletions command/resource/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package delete

import (
"errors"
"testing"

"github.com/mitchellh/cli"
Expand All @@ -17,41 +18,56 @@ func TestResourceDeleteInvalidArgs(t *testing.T) {
t.Parallel()

type tc struct {
args []string
expectedCode int
expectedErrMsg string
args []string
expectedCode int
expectedErr error
}

cases := map[string]tc{
"nil args": {
args: nil,
expectedCode: 1,
expectedErrMsg: "Must specify two arguments: resource type and resource name\n",
args: nil,
expectedCode: 1,
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
},
"empty args": {
args: []string{},
expectedCode: 1,
expectedErrMsg: "Must specify two arguments: resource type and resource name\n",
args: []string{},
expectedCode: 1,
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
},
"missing file path": {
args: []string{"-f"},
expectedCode: 1,
expectedErrMsg: "Failed to parse args: flag needs an argument: -f",
args: []string{"-f"},
expectedCode: 1,
expectedErr: errors.New("Failed to parse args: flag needs an argument: -f"),
},
"file not found": {
args: []string{"-f=../testdata/test.hcl"},
expectedCode: 1,
expectedErr: errors.New("Failed to load data: Failed to read file: open ../testdata/test.hcl: no such file or directory"),
},
"provide type and name": {
args: []string{"a.b.c"},
expectedCode: 1,
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
},
"missing resource name": {
args: []string{"a.b.c"},
expectedCode: 1,
expectedErrMsg: "Must specify two arguments: resource type and resource name",
"provide type and name with -f": {
args: []string{"a.b.c", "name", "-f", "test.hcl"},
expectedCode: 1,
expectedErr: errors.New("We ignored the -f flag if you provide gvk and resource name"),
},
"mal-formed group.version.kind": {
args: []string{"a.b", "name"},
expectedCode: 1,
expectedErrMsg: "Must include resource type argument in group.verion.kind format",
"provide type and name with -f and other flags": {
args: []string{"a.b.c", "name", "-f", "test.hcl", "-namespace", "default"},
expectedCode: 1,
expectedErr: errors.New("We ignored the -f flag if you provide gvk and resource name"),
},
"does not provide resource name after type": {
args: []string{"a.b.c", "-namespace", "default"},
expectedCode: 1,
expectedErrMsg: "Must provide resource name right after type",
args: []string{"a.b.c", "-namespace", "default"},
expectedCode: 1,
expectedErr: errors.New("Your argument format is incorrect: Must provide resource name right after type"),
},
"invalid resource type format": {
args: []string{"a.", "name", "-namespace", "default"},
expectedCode: 1,
expectedErr: errors.New("Your argument format is incorrect: Must include resource type argument in group.verion.kind format"),
},
}

Expand All @@ -60,8 +76,10 @@ func TestResourceDeleteInvalidArgs(t *testing.T) {
ui := cli.NewMockUi()
c := New(ui)

require.Equal(t, tc.expectedCode, c.Run(tc.args))
require.Contains(t, ui.ErrorWriter.String(), tc.expectedErrMsg)
code := c.Run(tc.args)

require.Equal(t, tc.expectedCode, code)
require.Contains(t, ui.ErrorWriter.String(), tc.expectedErr.Error())
})
}
}
Expand Down Expand Up @@ -101,28 +119,24 @@ func TestResourceDelete(t *testing.T) {
name string
args []string
expectedCode int
errMsg string
createResource bool
}{
{
name: "delete resource in hcl format",
args: []string{"-f=../testdata/demo.hcl"},
expectedCode: 0,
errMsg: "",
createResource: true,
},
{
name: "delete resource in command line format",
args: []string{"demo.v2.Artist", "korn", "-partition=default", "-namespace=default", "-peer=local"},
expectedCode: 0,
errMsg: "",
createResource: true,
},
{
name: "delete resource that doesn't exist in command line format",
args: []string{"demo.v2.Artist", "fake-korn", "-partition=default", "-namespace=default", "-peer=local"},
args: []string{"demo.v2.Artist", "korn", "-partition=default", "-namespace=default", "-peer=local"},
expectedCode: 0,
errMsg: "",
createResource: false,
},
}
Expand All @@ -136,8 +150,9 @@ func TestResourceDelete(t *testing.T) {
createResource(t, a)
}
code := c.Run(cliArgs)
require.Equal(t, ui.ErrorWriter.String(), tc.errMsg)
require.Empty(t, ui.ErrorWriter.String())
require.Equal(t, tc.expectedCode, code)
require.Contains(t, ui.OutputWriter.String(), "deleted")
})
}
}
2 changes: 1 addition & 1 deletion command/resource/read/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *cmd) Run(args []string) int {
}
} else {
if len(args) < 2 {
c.UI.Error("Must specify two arguments: resource type and resource name")
c.UI.Error("Your argument format is incorrect: Must specify two arguments: resource type and resource name")
return 1
}
var err error
Expand Down
68 changes: 43 additions & 25 deletions command/resource/read/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package read

import (
"errors"
"testing"

"github.com/mitchellh/cli"
Expand All @@ -17,41 +18,56 @@ func TestResourceReadInvalidArgs(t *testing.T) {
t.Parallel()

type tc struct {
args []string
expectedCode int
expectedErrMsg string
args []string
expectedCode int
expectedErr error
}

cases := map[string]tc{
"nil args": {
args: nil,
expectedCode: 1,
expectedErrMsg: "Must specify two arguments: resource type and resource name\n",
args: nil,
expectedCode: 1,
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
},
"empty args": {
args: []string{},
expectedCode: 1,
expectedErrMsg: "Must specify two arguments: resource type and resource name\n",
args: []string{},
expectedCode: 1,
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
},
"missing file path": {
args: []string{"-f"},
expectedCode: 1,
expectedErrMsg: "Failed to parse args: flag needs an argument: -f",
args: []string{"-f"},
expectedCode: 1,
expectedErr: errors.New("Failed to parse args: flag needs an argument: -f"),
},
"file not found": {
args: []string{"-f=../testdata/test.hcl"},
expectedCode: 1,
expectedErr: errors.New("Failed to load data: Failed to read file: open ../testdata/test.hcl: no such file or directory"),
},
"provide type and name": {
args: []string{"a.b.c"},
expectedCode: 1,
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
},
"missing resource name": {
args: []string{"a.b.c"},
expectedCode: 1,
expectedErrMsg: "Must specify two arguments: resource type and resource name",
"provide type and name with -f": {
args: []string{"a.b.c", "name", "-f", "test.hcl"},
expectedCode: 1,
expectedErr: errors.New("We ignored the -f flag if you provide gvk and resource name"),
},
"mal-formed group.version.kind": {
args: []string{"a.b", "name"},
expectedCode: 1,
expectedErrMsg: "Must include resource type argument in group.verion.kind format",
"provide type and name with -f and other flags": {
args: []string{"a.b.c", "name", "-f", "test.hcl", "-namespace", "default"},
expectedCode: 1,
expectedErr: errors.New("We ignored the -f flag if you provide gvk and resource name"),
},
"does not provide resource name after type": {
args: []string{"a.b.c", "-namespace", "default"},
expectedCode: 1,
expectedErrMsg: "Must provide resource name right after type",
args: []string{"a.b.c", "-namespace", "default"},
expectedCode: 1,
expectedErr: errors.New("Your argument format is incorrect: Must provide resource name right after type"),
},
"invalid resource type format": {
args: []string{"a.", "name", "-namespace", "default"},
expectedCode: 1,
expectedErr: errors.New("Your argument format is incorrect: Must include resource type argument in group.verion.kind format"),
},
}

Expand All @@ -60,8 +76,10 @@ func TestResourceReadInvalidArgs(t *testing.T) {
ui := cli.NewMockUi()
c := New(ui)

require.Equal(t, tc.expectedCode, c.Run(tc.args))
require.Contains(t, ui.ErrorWriter.String(), tc.expectedErrMsg)
code := c.Run(tc.args)

require.Equal(t, tc.expectedCode, code)
require.Contains(t, ui.ErrorWriter.String(), tc.expectedErr.Error())
})
}
}
Expand Down

0 comments on commit 9f00bb8

Please sign in to comment.