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
83 lines (66 loc) · 1.95 KB
/
config_test.go
File metadata and controls
83 lines (66 loc) · 1.95 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
package config
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDemoDir(t *testing.T) {
path, err := ioutil.TempDir("", "")
assert.NoError(t, err)
os.Chdir(path)
path, err = filepath.EvalSymlinks(path)
assert.NoError(t, err)
path = filepath.Join(path, "exercism-demo")
demoDir, err := demoDirectory()
assert.NoError(t, err)
assert.Equal(t, demoDir, path)
}
func TestExpandsTildeInExercismDirectory(t *testing.T) {
expandedDir := ReplaceTilde("~/exercism/directory")
assert.NotContains(t, "~", expandedDir)
}
func TestReadingWritingConfig(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
filename := Filename(tmpDir)
assert.NoError(t, err)
writtenConfig := Config{
GithubUsername: "user",
APIKey: "MyKey",
ExercismDirectory: "/exercism/directory",
Hostname: "localhost",
}
ToFile(filename, writtenConfig)
loadedConfig, err := FromFile(filename)
assert.NoError(t, err)
assert.Equal(t, writtenConfig, loadedConfig)
}
func TestDecodingConfig(t *testing.T) {
unsanitizedJson := `{"githubUsername":"user ","apiKey":"MyKey ","exercismDirectory":"/exercism/directory\r\n","hostname":"localhost \r\n"}`
sanitizedConfig := Config{
GithubUsername: "user",
APIKey: "MyKey",
ExercismDirectory: "/exercism/directory",
Hostname: "localhost",
}
b := bytes.NewBufferString(unsanitizedJson)
c, err := Decode(b)
assert.NoError(t, err)
assert.Equal(t, sanitizedConfig, c)
}
func TestEncodingConfig(t *testing.T) {
currentConfig := Config{
GithubUsername: "user\r\n",
APIKey: "MyKey ",
ExercismDirectory: "/home/user name ",
Hostname: "localhost ",
}
sanitizedJson := `{"githubUsername":"user","apiKey":"MyKey","exercismDirectory":"/home/user name","hostname":"localhost"}
`
buf := new(bytes.Buffer)
err := Encode(buf, currentConfig)
assert.NoError(t, err)
assert.Equal(t, sanitizedJson, buf.String())
}