Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khareyash05 committed Apr 23, 2023
1 parent a307414 commit c708860
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
3 changes: 1 addition & 2 deletions pkg/cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func (c CLI) newVersionCmd() *cobra.Command {
Long: fmt.Sprintf("Print the %s version", c.commandName),
Example: fmt.Sprintf("%s version", c.commandName),
RunE: func(_ *cobra.Command, _ []string) error {
fmt.Println(c.version)
return nil
return fmt.Errorf("%s", c.version)
},
}
}
43 changes: 13 additions & 30 deletions pkg/cli/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,26 @@ limitations under the License.
package cli

import (
"bytes"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
)

var _ = Describe("newVersionCmd", func() {
var (
command *cobra.Command
cli CLI
out *bytes.Buffer
)

BeforeEach(func() {
out = bytes.NewBufferString("")
cli = CLI{
commandName: "mycli",
version: "1.0.0",
}
command = cli.newVersionCmd()
command.SetOut(out)
})
func TestVersionCLI(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "CLI Version Suite")
}

It("should set the correct Use, Short, and Long values", func() {
Expect(command.Use).To(Equal("version"))
Expect(command.Short).To(Equal("Print the mycli version"))
Expect(command.Long).To(Equal("Print the mycli version"))
})
var _ = Describe("newVersionCmd", func() {
It("should print the version number", func() {
cli := CLI{version: "1.0.0", commandName: "myapp"}
cmd := cli.newVersionCmd()

It("should set the correct Example value", func() {
Expect(command.Example).To(Equal("mycli version"))
})
// execute the command
err := cmd.Execute()

It("should print the version when executed", func() {
err := command.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(out.String()).To(Equal("1.0.0\n"))
// verify the output
Expect(err.Error()).To(Equal("1.0.0"))
})
})

0 comments on commit c708860

Please sign in to comment.