
- Add comprehensive Git workflow automation tools - Include branch management utilities - Add commit helpers with conventional commit support - Implement GitHub integration for PR management - Add configuration management system - Include comprehensive test coverage - Add professional documentation and examples
267 lines
6.1 KiB
Go
267 lines
6.1 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func setupTestConfig(t *testing.T) (string, func()) {
|
|
t.Helper()
|
|
|
|
// Create a temporary directory for the test config
|
|
tempDir, err := os.MkdirTemp("", "config-test-")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp directory: %v", err)
|
|
}
|
|
|
|
// Get the original config file path
|
|
originalConfigFile := configFile
|
|
|
|
// Set the config file path to the temp directory
|
|
configFile = filepath.Join(tempDir, "config.yaml")
|
|
|
|
// Create a cleanup function
|
|
cleanup := func() {
|
|
configFile = originalConfigFile
|
|
os.RemoveAll(tempDir)
|
|
}
|
|
|
|
return tempDir, cleanup
|
|
}
|
|
|
|
func TestInit(t *testing.T) {
|
|
// Save original values
|
|
originalConfigFile := configFile
|
|
originalGlobalConfig := GlobalConfig
|
|
|
|
// Reset viper to ensure a clean state
|
|
ResetViper()
|
|
|
|
// Setup test config
|
|
_, cleanup := setupTestConfig(t)
|
|
defer cleanup()
|
|
|
|
// Reset global config
|
|
GlobalConfig = &Config{}
|
|
|
|
// Initialize the config
|
|
err := Init()
|
|
if err != nil {
|
|
t.Fatalf("Init failed: %v", err)
|
|
}
|
|
|
|
// Check default values
|
|
if GlobalConfig.DefaultRemote != "origin" {
|
|
t.Errorf("Expected default remote to be 'origin', got '%s'", GlobalConfig.DefaultRemote)
|
|
}
|
|
|
|
if GlobalConfig.DefaultBranch != "main" {
|
|
t.Errorf("Expected default branch to be 'main', got '%s'", GlobalConfig.DefaultBranch)
|
|
}
|
|
|
|
if GlobalConfig.GitHubURL != "https://api.github.com" {
|
|
t.Errorf("Expected GitHub URL to be 'https://api.github.com', got '%s'", GlobalConfig.GitHubURL)
|
|
}
|
|
|
|
if GlobalConfig.GitHubToken != "" {
|
|
t.Errorf("Expected GitHub token to be empty, got '%s'", GlobalConfig.GitHubToken)
|
|
}
|
|
|
|
// Restore original values
|
|
configFile = originalConfigFile
|
|
GlobalConfig = originalGlobalConfig
|
|
}
|
|
|
|
func TestSave(t *testing.T) {
|
|
// Save original values
|
|
originalConfigFile := configFile
|
|
originalGlobalConfig := GlobalConfig
|
|
|
|
// Reset viper to ensure a clean state
|
|
ResetViper()
|
|
|
|
// Setup test config
|
|
_, cleanup := setupTestConfig(t)
|
|
defer cleanup()
|
|
|
|
// Reset global config
|
|
GlobalConfig = &Config{}
|
|
|
|
// Initialize the config
|
|
if err := Init(); err != nil {
|
|
t.Fatalf("Init failed: %v", err)
|
|
}
|
|
|
|
// Set some values
|
|
Set("github_token", "test-token")
|
|
Set("default_branch", "develop")
|
|
Set("default_remote", "upstream")
|
|
|
|
// Save the config
|
|
err := Save()
|
|
if err != nil {
|
|
t.Fatalf("Save failed: %v", err)
|
|
}
|
|
|
|
// Check that the config file exists
|
|
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
|
t.Error("Config file does not exist")
|
|
}
|
|
|
|
// Reset viper again
|
|
ResetViper()
|
|
|
|
// Reset global config
|
|
GlobalConfig = &Config{}
|
|
|
|
// Re-initialize the config to load from file
|
|
if err := Init(); err != nil {
|
|
t.Fatalf("Init failed: %v", err)
|
|
}
|
|
|
|
// Check values
|
|
if GlobalConfig.GitHubToken != "test-token" {
|
|
t.Errorf("Expected GitHub token to be 'test-token', got '%s'", GlobalConfig.GitHubToken)
|
|
}
|
|
|
|
if GlobalConfig.DefaultBranch != "develop" {
|
|
t.Errorf("Expected default branch to be 'develop', got '%s'", GlobalConfig.DefaultBranch)
|
|
}
|
|
|
|
if GlobalConfig.DefaultRemote != "upstream" {
|
|
t.Errorf("Expected default remote to be 'upstream', got '%s'", GlobalConfig.DefaultRemote)
|
|
}
|
|
|
|
// Restore original values
|
|
configFile = originalConfigFile
|
|
GlobalConfig = originalGlobalConfig
|
|
}
|
|
|
|
func TestSet(t *testing.T) {
|
|
// Save original values
|
|
originalConfigFile := configFile
|
|
originalGlobalConfig := GlobalConfig
|
|
|
|
// Reset viper to ensure a clean state
|
|
ResetViper()
|
|
|
|
// Setup test config
|
|
_, cleanup := setupTestConfig(t)
|
|
defer cleanup()
|
|
|
|
// Reset global config
|
|
GlobalConfig = &Config{}
|
|
|
|
// Initialize the config
|
|
if err := Init(); err != nil {
|
|
t.Fatalf("Init failed: %v", err)
|
|
}
|
|
|
|
// Set a GitHub token
|
|
Set("github_token", "test-token")
|
|
|
|
// Check the value
|
|
if GlobalConfig.GitHubToken != "test-token" {
|
|
t.Errorf("Expected GitHub token to be 'test-token', got '%s'", GlobalConfig.GitHubToken)
|
|
}
|
|
|
|
// Set a default branch
|
|
Set("default_branch", "develop")
|
|
|
|
// Check the value
|
|
if GlobalConfig.DefaultBranch != "develop" {
|
|
t.Errorf("Expected default branch to be 'develop', got '%s'", GlobalConfig.DefaultBranch)
|
|
}
|
|
|
|
// Set a default remote
|
|
Set("default_remote", "upstream")
|
|
|
|
// Check the value
|
|
if GlobalConfig.DefaultRemote != "upstream" {
|
|
t.Errorf("Expected default remote to be 'upstream', got '%s'", GlobalConfig.DefaultRemote)
|
|
}
|
|
|
|
// Restore original values
|
|
configFile = originalConfigFile
|
|
GlobalConfig = originalGlobalConfig
|
|
}
|
|
|
|
func TestGet(t *testing.T) {
|
|
// Save original values
|
|
originalConfigFile := configFile
|
|
originalGlobalConfig := GlobalConfig
|
|
|
|
// Reset viper to ensure a clean state
|
|
ResetViper()
|
|
|
|
// Setup test config
|
|
_, cleanup := setupTestConfig(t)
|
|
defer cleanup()
|
|
|
|
// Reset global config
|
|
GlobalConfig = &Config{}
|
|
|
|
// Initialize the config
|
|
if err := Init(); err != nil {
|
|
t.Fatalf("Init failed: %v", err)
|
|
}
|
|
|
|
// Set some values
|
|
Set("github_token", "test-token")
|
|
Set("default_branch", "develop")
|
|
Set("default_remote", "upstream")
|
|
|
|
// Get the values
|
|
token := Get("github_token")
|
|
if token != "test-token" {
|
|
t.Errorf("Expected GitHub token to be 'test-token', got '%v'", token)
|
|
}
|
|
|
|
branch := Get("default_branch")
|
|
if branch != "develop" {
|
|
t.Errorf("Expected default branch to be 'develop', got '%v'", branch)
|
|
}
|
|
|
|
remote := Get("default_remote")
|
|
if remote != "upstream" {
|
|
t.Errorf("Expected default remote to be 'upstream', got '%v'", remote)
|
|
}
|
|
|
|
// Restore original values
|
|
configFile = originalConfigFile
|
|
GlobalConfig = originalGlobalConfig
|
|
}
|
|
|
|
func TestGetConfigFile(t *testing.T) {
|
|
// Save original values
|
|
originalConfigFile := configFile
|
|
originalGlobalConfig := GlobalConfig
|
|
|
|
// Reset viper to ensure a clean state
|
|
ResetViper()
|
|
|
|
// Setup test config
|
|
_, cleanup := setupTestConfig(t)
|
|
defer cleanup()
|
|
|
|
// Reset global config
|
|
GlobalConfig = &Config{}
|
|
|
|
// Initialize the config
|
|
if err := Init(); err != nil {
|
|
t.Fatalf("Init failed: %v", err)
|
|
}
|
|
|
|
// Get the config file path
|
|
configFilePath := GetConfigFile()
|
|
|
|
// Check that it matches the expected path
|
|
if configFilePath != configFile {
|
|
t.Errorf("Expected config file path to be '%s', got '%s'", configFile, configFilePath)
|
|
}
|
|
|
|
// Restore original values
|
|
configFile = originalConfigFile
|
|
GlobalConfig = originalGlobalConfig
|
|
} |