Dan Anglin
d38431d9e8
Summary: If the user hasn't supplied the path to a configuration directory and Enbas fails to find the user's default home configuration directory, Enbas will now return an error message back to the user. Previously Enbas would try to, instead, calculate the configuration directory using the user's current directory. Changes: - Return an error if the user's home configuration directory cannot be found. - Add tests to the internal.config package. - Update the tests in internal.utilities package. PR: #54
83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
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
|
|
}
|