forked from exercism/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
181 lines (161 loc) · 4.4 KB
/
config_test.go
File metadata and controls
181 lines (161 loc) · 4.4 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package config
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLoad(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
configPath := filepath.Join(tmpDir, "config.json")
if err := os.Link(fixturePath(t, "config.json"), configPath); err != nil {
t.Fatal(err)
}
dirtyPath := filepath.Join(tmpDir, "dirty.json")
if err := os.Link(fixturePath(t, "dirty.json"), dirtyPath); err != nil {
t.Fatal(err)
}
testCases := []struct {
desc string
in string // the name of the file passed as a command line argument
out string // the name of the file that the config will be written to
dir, key, api, xapi string // the actual config values
}{
{
desc: "defaults",
in: "",
out: filepath.Join(tmpDir, File),
dir: filepath.Join(tmpDir, DirExercises),
key: "",
api: hostAPI,
xapi: hostXAPI,
},
{
desc: "no such file",
in: filepath.Join(tmpDir, "no-such.json"),
out: filepath.Join(tmpDir, "no-such.json"),
dir: filepath.Join(tmpDir, DirExercises),
key: "",
api: hostAPI,
xapi: hostXAPI,
},
{
desc: "file exists",
in: configPath,
out: configPath,
dir: "/a/b/c",
key: "abc123",
api: "http://api.example.com",
xapi: "http://x.example.com",
},
{
desc: "unexpanded path",
in: "~/config.json",
out: configPath,
dir: "/a/b/c",
key: "abc123",
api: "http://api.example.com",
xapi: "http://x.example.com",
},
{
desc: "sanitizes whitespace",
in: "~/dirty.json",
out: filepath.Join(tmpDir, "dirty.json"),
dir: "/a/b/c",
key: "abc123",
api: "http://api.example.com",
xapi: "http://x.example.com",
},
}
for _, tc := range testCases {
c := &Config{home: tmpDir}
if err := c.load(tc.in); err != nil {
t.Fatal(err)
}
assert.Equal(t, tc.out, c.File, tc.desc)
assert.Equal(t, tc.dir, c.Dir, tc.desc)
assert.Equal(t, tc.key, c.APIKey, tc.desc)
assert.Equal(t, tc.api, c.API, tc.desc)
assert.Equal(t, tc.xapi, c.XAPI, tc.desc)
}
}
func TestReadDirectory(t *testing.T) {
// if the provided path is a directory, append the default filename
tmpDir, err := ioutil.TempDir("", "")
assert.NoError(t, err)
myConfig, err := New(tmpDir)
assert.NoError(t, err)
expected := filepath.Join(tmpDir, File)
actual := myConfig.File
assert.Equal(t, expected, actual)
// if it can't determine if the provided path is a directory, don't modify
// the path
myConfig, err = New("badpath")
assert.NoError(t, err)
assert.Equal(t, "badpath", myConfig.File)
}
func TestLoad_InvalidJSON(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
invalidPath := filepath.Join(tmpDir, "config_invalid.json")
if err := os.Link(fixturePath(t, "config_invalid.json"), invalidPath); err != nil {
t.Fatal(err)
}
c := &Config{home: tmpDir}
err = c.load("~/config_invalid.json")
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "invalid JSON syntax")
}
}
func TestReadingWritingConfig(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
filename := fmt.Sprintf("%s/%s", tmpDir, File)
assert.NoError(t, err)
c1 := &Config{
APIKey: "MyKey",
Dir: "/exercism/directory",
API: "localhost",
XAPI: "localhost",
File: filename,
}
c1.Write()
c2, err := New(filename)
assert.NoError(t, err)
assert.Equal(t, c1.APIKey, c2.APIKey)
assert.Equal(t, c1.Dir, c2.Dir)
assert.Equal(t, c1.API, c2.API)
assert.Equal(t, c1.XAPI, c2.XAPI)
}
func TestUpdateConfig(t *testing.T) {
c := &Config{
APIKey: "MyKey",
API: "localhost",
Dir: "/exercism/directory",
XAPI: "localhost",
}
// Test the blank values don't overwrite existing values
c.Update("", "", "", "")
assert.Equal(t, "MyKey", c.APIKey)
assert.Equal(t, "localhost", c.API)
assert.Equal(t, "/exercism/directory", c.Dir)
assert.Equal(t, "localhost", c.XAPI)
// Test that each value can be overwritten
c.Update("NewKey", "http://example.com", "/tmp/exercism", "http://x.example.org")
assert.Equal(t, "NewKey", c.APIKey)
assert.Equal(t, "http://example.com", c.API)
assert.Equal(t, "/tmp/exercism", c.Dir)
assert.Equal(t, "http://x.example.org", c.XAPI)
}
func fixturePath(t *testing.T, filename string) string {
_, caller, _, ok := runtime.Caller(0)
assert.True(t, ok)
return filepath.Join(filepath.Dir(caller), "..", "fixtures", filename)
}