aboutsummaryrefslogtreecommitdiff
path: root/config
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-04-17 18:13:38 +0300
committerTulir Asokan <tulir@maunium.net>2018-04-17 18:13:38 +0300
commit55a2fb7803ebb2177d43736c7c682a7f461cf19a (patch)
tree8d05addc5098a08b1205d513d88b8f314ade9dbd /config
parent96ce95cf14e710c5079834c6a28895623c201ff4 (diff)
Add tests for config handling
Diffstat (limited to 'config')
-rw-r--r--config/config.go7
-rw-r--r--config/config_test.go128
2 files changed, 134 insertions, 1 deletions
diff --git a/config/config.go b/config/config.go
index 85160c6..55601df 100644
--- a/config/config.go
+++ b/config/config.go
@@ -52,16 +52,19 @@ func (config *Config) Clear() {
config.Session.Clear()
}
os.RemoveAll(config.HistoryDir)
+ os.RemoveAll(config.MediaDir)
}
// Load loads the config from config.yaml in the directory given to the config struct.
func (config *Config) Load() {
os.MkdirAll(config.Dir, 0700)
- os.MkdirAll(config.HistoryDir, 0700)
+
configPath := filepath.Join(config.Dir, "config.yaml")
data, err := ioutil.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {
+ os.MkdirAll(config.HistoryDir, 0700)
+ os.MkdirAll(config.MediaDir, 0700)
return
} else {
fmt.Println("Failed to read config from", configPath)
@@ -74,6 +77,8 @@ func (config *Config) Load() {
fmt.Println("Failed to parse config at", configPath)
panic(err)
}
+ os.MkdirAll(config.HistoryDir, 0700)
+ os.MkdirAll(config.MediaDir, 0700)
}
// Save saves this config to config.yaml in the directory given to the config struct.
diff --git a/config/config_test.go b/config/config_test.go
index 780ac6b..525a09d 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -15,3 +15,131 @@
// along with this program. If not, see <http://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")
+ 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")
+
+ 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")
+
+ 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")
+
+ 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")
+
+ 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")
+
+ 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")
+
+ 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")
+}