Skip to content

Commit 233a54b

Browse files
committed
Adding cmd/version_test.go file, handles when we can and can\'t get the version
1 parent faed538 commit 233a54b

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

cmd/version.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ package cmd
2727

2828
import (
2929
"fmt"
30+
"io"
31+
"os"
3032
"strings"
3133

32-
"github.com/deckarep/tips/pkg"
3334
"github.com/deckarep/tips/pkg/tailscale_cli"
3435

36+
"github.com/deckarep/tips/pkg"
37+
3538
"github.com/spf13/cobra"
3639
)
3740

@@ -44,14 +47,20 @@ var versionCmd = &cobra.Command{
4447
Short: pkg.AppShortName + " empowers you to wrangle your Tailnet",
4548
Long: pkg.AppLongName + " empowers you to manage your Tailscale cluster like a pro",
4649
Run: func(cmd *cobra.Command, args []string) {
47-
fmt.Printf("%s (%s) %s\n", pkg.AppLongName, pkg.AppShortName, pkg.AppVersion)
48-
fmt.Println()
49-
50-
cliVersion, err := tailscale_cli.GetVersion()
51-
if err == nil {
52-
fmt.Println("Tailscale CLI")
53-
fmt.Println(strings.Repeat("*", 32))
54-
fmt.Println(cliVersion)
55-
}
50+
printVersion(os.Stdout, tailscale_cli.GetVersion)
5651
},
5752
}
53+
54+
type versionGetter func() (string, error)
55+
56+
func printVersion(w io.Writer, getVersion versionGetter) {
57+
fmt.Fprintf(w, "%s (%s) %s\n", pkg.AppLongName, pkg.AppShortName, pkg.AppVersion)
58+
fmt.Fprintln(w)
59+
60+
cliVersion, err := getVersion()
61+
if err == nil {
62+
fmt.Fprintln(w, "Tailscale CLI")
63+
fmt.Fprintln(w, strings.Repeat("*", 32))
64+
fmt.Fprintln(w, cliVersion)
65+
}
66+
}

cmd/version_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestPrintVersion(t *testing.T) {
12+
var b bytes.Buffer
13+
14+
// When we can get the version.
15+
var getVersion = func() (string, error) {
16+
return "whatever 2.0.1", nil
17+
}
18+
19+
printVersion(&b, getVersion)
20+
21+
assert.Equal(t, b.String(),
22+
"Tailscale IPs (tips) 0.0.1\n\nTailscale CLI\n********************************\nwhatever 2.0.1\n")
23+
24+
b.Reset()
25+
26+
// When we can't get the version.
27+
getVersion = func() (string, error) {
28+
return "", errors.New("couldn't get version cause some crazy reason!")
29+
}
30+
31+
printVersion(&b, getVersion)
32+
33+
assert.Equal(t, b.String(),
34+
"Tailscale IPs (tips) 0.0.1\n\n")
35+
}

0 commit comments

Comments
 (0)