-
Notifications
You must be signed in to change notification settings - Fork 24
/
fileread_flag_test.go
44 lines (37 loc) · 2.09 KB
/
fileread_flag_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
// Copyright 2015 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package flagz
import (
"testing"
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
func TestFileFlag_ReadsWithADefault(t *testing.T) {
set := flag.NewFlagSet("foobar", flag.ContinueOnError)
dynFlag := DynJSON(set, "some_json_1", defaultJSON, "Use it or lose it").WithFileFlag("testdata/fileread_good.json")
assert.EqualValues(t, defaultJSON, dynFlag.Get(), "value must be default after create, but before reading files")
assert.NoError(t, ReadFileFlags(set), "reading from a file should succeed")
assert.EqualValues(t,
&outerJSON{FieldInts: []int{42}, FieldString: "new-value", FieldInner: &innerJSON{FieldBool: false}},
dynFlag.Get(),
"value must be set after reading from file")
}
func TestFileFlag_EmptyPathsAreIgnored(t *testing.T) {
set := flag.NewFlagSet("foobar", flag.ContinueOnError)
dynFlag := DynJSON(set, "some_json_1", defaultJSON, "Use it or lose it").WithFileFlag("")
assert.EqualValues(t, defaultJSON, dynFlag.Get(), "value must be default after create, but before reading files")
assert.NoError(t, ReadFileFlags(set), "reading from a file should succeed")
assert.EqualValues(t, defaultJSON, dynFlag.Get(), "value must be default after read from file")
}
func TestFileFlag_BadFlagPath(t *testing.T) {
set := flag.NewFlagSet("foobar", flag.ContinueOnError)
dynFlag := DynJSON(set, "some_json_1", defaultJSON, "Use it or lose it").WithFileFlag("testdata/unknown.json")
assert.Error(t, ReadFileFlags(set), "reading from must not succeed for an unknown json")
assert.EqualValues(t, defaultJSON, dynFlag.Get(), "value must be default after failed to read a file")
}
func TestFileFlag_BadFileContent(t *testing.T) {
set := flag.NewFlagSet("foobar", flag.ContinueOnError)
dynFlag := DynJSON(set, "some_json_1", defaultJSON, "Use it or lose it").WithFileFlag("testdata/fileread_bad.json")
assert.Error(t, ReadFileFlags(set), "reading from must not succeed for an unknown json")
assert.EqualValues(t, defaultJSON, dynFlag.Get(), "value must be default after failed to read a file")
}