Add tests for config handling

This commit is contained in:
Tulir Asokan 2018-04-17 18:13:38 +03:00
parent 96ce95cf14
commit 55a2fb7803
2 changed files with 134 additions and 1 deletions

View File

@ -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.

View File

@ -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")
}