Files
gibidify/config/config_test.go
Ismo Vuorinen 4b8d66c778 feat(tests): more tests and ci action (#14)
* feat(tests): more tests and ci action
* fix(ci): coverage and pr-lint
* fix(ci): renovate rules, permissions, linting, actions
* fix(lint): editorconfig fixes
* fix(lint): kics.config
* fix(lint): formatting, permissions, pre-commit config
* chore(ci): set workflow to use go 1.23, go mod tidy
* chore(ci): fixes and stuff
* chore(ci): disable GO_GOLANGCI_LINT
* chore(ci): pinning, permissions
2025-03-23 19:41:39 +02:00

83 lines
2.2 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
"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 os.RemoveAll(tmpDir)
// Point Viper to the temp directory with no config file.
originalConfigPaths := viper.ConfigFileUsed()
viper.Reset()
viper.AddConfigPath(tmpDir)
LoadConfig()
// Check defaults
defaultSizeLimit := GetFileSizeLimit()
if defaultSizeLimit != 5242880 {
t.Errorf("Expected default file size limit of 5242880, got %d", defaultSizeLimit)
}
ignoredDirs := 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 os.RemoveAll(tmpDir)
// 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)
}
}