Skip to content

Commit 9b0f2ef

Browse files
Merge pull request #7 from andreimerlescu/hotfix/listmap-append
Implemented PolicyMapAppend and added tests for PolicyFlagAppend
2 parents d9a5048 + b4677b3 commit 9b0f2ef

File tree

11 files changed

+78
-10
lines changed

11 files changed

+78
-10
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v2.0.7
1+
v2.0.8

assure_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package figtree
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
65
"time"
6+
7+
"github.com/stretchr/testify/assert"
78
)
89

910
func TestSomeAssurances(t *testing.T) {

callback_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package figtree
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
65
"time"
6+
7+
"github.com/stretchr/testify/assert"
78
)
89

910
func TestTree_WithCallback(t *testing.T) {

list_flag.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func (l *ListFlag) Set(value string) error {
4545
items := strings.Split(value, ",")
4646
if PolicyListAppend {
4747
*l.values = append(*l.values, items...)
48+
} else {
49+
*l.values = items
4850
}
4951
return nil
5052
}

list_flag_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package figtree
22

33
import (
4-
"github.com/stretchr/testify/assert"
4+
"os"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
func TestTree_ListValues(t *testing.T) {
@@ -11,3 +13,28 @@ func TestTree_ListValues(t *testing.T) {
1113
assert.NoError(t, figs.Parse())
1214
assert.Contains(t, *figs.List(t.Name()), "yahuah")
1315
}
16+
17+
func TestListFlag_Set(t *testing.T) {
18+
t.Run("PolicyListAppend_TRUE", func(t *testing.T) {
19+
PolicyListAppend = true
20+
os.Args = []string{os.Args[0], "-x", "yahuah"}
21+
figs := With(Options{Germinate: true})
22+
figs.NewList("x", []string{"bum"}, "Name List")
23+
assert.NoError(t, figs.Parse())
24+
assert.Equal(t, "", figs.Fig("x").ToString())
25+
assert.Contains(t, *figs.List("x"), "yahuah")
26+
assert.Contains(t, *figs.List("x"), "bum") // Contains because of PolicyListAppend
27+
os.Args = []string{os.Args[0]}
28+
})
29+
t.Run("PolicyListAppend_DEFAULT", func(t *testing.T) {
30+
PolicyListAppend = false
31+
os.Args = []string{os.Args[0], "-x", "yahuah"}
32+
figs := With(Options{Germinate: true})
33+
figs.NewList("x", []string{"bum"}, "Name List")
34+
assert.NoError(t, figs.Parse())
35+
assert.Equal(t, "", figs.Fig("x").ToString())
36+
assert.Contains(t, *figs.List("x"), "yahuah")
37+
assert.NotContains(t, *figs.List("x"), "bum") // NotContains because of PolicyListAppend
38+
os.Args = []string{os.Args[0]}
39+
})
40+
}

map_flag.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ func (m *MapFlag) String() string {
6666
return strings.Join(entries, ",")
6767
}
6868

69+
var PolicyMapAppend = false
70+
6971
// Set accepts a value like KEY=VALUE,KEY=VALUE,KEY=VALUE to override map values
7072
func (m *MapFlag) Set(value string) error {
7173
if m.values == nil {
7274
m.values = &map[string]string{}
7375
}
76+
if !PolicyMapAppend {
77+
m.values = &map[string]string{}
78+
}
7479
pairs := strings.Split(value, ",")
7580
for _, pair := range pairs {
7681
kv := strings.SplitN(pair, "=", 2)

map_flag_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package figtree
22

33
import (
4-
"github.com/stretchr/testify/assert"
4+
"os"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
func TestTree_MapKeys(t *testing.T) {
@@ -11,3 +13,29 @@ func TestTree_MapKeys(t *testing.T) {
1113
assert.NoError(t, figs.Parse())
1214
assert.Contains(t, figs.MapKeys(t.Name()), "name")
1315
}
16+
17+
func TestMapFlag_Set(t *testing.T) {
18+
t.Run("PolicyMapAppend_TRUE", func(t *testing.T) {
19+
PolicyMapAppend = true
20+
os.Args = []string{os.Args[0], "-x", "name=yahuah"}
21+
figs := With(Options{Germinate: true})
22+
figs.NewMap("x", map[string]string{"job": "bum"}, "Name Map")
23+
assert.NoError(t, figs.Parse())
24+
assert.Equal(t, "", figs.Fig("x").ToString())
25+
assert.Contains(t, figs.MapKeys("x"), "name")
26+
assert.Contains(t, figs.MapKeys("x"), "job") // Contains because of PolicyMapAppend
27+
os.Args = []string{os.Args[0]}
28+
})
29+
t.Run("PolicyMapAppend_DEFAULT", func(t *testing.T) {
30+
PolicyMapAppend = false
31+
os.Args = []string{os.Args[0], "-x", "name=yahuah"}
32+
figs := With(Options{Germinate: true})
33+
figs.NewMap("x", map[string]string{"job": "bum"}, "Name Map")
34+
assert.NoError(t, figs.Parse())
35+
assert.Equal(t, "", figs.Fig("x").ToString())
36+
assert.Contains(t, figs.MapKeys("x"), "name")
37+
assert.NotContains(t, figs.MapKeys("x"), "job") // NotContains because no PolicyMapAppend
38+
os.Args = []string{os.Args[0]}
39+
})
40+
41+
}

parsing_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package figtree
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"path/filepath"
65
"testing"
6+
7+
"github.com/stretchr/testify/assert"
78
)
89

910
func TestTree_Parse(t *testing.T) {

rules_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package figtree
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"os"
65
"sync"
76
"testing"
87
"time"
8+
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestRules(t *testing.T) {

savior.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"github.com/go-ini/ini"
8-
"gopkg.in/yaml.v3"
97
"os"
108
"path/filepath"
9+
10+
"github.com/go-ini/ini"
11+
"gopkg.in/yaml.v3"
1112
)
1213

1314
func (tree *figTree) ReadFrom(path string) error {

0 commit comments

Comments
 (0)