gpaste/config_test.go

94 lines
1.9 KiB
Go
Raw Normal View History

2022-01-18 23:38:25 +00:00
package gpaste_test
import (
"os"
"strings"
"testing"
"git.t-juice.club/torjus/gpaste"
"github.com/google/go-cmp/cmp"
)
func TestServerConfig(t *testing.T) {
t.Run("FromReader", func(t *testing.T) {
clearEnv()
simpleConfig := `
LogLevel = "INFO"
URL = "http://paste.example.org"
ListenAddr = ":8080"
[Store]
2022-01-19 00:03:24 +00:00
Type = "fs"
[Store.FS]
Dir = "/tmp"
2022-01-18 23:38:25 +00:00
`
expected := &gpaste.ServerConfig{
LogLevel: "INFO",
URL: "http://paste.example.org",
ListenAddr: ":8080",
Store: &gpaste.ServerStoreConfig{
2022-01-19 00:03:24 +00:00
Type: "fs",
FS: &gpaste.ServerStoreFSStoreConfig{
Dir: "/tmp",
},
2022-01-18 23:38:25 +00:00
},
}
sr := strings.NewReader(simpleConfig)
c, err := gpaste.ServerConfigFromReader(sr)
if err != nil {
t.Fatalf("Error parsing config: %s", err)
}
if !cmp.Equal(c, expected) {
t.Errorf("Result does not match: %s", cmp.Diff(c, expected))
}
})
t.Run("FromEnv", func(t *testing.T) {
clearEnv()
var envMap map[string]string = map[string]string{
2022-01-19 00:03:24 +00:00
"GPASTE_LOGLEVEL": "DEBUG",
"GPASTE_URL": "http://gpaste.example.org",
"GPASTE_STORE_TYPE": "fs",
"GPASTE_LISTENADDR": ":8000",
"GPASTE_STORE_FS_DIR": "/tmp",
2022-01-18 23:38:25 +00:00
}
expected := &gpaste.ServerConfig{
LogLevel: "DEBUG",
URL: "http://gpaste.example.org",
ListenAddr: ":8000",
Store: &gpaste.ServerStoreConfig{
2022-01-19 00:03:24 +00:00
Type: "fs",
FS: &gpaste.ServerStoreFSStoreConfig{
Dir: "/tmp",
},
2022-01-18 23:38:25 +00:00
},
}
for k, v := range envMap {
os.Setenv(k, v)
}
sr := strings.NewReader("")
c, err := gpaste.ServerConfigFromReader(sr)
if err != nil {
t.Fatalf("Error parsing empty config")
}
if !cmp.Equal(c, expected) {
t.Errorf("Result does not match: %s", cmp.Diff(c, expected))
}
})
}
func clearEnv() {
for _, env := range os.Environ() {
result := strings.Split(env, "=")
value := result[0]
if strings.Contains(value, "GPASTE_") {
os.Unsetenv(value)
}
}
}