Skip to content

Commit

Permalink
issue #104: add version command
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Nov 11, 2018
1 parent 0c2ffff commit 2579866
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
9 changes: 0 additions & 9 deletions cmd/retry/build.go

This file was deleted.

13 changes: 9 additions & 4 deletions cmd/retry/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@ import (
"bytes"
"testing"

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

func TestCompletion(t *testing.T) {
before := Completion.OutOrStdout()
defer Completion.SetOutput(before)

buf := bytes.NewBuffer(nil)
cmd := &cobra.Command{Use: "test"}
cmd.AddCommand(Completion)
cmd.SetOutput(buf)

tests := []struct {
name string
format string
expected string
}{
{"Bash", "bash", "# bash completion for completion"},
{"Zsh", "zsh", "#compdef completion"},
{"Bash", "bash", "# bash completion for test"},
{"Zsh", "zsh", "#compdef test"},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
Completion.SetOutput(buf)
buf.Reset()
Completion.Flag("format").Value.Set(tc.format)
assert.NoError(t, Completion.RunE(Completion, nil))
assert.Contains(t, buf.String(), tc.expected)
Expand Down
27 changes: 27 additions & 0 deletions cmd/retry/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build go1.10

package main

import (
"runtime"

"github.com/spf13/cobra"
)

var (
commit = "none"
date = "unknown"
version = "dev"
)

// Version shows application version.
var Version = &cobra.Command{
Use: "version",
Short: "Show application version",
Run: func(cmd *cobra.Command, args []string) {
cmd.Printf(
"Version %s (commit: %s, build date: %s, go version: %s, compiler: %s, platform: %s/%s)\n",
version, commit, date, runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH)
},
Version: version,
}
45 changes: 45 additions & 0 deletions cmd/retry/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// +build go1.10

package main

import (
"bytes"
"testing"

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

func TestVersion(t *testing.T) {
type memo struct {
commit string
date string
version string
}

buf := bytes.NewBuffer(nil)
cmd := &cobra.Command{Use: "test"}
cmd.AddCommand(Version)
cmd.SetOutput(buf)

tests := []struct {
name string
memo
expected string
}{
{"Version 4.0", memo{commit: "0c2ffff", date: "November 30, 2018", version: "4.0.0"},
"Version 4.0.0 (commit: 0c2ffff, build date: November 30, 2018"},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
before := memo{commit: commit, date: date, version: version}
defer func() { commit, date, version = before.commit, before.date, before.version }()
commit, date, version = tc.commit, tc.date, tc.version

buf.Reset()
Version.Run(Version, nil)
assert.Contains(t, buf.String(), tc.expected)
})
}
}

0 comments on commit 2579866

Please sign in to comment.