-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_test.go
115 lines (98 loc) · 3.72 KB
/
start_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) Christoph Berger. All rights reserved.
// Use of this source code is governed by the BSD (3-Clause)
// License that can be found in the LICENSE.txt file.
//
// This source code may import third-party source code whose
// licenses are provided in the respective license files.
package start
import (
"os"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
flag "github.com/spf13/pflag"
)
func TestParse(t *testing.T) {
// ContinueOnError is required when running goconvey as server; otherwise, unrecognized
// flags that are passed to the test executable will cause an error:
// "unknown shorthand flag: 't' in -test.v=true"
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
os.Args = []string{os.Args[0], "--cmdline=FromCmdLine", "anargument", "anotherarg"}
Convey("When setting up some flag variables", t, func() {
var stringFlag string
var cmdlineStringFlag string
flag.StringVarP(&stringFlag, "envvar", "a", "From default", "A string flag")
flag.StringVarP(&cmdlineStringFlag, "cmdline", "c", "From default", "A string flag")
intFlag := flag.IntP("anint", "i", 23, "An integer flag")
boolFlag := flag.BoolP("anewbool", "b", true, "A boolean flag")
testcfg := "test/test.toml"
testenv := "START_ENVVAR"
SetConfigFile(testcfg)
os.Setenv(testenv, "From Environment Variable")
Reparse()
Convey("Then (Re)Parse() should find the correct values from config file ("+testcfg+"), env var ("+strings.ToUpper(appName()+"_"+"envvar")+"), or default.", func() {
So(cmdlineStringFlag, ShouldEqual, "FromCmdLine")
So(stringFlag, ShouldEqual, os.Getenv(testenv))
So(*intFlag, ShouldEqual, 42) // from config file
So(*boolFlag, ShouldEqual, true) // from default
})
Reset(func() {
os.Setenv("START_ASTRING", "")
})
})
}
func TestUp(t *testing.T) {
var first int
var second int
var global int
var params []string
SetInitFunc(func() error {
SetDescription("Testing testcmd")
return nil
})
// ContinueOnError is required when running goconvey as server; otherwise, unrecognized
// flags that are passed to the test executable will cause an error:
// "unknown shorthand flag: 't' in -test.v=true"
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
// To suppress warnings resulting from standard flags -test and -json,
// read -t and -j into dummy flags.
// The flags used for actual testing must not use -t or -j shorthands.
var testflag string
var jsonflag string
flag.StringVarP(&testflag, "t", "t", "t", "t")
flag.StringVarP(&jsonflag, "j", "j", "j", "j")
flag.IntVarP(&first, "first", "f", 1, "The first flag")
flag.IntVarP(&second, "second", "s", 2, "The second flag")
flag.IntVarP(&global, "global", "g", 3, "The global flag")
os.Args = []string{os.Args[0], "testcmd", "--first=10", "-s20", "arg1", "arg2"}
Commands = make(CommandMap) // clear the commands map for this test
Add(&Command{
Name: "testcmd",
Flags: []string{"first", "second"},
Cmd: func(cmd *Command) error {
params = cmd.Args
return nil
},
})
Up()
Convey("The test command should read all flags and parameters.", t, func() {
So(first, ShouldEqual, 10)
So(second, ShouldEqual, 20)
So(global, ShouldEqual, 3)
So(params[0], ShouldEqual, "arg1")
So(params[1], ShouldEqual, "arg2")
So(description, ShouldEqual, "Testing testcmd")
})
Convey("The test command should contain only its private flags.", t, func() {
cmd := Commands["testcmd"]
So(len(cmd.Flags), ShouldEqual, 2)
So(cmd.Flags[0], ShouldEqual, "first")
So(cmd.Flags[1], ShouldEqual, "second")
})
Convey("The help command should be available", t, func() {
cmd := Commands["help"]
So(cmd, ShouldNotBeNil)
So(cmd.Name, ShouldEqual, "help")
So(cmd.Cmd, ShouldEqual, help)
})
}