@@ -2,40 +2,15 @@ package cli
22
33import (
44 "flag"
5+ "strconv"
6+ "strings"
57 "testing"
68
79 "github.com/rs/zerolog"
810 "github.com/stretchr/testify/assert"
9- "github.com/stretchr/testify/mock"
1011 "github.com/urfave/cli/v2"
1112)
1213
13- func TestCombineBeforeFuncs (t * testing.T ) {
14- mockBeforeFuncs := new (mockBeforeFuncs )
15- mockBeforeFuncs .On ("Func1" , mock .Anything ).Return (nil )
16- mockBeforeFuncs .On ("Func2" , mock .Anything ).Return (nil )
17-
18- before_func := CombineBeforeFuncs (mockBeforeFuncs .Func1 , mockBeforeFuncs .Func2 )
19-
20- _ = before_func (nil )
21-
22- mockBeforeFuncs .AssertExpectations (t )
23- }
24-
25- type mockBeforeFuncs struct {
26- mock.Mock
27- }
28-
29- func (m * mockBeforeFuncs ) Func1 (cCtx * cli.Context ) error {
30- args := m .Called (cCtx )
31- return args .Error (0 )
32- }
33-
34- func (m * mockBeforeFuncs ) Func2 (cCtx * cli.Context ) error {
35- args := m .Called (cCtx )
36- return args .Error (0 )
37- }
38-
3914func TestConfigureLogs (t * testing.T ) {
4015 testCases := map [bool ]zerolog.Level {
4116 true : zerolog .DebugLevel ,
@@ -53,26 +28,127 @@ func TestConfigureLogs(t *testing.T) {
5328 }
5429}
5530
56- // Tests for ConfigFileLoader when no file is found.
57- // There should be an equivalent test for when the file is found, but it's tough.
58- func TestConfigFileLoaderNoFile (t * testing.T ) {
59- flag := flag .NewFlagSet ("config" , flag .ContinueOnError )
60- flag .String ("config" , "nonexistent" , "" )
61- context := cli .NewContext (nil , flag , nil )
31+ func TestGetStringIfSettest (t * testing.T ) {
32+ want := "hello"
33+ flagName := "testFlag"
6234
63- beforeFunc := GetConfigFileLoader (nil , "config" )
64- err := beforeFunc (context )
35+ flag := flag .NewFlagSet ("" , flag .ContinueOnError )
36+ flag .String (flagName , "" , "" )
37+ _ = flag .Set (flagName , want )
38+ cCtx := cli .NewContext (nil , flag , nil )
6539
66- assert .Nil (t , err )
40+ got := getStringIfSet (cCtx , flagName )
41+
42+ assert .Equal (t , want , * got )
6743}
6844
69- func TestLogArguments (t * testing.T ) {
70- flag := flag .NewFlagSet ("flag" , flag .ContinueOnError )
71- flag .String ("some-flag" , "" , "" )
72- _ = flag .Set ("some-flag" , "value" )
73- context := cli .NewContext (nil , flag , nil )
45+ func TestGetStringIfSet (t * testing.T ) {
46+ testCases := []struct {
47+ name string
48+ want string
49+ set bool
50+ }{{
51+ name : "value" ,
52+ want : "hello" ,
53+ set : true ,
54+ }, {
55+ name : "nil" ,
56+ want : "" ,
57+ set : false ,
58+ }}
59+
60+ flagName := "testFlag"
61+ for _ , tc := range testCases {
62+ t .Run (tc .name , func (t * testing.T ) {
63+ flag := flag .NewFlagSet ("" , flag .ContinueOnError )
64+ flag .String (flagName , "" , "" )
65+ if tc .set {
66+ _ = flag .Set (flagName , tc .want )
67+ }
68+ cCtx := cli .NewContext (nil , flag , nil )
69+
70+ got := getStringIfSet (cCtx , flagName )
71+
72+ if tc .set {
73+ assert .NotNil (t , got )
74+ assert .Equal (t , tc .want , * got )
75+ } else {
76+ assert .Nil (t , got )
77+ }
78+ })
79+ }
80+ }
7481
75- _ = LogArguments (context )
82+ func TestGetStringSliceIfSet (t * testing.T ) {
83+ testCases := []struct {
84+ name string
85+ want []string
86+ set bool
87+ }{{
88+ name : "value" ,
89+ want : []string {"hello" , "world" },
90+ set : true ,
91+ }, {
92+ name : "nil" ,
93+ want : []string {},
94+ set : false ,
95+ }}
96+
97+ flagName := "testFlag"
98+ for _ , tc := range testCases {
99+ t .Run (tc .name , func (t * testing.T ) {
100+ flag := flag .NewFlagSet ("" , flag .ContinueOnError )
101+ flag .Var (& cli.StringSlice {}, flagName , "" )
102+ if tc .set {
103+ _ = flag .Set (flagName , strings .Join (tc .want , ", " ))
104+ }
105+ cCtx := cli .NewContext (nil , flag , nil )
106+
107+ got := getStringSliceIfSet (cCtx , flagName )
108+
109+ if tc .set {
110+ assert .NotNil (t , got )
111+ assert .Equal (t , tc .want , * got )
112+ } else {
113+ assert .Nil (t , got )
114+ }
115+ })
116+ }
117+ }
76118
77- // How to assert that the log message was correct?
119+ func TestGetBoolIfSet (t * testing.T ) {
120+ testCases := []struct {
121+ name string
122+ want bool
123+ set bool
124+ }{{
125+ name : "value" ,
126+ want : true ,
127+ set : true ,
128+ }, {
129+ name : "nil" ,
130+ want : false ,
131+ set : false ,
132+ }}
133+
134+ flagName := "testFlag"
135+ for _ , tc := range testCases {
136+ t .Run (tc .name , func (t * testing.T ) {
137+ flag := flag .NewFlagSet ("" , flag .ContinueOnError )
138+ flag .Bool (flagName , false , "" )
139+ if tc .set {
140+ _ = flag .Set (flagName , strconv .FormatBool (tc .want ))
141+ }
142+ cCtx := cli .NewContext (nil , flag , nil )
143+
144+ got := getBoolIfSet (cCtx , flagName )
145+
146+ if tc .set {
147+ assert .NotNil (t , got )
148+ assert .Equal (t , tc .want , * got )
149+ } else {
150+ assert .Nil (t , got )
151+ }
152+ })
153+ }
78154}
0 commit comments