From 91d99d180679dff5cc1f26f6ab0942574d5c9d9c Mon Sep 17 00:00:00 2001 From: Harsh4902 Date: Thu, 1 May 2025 21:52:49 +0530 Subject: [PATCH] fixes #132 Add unit tests for config.go Signed-off-by: Harsh4902 --- pkg/config/config_test.go | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 pkg/config/config_test.go diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go new file mode 100644 index 0000000..1f53504 --- /dev/null +++ b/pkg/config/config_test.go @@ -0,0 +1,57 @@ +package config + +import ( + "os" + "path/filepath" + "testing" +) + +func TestDefaultConfig(t *testing.T) { + cfg := defaultConfig() + if cfg.Instance.Name != "microcks" { + t.Errorf("Expected default name 'microcks', got %s", cfg.Instance.Name) + } + if cfg.Instance.Driver != "docker" { + t.Errorf("Expected default driver 'docker', got %s", cfg.Instance.Driver) + } +} + +func TestSaveAndLoadConfig(t *testing.T) { + tmpDir := t.TempDir() + cfgPath := filepath.Join(tmpDir, "test-config.yaml") + + original := defaultConfig() + original.Instance.Image = "test-image" + + err := SaveConfig(cfgPath, original) + if err != nil { + t.Fatalf("SaveConfig failed: %v", err) + } + + loaded, err := LoadConfig(cfgPath) + if err != nil { + t.Fatalf("LoadConfig failed: %v", err) + } + + if loaded.Instance.Image != "test-image" { + t.Errorf("Expected image 'test-image', got %s", loaded.Instance.Image) + } +} + +func TestEnsureConfigCreatesFile(t *testing.T) { + tmpDir := t.TempDir() + cfgPath := filepath.Join(tmpDir, "new-config.yaml") + + cfg, err := EnsureConfig(cfgPath) + if err != nil { + t.Fatalf("EnsureConfig failed: %v", err) + } + + if cfg.Instance.Name != "microcks" { + t.Errorf("Expected default name 'microcks', got %s", cfg.Instance.Name) + } + + if _, err := os.Stat(cfgPath); os.IsNotExist(err) { + t.Errorf("Config file was not created at %s", cfgPath) + } +}