This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli_source_test.go
112 lines (86 loc) · 3.23 KB
/
cli_source_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
package configo
import (
"bytes"
"fmt"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestCLISourceFixture(t *testing.T) {
gunit.Run(new(CLISourceFixture), t)
}
type CLISourceFixture struct {
*gunit.Fixture
source *CLISource
output *bytes.Buffer
}
var commandLineValue = "value from command line"
func (this *CLISourceFixture) TestMatchingValueFound() {
this.Println(`Simulates at the command line: ./app -flagName="value from command line"`)
this.source = FromCLI(Flag("flagName", "This is a cool flag"))
this.source.source = []string{"./app", fmt.Sprintf("-flagName=%s", commandLineValue)}
this.source.Initialize()
values, err := this.source.Strings("flagName")
this.So(values, should.Resemble, []string{commandLineValue})
this.So(err, should.BeNil)
}
func (this *CLISourceFixture) TestFlagNotPassed__NotFound() {
this.Println(`Simulates no command line flag passed.`)
this.source = FromCLI(Flag("flagName", "This is a cool flag"))
this.source.source = []string{"./app"}
this.source.Initialize()
values, err := this.source.Strings("flagName")
var expectedValue []string // nil
this.So(values, should.Resemble, expectedValue)
this.So(err, should.Equal, ErrKeyNotFound)
}
func (this *CLISourceFixture) TestFlagNotDefined__NoValuesReturned() {
this.Println(`Simulates requesting the value of an undefined flag`)
this.source = FromCLI(Flag("flagName", "This is a cool flag"))
this.source.source = []string{"./app"}
this.source.Initialize()
values, err := this.source.Strings("unknown")
var expectedValue []string // nil
this.So(values, should.Resemble, expectedValue)
this.So(err, should.Equal, ErrKeyNotFound)
}
func (this *CLISourceFixture) TestBooleanFlagDefined() {
this.Println(`Simulates requesting the value of a boolean flag`)
this.source = FromCLI(
BoolFlag("flagname1", "This is cool"),
BoolFlag("flagname2", "This is cooler"),
BoolFlag("flagname3", "This is coolest"),
BoolFlag("flagname4", "This is stellar"),
)
this.source.source = []string{"./app", "-flagname1", "-flagname2=true", "-flagname3=false"}
this.source.Initialize()
values, err := this.source.Strings("flagname1")
this.So(values, should.Resemble, []string{"true"})
this.So(err, should.BeNil)
values, err = this.source.Strings("flagname2")
this.So(values, should.Resemble, []string{"true"})
this.So(err, should.BeNil)
values, err = this.source.Strings("flagname3")
this.So(values, should.Resemble, []string{"false"})
this.So(err, should.BeNil)
values, err = this.source.Strings("flagname4")
this.So(values, should.BeNil)
this.So(err, should.Equal, ErrKeyNotFound)
values, err = this.source.Strings("flagname5")
this.So(values, should.BeNil)
this.So(err, should.Equal, ErrKeyNotFound)
}
func (this *CLISourceFixture) TestUsageMessage() {
buffer := new(bytes.Buffer)
this.source = FromCLI(
Flag("something", "yeah"),
Usage("This is some helpful text"),
ContinueOnError(),
SetOutput(buffer),
)
this.source.source = []string{"./app", "-help", "-something"}
this.source.Initialize()
this.So(buffer.String(), should.ContainSubstring, "-something")
this.So(buffer.String(), should.ContainSubstring, "yeah")
this.So(buffer.String(), should.ContainSubstring, "This is some helpful text")
}