From 4c879936677e72d2d48999ec12bd9210284ba1a1 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 18 Aug 2024 19:40:12 +0100 Subject: [PATCH] added test for config saving and loading --- internal/config/config.go | 12 +++-- internal/config/config_test.go | 83 ++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 internal/config/config_test.go diff --git a/internal/config/config.go b/internal/config/config.go index b74cad9..8946aa0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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: "", diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..795a060 --- /dev/null +++ b/internal/config/config_test.go @@ -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 +}