Skip to content

Commit

Permalink
test(version): add a test function to test the result of the version …
Browse files Browse the repository at this point in the history
…command
  • Loading branch information
danvergara committed Apr 2, 2021
1 parent 1392c41 commit e3b41c3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
31 changes: 19 additions & 12 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package cmd

/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Expand All @@ -13,28 +15,33 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "The version of the project",
Long: `The current version of the project.
This projects follows the semantic versioning standard.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("v0.1.0")
},
// NewVersionCmd return a versionCmd instance
func NewVersionCmd() *cobra.Command {

// versionCmd represents the version command
versionCmd := &cobra.Command{
Use: "version",
Short: "The version of the project",
Long: `The current version of the project.
This projects follows the semantic versioning standard.`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Fprintln(cmd.OutOrStdout(), "v0.1.0")
return nil
},
}

return versionCmd
}

func init() {
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(NewVersionCmd())

// Here you will define your flags and configuration settings.

Expand Down
23 changes: 23 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"bytes"
"io/ioutil"
"strings"
"testing"
)

func TestVersionCmd(t *testing.T) {
cmd := NewVersionCmd()
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.Execute()
out, err := ioutil.ReadAll(b)
if err != nil {
t.Fatal(err)
}

if !strings.Contains(string(out), "v0.1.0") {
t.Fatalf("expected \"%s\" got \"%s\"", "v0.1.0", string(out))
}
}

0 comments on commit e3b41c3

Please sign in to comment.