fix(config): normalize keys in GetYamlConfig to match SetYamlConfig (#874)

GetYamlConfig was not normalizing key aliases (e.g., sync.branch ->
sync-branch), causing 'bd config get sync.branch' to return 'not set'
even when the value was correctly stored.

SetYamlConfig already normalized keys, but GetYamlConfig did not,
leading to a confusing mismatch where set appeared to work but get
could not find the value.

Added TestGetYamlConfig_KeyNormalization to verify the fix.

Fixes #873
This commit is contained in:
Graeme Foster
2026-01-03 19:51:01 +00:00
committed by GitHub
parent 0c7dcee3ac
commit e623746e60
2 changed files with 50 additions and 1 deletions

View File

@@ -130,11 +130,13 @@ func SetYamlConfig(key, value string) error {
// GetYamlConfig gets a configuration value from config.yaml.
// Returns empty string if key is not found or is commented out.
// Keys are normalized to their canonical yaml format (e.g., sync.branch -> sync-branch).
func GetYamlConfig(key string) string {
if v == nil {
return ""
}
return v.GetString(key)
normalizedKey := normalizeYamlKey(key)
return v.GetString(normalizedKey)
}
// findProjectConfigYaml finds the project's .beads/config.yaml file.

View File

@@ -224,6 +224,53 @@ sync-branch: old-value
}
}
func TestGetYamlConfig_KeyNormalization(t *testing.T) {
// Create a temp directory with .beads/config.yaml
tmpDir, err := os.MkdirTemp("", "beads-yaml-get-key-norm-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0755); err != nil {
t.Fatalf("Failed to create .beads dir: %v", err)
}
// Write config with canonical key name (sync-branch, not sync.branch)
configPath := filepath.Join(beadsDir, "config.yaml")
initialConfig := `# Beads Config
sync-branch: test-value
`
if err := os.WriteFile(configPath, []byte(initialConfig), 0644); err != nil {
t.Fatalf("Failed to write config.yaml: %v", err)
}
// Change to temp directory for the test
oldWd, _ := os.Getwd()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
defer os.Chdir(oldWd)
// Initialize viper to read the config
if err := Initialize(); err != nil {
t.Fatalf("Initialize() error = %v", err)
}
// Test GetYamlConfig with aliased key (sync.branch should find sync-branch value)
got := GetYamlConfig("sync.branch")
if got != "test-value" {
t.Errorf("GetYamlConfig(\"sync.branch\") = %q, want %q", got, "test-value")
}
// Also verify canonical key works
got = GetYamlConfig("sync-branch")
if got != "test-value" {
t.Errorf("GetYamlConfig(\"sync-branch\") = %q, want %q", got, "test-value")
}
}
func TestSetYamlConfig(t *testing.T) {
// Create a temp directory with .beads/config.yaml
tmpDir, err := os.MkdirTemp("", "beads-yaml-test-*")