/
config_test.go
85 lines (73 loc) · 1.5 KB
/
config_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
package main
import (
"io/ioutil"
"log"
"os"
"syscall"
"testing"
)
const payload string = `
{
"dot_path": "/path/to/dotfiles",
"files": {
"test_file_1": "path-to-test-file-1",
"test_file_2": "path-to-test-file-2"
}
}
`
// Define here the setup and teardown functions
func TestMain(m *testing.M) {
setUp()
retCode := m.Run()
os.Exit(retCode)
}
// setUp() will create a config file for the other tests so we can test the
// methods associated with the config struct
func setUp() {
// create a test dotconfig file
f, err := ioutil.TempFile("", ".dotconfig")
if err != nil {
log.Fatal(err)
}
defer syscall.Unlink(f.Name())
ioutil.WriteFile(
f.Name(),
[]byte(payload),
0644,
)
// try to read the file
_, err = NewConfig(f.Name())
if err != nil {
log.Fatal(err)
}
}
func TestNewConfig(t *testing.T) {
// create a test dotconfig file
f, err := ioutil.TempFile("", ".dotconfig")
if err != nil {
log.Fatal(err)
}
defer syscall.Unlink(f.Name())
ioutil.WriteFile(
f.Name(),
[]byte(payload),
0644,
)
// try to read the file
c, err := NewConfig(f.Name())
if err != nil {
log.Fatal(err)
}
// c.DotPath should be the same as payload
if c.DotPath != "/path/to/dotfiles" {
t.Error("c.DotPath doesn't match")
}
// c.Files should be the same as payload
if c.Files["test_file_1"] != "path-to-test-file-1" {
t.Error("c.Files doesn't match")
}
// c.Files should be the same as payload
if c.Files["test_file_2"] != "path-to-test-file-2" {
t.Error("c.Files doesn't match")
}
}