-
Notifications
You must be signed in to change notification settings - Fork 16
/
callback_var_test.go
64 lines (57 loc) · 1.51 KB
/
callback_var_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package goflags
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSuccessfulCallback(t *testing.T) {
toolName := "tool_1"
want := `updated successfully!`
got := &bytes.Buffer{}
flagSet := NewFlagSet()
flagSet.CreateGroup("Update", "Update",
flagSet.CallbackVar(updateCallbackFunc(toolName, got), "update", fmt.Sprintf("update %v to the latest released version", toolName)),
flagSet.CallbackVarP(func() {}, "disable-update-check", "duc", "disable automatic update check"),
)
os.Args = []string{
os.Args[0],
"-update",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, want, got.String())
tearDown(t.Name())
}
func TestFailCallback(t *testing.T) {
toolName := "tool_1"
got := &bytes.Buffer{}
if os.Getenv("IS_SUB_PROCESS") == "1" {
flagSet := NewFlagSet()
flagSet.CommandLine.SetOutput(got)
flagSet.CreateGroup("Update", "Update",
flagSet.CallbackVar(nil, "update", fmt.Sprintf("update %v to the latest released version", toolName)),
)
os.Args = []string{
os.Args[0],
"-update",
}
_ = flagSet.Parse()
}
cmd := exec.Command(os.Args[0], "-test.run=TestFailCallback")
cmd.Env = append(os.Environ(), "IS_SUB_PROCESS=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit error", err)
tearDown(t.Name())
}
func updateCallbackFunc(toolName string, cliOutput io.Writer) func() {
return func() {
fmt.Fprintf(cliOutput, "updated successfully!")
}
}