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] Type = "fs" [Store.FS] Dir = "/tmp" ` expected := &gpaste.ServerConfig{ LogLevel: "INFO", URL: "http://paste.example.org", ListenAddr: ":8080", Store: &gpaste.ServerStoreConfig{ Type: "fs", FS: &gpaste.ServerStoreFSStoreConfig{ Dir: "/tmp", }, }, } 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{ "GPASTE_LOGLEVEL": "DEBUG", "GPASTE_URL": "http://gpaste.example.org", "GPASTE_STORE_TYPE": "fs", "GPASTE_LISTENADDR": ":8000", "GPASTE_STORE_FS_DIR": "/tmp", } expected := &gpaste.ServerConfig{ LogLevel: "DEBUG", URL: "http://gpaste.example.org", ListenAddr: ":8000", Store: &gpaste.ServerStoreConfig{ Type: "fs", FS: &gpaste.ServerStoreFSStoreConfig{ Dir: "/tmp", }, }, } 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) } } }