diff options
author | Tulir Asokan <tulir@maunium.net> | 2020-04-16 19:27:35 +0300 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2020-04-19 15:01:16 +0300 |
commit | 815190be147e575f12211c468f8121e5c60e6337 (patch) | |
tree | 8abd9b3d3952dbf767ca369ddad400db8a6f2d45 /config | |
parent | ff20c2c44f86b40f9214f1dc3d339584e48374f1 (diff) |
Update stuff and move pushrules to mautrix-go
Diffstat (limited to 'config')
-rw-r--r-- | config/config.go | 25 | ||||
-rw-r--r-- | config/config_test.go | 149 |
2 files changed, 13 insertions, 161 deletions
diff --git a/config/config.go b/config/config.go index e62a8bd..634120b 100644 --- a/config/config.go +++ b/config/config.go @@ -26,9 +26,10 @@ import ( "gopkg.in/yaml.v2" "maunium.net/go/mautrix" + "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix/pushrules" "maunium.net/go/gomuks/debug" - "maunium.net/go/gomuks/matrix/pushrules" "maunium.net/go/gomuks/matrix/rooms" ) @@ -52,9 +53,9 @@ type UserPreferences struct { // Config contains the main config of gomuks. type Config struct { - UserID string `yaml:"mxid"` - AccessToken string `yaml:"access_token"` - HS string `yaml:"homeserver"` + UserID id.UserID `yaml:"mxid"` + AccessToken string `yaml:"access_token"` + HS string `yaml:"homeserver"` RoomCacheSize int `yaml:"room_cache_size"` RoomCacheAge int64 `yaml:"room_cache_age"` @@ -242,36 +243,36 @@ func (config *Config) save(name, dir, file string, source interface{}) { } } -func (config *Config) GetUserID() string { +func (config *Config) GetUserID() id.UserID { return config.UserID } -func (config *Config) SaveFilterID(_, filterID string) { +func (config *Config) SaveFilterID(_ id.UserID, filterID string) { config.AuthCache.FilterID = filterID config.SaveAuthCache() } -func (config *Config) LoadFilterID(_ string) string { +func (config *Config) LoadFilterID(_ id.UserID) string { return config.AuthCache.FilterID } -func (config *Config) SaveNextBatch(_, nextBatch string) { +func (config *Config) SaveNextBatch(_ id.UserID, nextBatch string) { config.AuthCache.NextBatch = nextBatch config.SaveAuthCache() } -func (config *Config) LoadNextBatch(_ string) string { +func (config *Config) LoadNextBatch(_ id.UserID) string { return config.AuthCache.NextBatch } -func (config *Config) SaveRoom(room *mautrix.Room) { +func (config *Config) SaveRoom(_ *mautrix.Room) { panic("SaveRoom is not supported") } -func (config *Config) LoadRoom(roomID string) *mautrix.Room { +func (config *Config) LoadRoom(_ id.RoomID) *mautrix.Room { panic("LoadRoom is not supported") } -func (config *Config) GetRoom(roomID string) *rooms.Room { +func (config *Config) GetRoom(roomID id.RoomID) *rooms.Room { return config.Rooms.GetOrCreate(roomID) } diff --git a/config/config_test.go b/config/config_test.go deleted file mode 100644 index 4aa076f..0000000 --- a/config/config_test.go +++ /dev/null @@ -1,149 +0,0 @@ -// 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.db", cfg.HistoryPath) - 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()) - - /* FIXME - 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_path": "/tmp/gomuks-test-3/foo.db", - "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.db", cfg.HistoryPath) - 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()) - - /* FIXME - 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()) - - /* FIXME - 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) - - /* FIXME - 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") -} |