This repository was archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathparser_test.go
98 lines (86 loc) · 2.31 KB
/
parser_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
package config
import (
"errors"
"io/ioutil"
"os"
"os/user"
"path"
"testing"
"github.com/stretchr/testify/assert"
)
// TestNewParserJSON tests the parser using JSON backend.
func TestNewParserJSON(t *testing.T) {
p := NewParser(
InDir("../testdata/config/", "config_test.json", JSON()),
)
c, err := p.Parse()
assert.NoError(t, err)
assert.NotNil(t, c)
}
// TestNewParserPathShifting tests the parser when the filename suggests a directory substructure.
func TestNewParserPathShifting(t *testing.T) {
p := NewParser(
InDir("../testdata", "config/config_test.json", JSON()),
)
c, err := p.Parse()
assert.NoError(t, err)
assert.NotNil(t, c)
assert.Equal(t, "xxxxx", c.Password)
}
// TestNewParserYAML tests the parser using YAML backend.
func TestNewParserYAML(t *testing.T) {
p := NewParser(
InDir("../testdata/config/", "config_test.yml", YAML()),
)
c, err := p.Parse()
assert.NoError(t, err)
assert.NotNil(t, c)
assert.Equal(t, "fritz.box", c.Host)
}
// TestNewParserFileNotFound tests the parser when a file does not exist.
func TestNewParserFileNotFound(t *testing.T) {
p := NewParser(
InHomeDir(user.Current, "kjewhgjgsjdbgbjnjjub.json", JSON()),
)
_, err := p.Parse()
assert.Error(t, err)
}
// TestNewParserHomeDirError tests the parser a user's $HOME cannot be determined.
func TestNewParserHomeDirError(t *testing.T) {
p := NewParser(
InHomeDir(func() (*user.User, error) {
return nil, errors.New("cannot determine current user")
}, "config.json", JSON()),
)
_, err := p.Parse()
assert.Error(t, err)
}
// TestNewParserFileEmpty tests the parser when a config file is empty.
func TestNewParserFileEmpty(t *testing.T) {
f, err := ioutil.TempFile("", "TestNewParserFileEmpty_config.json")
assert.NoError(t, err)
defer f.Close()
defer os.Remove(f.Name())
tmpDir, fName := path.Split(f.Name())
p := NewParser(
InHomeDir(func() (*user.User, error) {
return &user.User{
HomeDir: tmpDir,
}, nil
}, fName, JSON()),
)
_, err = p.Parse()
assert.Error(t, err)
}
type errReader struct {
}
// Read always fails.
func (e *errReader) Read(p []byte) (n int, err error) {
return 0, errors.New("I always fail")
}
// TestYAMLWithError test the YAML Decode when there is an IO error.
func TestYAMLWithError(t *testing.T) {
y := YAML()
err := y(&errReader{}, &Config{})
assert.Error(t, err)
}