added test for config saving and loading
All checks were successful
Tests / test (pull_request) Has been skipped
All checks were successful
Tests / test (pull_request) Has been skipped
This commit is contained in:
parent
b35040aa79
commit
4c87993667
2 changed files with 91 additions and 4 deletions
|
@ -10,7 +10,11 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
configFileName = "config.json"
|
||||
configFileName string = "config.json"
|
||||
|
||||
defaultHTTPTimeout int = 5
|
||||
defaultHTTPMediaTimeout int = 30
|
||||
defaultLineWrapMaxWidth int = 80
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -109,10 +113,10 @@ func defaultConfig() Config {
|
|||
CredentialsFile: "",
|
||||
CacheDirectory: "",
|
||||
HTTP: HTTPConfig{
|
||||
Timeout: 5,
|
||||
MediaTimeout: 30,
|
||||
Timeout: defaultHTTPTimeout,
|
||||
MediaTimeout: defaultHTTPMediaTimeout,
|
||||
},
|
||||
LineWrapMaxWidth: 80,
|
||||
LineWrapMaxWidth: defaultLineWrapMaxWidth,
|
||||
Integrations: Integrations{
|
||||
Browser: "",
|
||||
Editor: "",
|
||||
|
|
83
internal/config/config_test.go
Normal file
83
internal/config/config_test.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package config_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
||||
)
|
||||
|
||||
func TestConfigFile(t *testing.T) {
|
||||
t.Log("Testing saving and loading the configuration")
|
||||
|
||||
projectDir, err := projectRoot()
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to get the project root directory: %v", err)
|
||||
}
|
||||
|
||||
configDir := filepath.Join(projectDir, "test", "config")
|
||||
|
||||
t.Run("Save the default configuration to file", testSaveDefaultConfigToFile(configDir))
|
||||
t.Run("Load the configuration from file", testLoadConfigFromFile(configDir))
|
||||
|
||||
expectedConfigFile := filepath.Join(configDir, "config.json")
|
||||
if err := os.Remove(expectedConfigFile); err != nil {
|
||||
t.Fatalf(
|
||||
"received an error after trying to clean up the test configuration at %q: %v",
|
||||
expectedConfigFile,
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func testSaveDefaultConfigToFile(configDir string) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
if err := config.SaveDefaultConfigToFile(configDir); err != nil {
|
||||
t.Fatalf("Unable to save the default configuration within %q: %v", configDir, err)
|
||||
}
|
||||
|
||||
fileExists, err := config.FileExists(configDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to determine if the configuration exists within %q: %v", configDir, err)
|
||||
}
|
||||
|
||||
if !fileExists {
|
||||
t.Fatalf("The configuration does not appear to exist within %q", configDir)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testLoadConfigFromFile(configDir string) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
config, err := config.NewConfigFromFile(configDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to load the configuration from file: %v", err)
|
||||
}
|
||||
|
||||
expectedDefaultHTTPTimeout := 5
|
||||
|
||||
if config.HTTP.Timeout != expectedDefaultHTTPTimeout {
|
||||
t.Errorf(
|
||||
"Unexpected HTTP Timeout settings loaded from the configuration: want %d, got %d",
|
||||
expectedDefaultHTTPTimeout,
|
||||
config.HTTP.Timeout,
|
||||
)
|
||||
} else {
|
||||
t.Logf(
|
||||
"Expected HTTP Timeout settings loaded from the configuration: got %d",
|
||||
config.HTTP.Timeout,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func projectRoot() (string, error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to get the current working directory, %w", err)
|
||||
}
|
||||
|
||||
return filepath.Join(cwd, "..", ".."), nil
|
||||
}
|
Loading…
Reference in a new issue