Skip to content

Commit 12ac7ce

Browse files
committed
Fix gtm verify
1 parent 47984f8 commit 12ac7ce

3 files changed

Lines changed: 42 additions & 12 deletions

File tree

command/verify.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
package command
66

77
import (
8+
"bytes"
89
"flag"
910
"fmt"
11+
"io"
12+
"os"
1013
"strings"
1114

1215
"github.com/hashicorp/go-version"
@@ -15,15 +18,9 @@ import (
1518

1619
// VerifyCmd contains CLI commands for verify
1720
type VerifyCmd struct {
18-
Ui cli.Ui
19-
Version string
20-
}
21-
22-
// NewVerify returns new VerifyCmd struct with version set
23-
func NewVerify(v string) func() (cli.Command, error) {
24-
return func() (cli.Command, error) {
25-
return VerifyCmd{Version: v}, nil
26-
}
21+
Ui cli.Ui
22+
Version string
23+
ResultWriter *bytes.Buffer
2724
}
2825

2926
// Help returns CLI help for Verify command
@@ -54,7 +51,16 @@ func (c VerifyCmd) Run(args []string) int {
5451
c.Ui.Error(err.Error())
5552
return 1
5653
}
57-
c.Ui.Output(fmt.Sprintf("%t", valid))
54+
55+
// c.Ui.Output adds a newline which is a bad for us here.
56+
// Clients calling this method are comparing equal to "true" (without newline)
57+
// ResultWriter allows for mocking during testing and defaults to Stdout
58+
var writer io.Writer
59+
writer = os.Stdout
60+
if c.ResultWriter != nil {
61+
writer = c.ResultWriter
62+
}
63+
fmt.Fprintf(writer, "%t", valid)
5864
return 0
5965
}
6066

command/verify_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
package command
66

7-
import "testing"
7+
import (
8+
"bytes"
9+
"testing"
10+
11+
"github.com/mitchellh/cli"
12+
)
813

914
func TestCheck(t *testing.T) {
1015
cases := []struct {
@@ -33,4 +38,22 @@ func TestCheck(t *testing.T) {
3338
tc.input, tc.cmd.Version, tc.valid, valid)
3439
}
3540
}
41+
42+
}
43+
44+
func TestVerify(t *testing.T) {
45+
ui := new(cli.MockUi)
46+
c := VerifyCmd{Ui: ui, Version: "1.0.0", ResultWriter: new(bytes.Buffer)}
47+
48+
args := []string{">= 1.0.0"}
49+
rc := c.Run(args)
50+
51+
if rc != 0 {
52+
t.Errorf("gtm verify(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
53+
}
54+
55+
want := "true"
56+
if want != c.ResultWriter.String() {
57+
t.Errorf("gtm verify(%+v), want '%s' got '%s', %s", args, want, ui.OutputWriter.String(), ui.ErrorWriter.String())
58+
}
3659
}

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ func main() {
4646
},
4747
"verify": func() (cli.Command, error) {
4848
return &command.VerifyCmd{
49-
Ui: ui,
49+
Ui: ui,
50+
Version: Version,
5051
}, nil
5152
},
5253
"uninit": func() (cli.Command, error) {

0 commit comments

Comments
 (0)