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
|
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2019 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package config_test
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"maunium.net/go/gomuks/config"
)
func TestNewConfig_Defaults(t *testing.T) {
cfg := config.NewConfig("/tmp/gomuks-test-0", "/tmp/gomuks-test-0")
assert.Equal(t, "/tmp/gomuks-test-0", cfg.Dir)
assert.Equal(t, "/tmp/gomuks-test-0/history", cfg.HistoryDir)
assert.Equal(t, "/tmp/gomuks-test-0/media", cfg.MediaDir)
}
func TestConfig_Load_NonexistentDoesntFail(t *testing.T) {
cfg := config.NewConfig("/tmp/gomuks-test-1", "/tmp/gomuks-test-1")
defer os.RemoveAll("/tmp/gomuks-test-1")
cfg.Load()
stat, err := os.Stat(cfg.MediaDir)
assert.Nil(t, err)
assert.True(t, stat.IsDir())
stat, err = os.Stat(cfg.HistoryDir)
assert.Nil(t, err)
assert.True(t, stat.IsDir())
}
func TestConfig_Load_DirectoryFails(t *testing.T) {
os.MkdirAll("/tmp/gomuks-test-2/config.yaml", 0700)
cfg := config.NewConfig("/tmp/gomuks-test-2", "/tmp/gomuks-test-2")
defer os.RemoveAll("/tmp/gomuks-test-2")
defer func() {
if err := recover(); err == nil {
t.Fatalf("Load() didn't panic")
}
}()
cfg.Load()
}
func TestConfig_Load_ExistingFileIsLoaded(t *testing.T) {
os.MkdirAll("/tmp/gomuks-test-3", 0700)
ioutil.WriteFile("/tmp/gomuks-test-3/config.yaml", []byte(`{
"mxid": "foo",
"homeserver": "bar",
"history_dir": "/tmp/gomuks-test-3/foo",
"media_dir": "/tmp/gomuks-test-3/bar"
}`), 0700)
cfg := config.NewConfig("/tmp/gomuks-test-3", "/tmp/gomuks-test-3")
defer os.RemoveAll("/tmp/gomuks-test-3")
cfg.Load()
assert.Equal(t, "foo", cfg.UserID)
assert.Equal(t, "bar", cfg.HS)
assert.Equal(t, "/tmp/gomuks-test-3/foo", cfg.HistoryDir)
assert.Equal(t, "/tmp/gomuks-test-3/bar", cfg.MediaDir)
stat, err := os.Stat(cfg.MediaDir)
assert.Nil(t, err)
assert.True(t, stat.IsDir())
stat, err = os.Stat(cfg.HistoryDir)
assert.Nil(t, err)
assert.True(t, stat.IsDir())
}
func TestConfig_Load_InvalidExistingFilePanics(t *testing.T) {
os.MkdirAll("/tmp/gomuks-test-4", 0700)
ioutil.WriteFile("/tmp/gomuks-test-4/config.yaml", []byte(`this is not JSON.`), 0700)
cfg := config.NewConfig("/tmp/gomuks-test-4", "/tmp/gomuks-test-4")
defer os.RemoveAll("/tmp/gomuks-test-4")
defer func() {
if err := recover(); err == nil {
t.Fatalf("Load() didn't panic")
}
}()
cfg.Load()
}
func TestConfig_Clear(t *testing.T) {
cfg := config.NewConfig("/tmp/gomuks-test-5", "/tmp/gomuks-test-5")
defer os.RemoveAll("/tmp/gomuks-test-5")
cfg.Load()
stat, err := os.Stat(cfg.MediaDir)
assert.Nil(t, err)
assert.True(t, stat.IsDir())
stat, err = os.Stat(cfg.HistoryDir)
assert.Nil(t, err)
assert.True(t, stat.IsDir())
cfg.Clear()
stat, err = os.Stat(cfg.MediaDir)
assert.True(t, os.IsNotExist(err))
assert.Nil(t, stat)
stat, err = os.Stat(cfg.HistoryDir)
assert.True(t, os.IsNotExist(err))
assert.Nil(t, stat)
}
func TestConfig_Save(t *testing.T) {
cfg := config.NewConfig("/tmp/gomuks-test-6", "/tmp/gomuks-test-6")
defer os.RemoveAll("/tmp/gomuks-test-6")
cfg.Load()
cfg.Save()
dat, err := ioutil.ReadFile("/tmp/gomuks-test-6/config.yaml")
assert.Nil(t, err)
assert.Contains(t, string(dat), "/tmp/gomuks-test-6")
}
|