mirror of
https://github.com/ivuorinen/gibidify.git
synced 2026-01-26 11:34:03 +00:00
92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
package config_test
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
configpkg "github.com/ivuorinen/gibidify/config"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// TestDefaultConfig verifies that if no config file is found,
|
|
// the default configuration values are correctly set.
|
|
func TestDefaultConfig(t *testing.T) {
|
|
// Create a temporary directory to ensure no config file is present.
|
|
tmpDir, err := os.MkdirTemp("", "gibidify_config_test_default")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp directory: %v", err)
|
|
}
|
|
defer func() {
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
t.Fatalf("cleanup failed: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Point Viper to the temp directory with no config file.
|
|
originalConfigPaths := viper.ConfigFileUsed()
|
|
viper.Reset()
|
|
viper.AddConfigPath(tmpDir)
|
|
configpkg.LoadConfig()
|
|
|
|
// Check defaults
|
|
defaultSizeLimit := configpkg.GetFileSizeLimit()
|
|
if defaultSizeLimit != 5242880 {
|
|
t.Errorf("Expected default file size limit of 5242880, got %d", defaultSizeLimit)
|
|
}
|
|
|
|
ignoredDirs := configpkg.GetIgnoredDirectories()
|
|
if len(ignoredDirs) == 0 {
|
|
t.Errorf("Expected some default ignored directories, got none")
|
|
}
|
|
|
|
// Restore Viper state
|
|
viper.SetConfigFile(originalConfigPaths)
|
|
}
|
|
|
|
// TestLoadConfigFile verifies that when a valid config file is present,
|
|
// viper loads the specified values correctly.
|
|
func TestLoadConfigFile(t *testing.T) {
|
|
tmpDir, err := os.MkdirTemp("", "gibidify_config_test_file")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp directory: %v", err)
|
|
}
|
|
defer func() {
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
t.Fatalf("cleanup failed: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Prepare a minimal config file
|
|
configContent := []byte(`---
|
|
fileSizeLimit: 123456
|
|
ignoreDirectories:
|
|
- "testdir1"
|
|
- "testdir2"
|
|
`)
|
|
|
|
configPath := filepath.Join(tmpDir, "config.yaml")
|
|
if err := os.WriteFile(configPath, configContent, 0644); err != nil {
|
|
t.Fatalf("Failed to write config file: %v", err)
|
|
}
|
|
|
|
// Reset viper and point to the new config path
|
|
viper.Reset()
|
|
viper.AddConfigPath(tmpDir)
|
|
|
|
// Force Viper to read our config file
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
t.Fatalf("Could not read config file: %v", err)
|
|
}
|
|
|
|
// Validate loaded data
|
|
if got := viper.GetInt64("fileSizeLimit"); got != 123456 {
|
|
t.Errorf("Expected fileSizeLimit=123456, got %d", got)
|
|
}
|
|
|
|
ignored := viper.GetStringSlice("ignoreDirectories")
|
|
if len(ignored) != 2 || ignored[0] != "testdir1" || ignored[1] != "testdir2" {
|
|
t.Errorf("Expected [\"testdir1\", \"testdir2\"], got %v", ignored)
|
|
}
|
|
}
|