-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileread_flag_test.go
47 lines (40 loc) · 2.24 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
45
46
47
// Copyright 2015 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package dflag
import (
"flag"
"testing"
"fortio.org/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_BadFlagPathThenGood(t *testing.T) {
set := flag.NewFlagSet("foobar", flag.ContinueOnError)
dynFlag, fileFlag := 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")
// try changing the path to good one
fileFlag.Set("testdata/fileread_good.json")
assert.NoError(t, ReadFileFlags(set), "reading from a corrected set file should succeed")
}
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")
}