|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package linter |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func writeYAML(t *testing.T, content string) string { |
| 12 | + t.Helper() |
| 13 | + dir := t.TempDir() |
| 14 | + path := filepath.Join(dir, "lint-config.yaml") |
| 15 | + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 16 | + t.Fatalf("write YAML: %v", err) |
| 17 | + } |
| 18 | + return path |
| 19 | +} |
| 20 | + |
| 21 | +func TestLoadConfig_ExcludedModules(t *testing.T) { |
| 22 | + path := writeYAML(t, ` |
| 23 | +excludeModules: |
| 24 | + - Administration |
| 25 | + - Anonymous |
| 26 | +`) |
| 27 | + cfg, err := LoadConfig(path) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("LoadConfig: %v", err) |
| 30 | + } |
| 31 | + if len(cfg.ExcludeModules) != 2 { |
| 32 | + t.Fatalf("want 2 excluded modules, got %d", len(cfg.ExcludeModules)) |
| 33 | + } |
| 34 | + if cfg.ExcludeModules[0] != "Administration" || cfg.ExcludeModules[1] != "Anonymous" { |
| 35 | + t.Errorf("unexpected modules: %v", cfg.ExcludeModules) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestLoadConfig_RuleEnabled(t *testing.T) { |
| 40 | + path := writeYAML(t, ` |
| 41 | +rules: |
| 42 | + MPR001: |
| 43 | + enabled: false |
| 44 | +`) |
| 45 | + cfg, err := LoadConfig(path) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("LoadConfig: %v", err) |
| 48 | + } |
| 49 | + rule, ok := cfg.Rules["MPR001"] |
| 50 | + if !ok { |
| 51 | + t.Fatal("MPR001 rule not found") |
| 52 | + } |
| 53 | + if rule.Enabled == nil || *rule.Enabled != false { |
| 54 | + t.Errorf("expected enabled=false, got %v", rule.Enabled) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestLoadConfig_RuleSeverity(t *testing.T) { |
| 59 | + path := writeYAML(t, ` |
| 60 | +rules: |
| 61 | + PH009: |
| 62 | + severity: hint |
| 63 | +`) |
| 64 | + cfg, err := LoadConfig(path) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("LoadConfig: %v", err) |
| 67 | + } |
| 68 | + if cfg.Rules["PH009"].Severity != "hint" { |
| 69 | + t.Errorf("expected severity hint, got %q", cfg.Rules["PH009"].Severity) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestLoadConfig_RuleOptions(t *testing.T) { |
| 74 | + path := writeYAML(t, ` |
| 75 | +rules: |
| 76 | + MPR003: |
| 77 | + options: |
| 78 | + max_entities: 20 |
| 79 | +`) |
| 80 | + cfg, err := LoadConfig(path) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("LoadConfig: %v", err) |
| 83 | + } |
| 84 | + opts := cfg.Rules["MPR003"].Options |
| 85 | + if opts == nil { |
| 86 | + t.Fatal("expected options map, got nil") |
| 87 | + } |
| 88 | + // YAML numbers unmarshal as int when they fit. |
| 89 | + v, ok := opts["max_entities"] |
| 90 | + if !ok { |
| 91 | + t.Fatal("max_entities not found in options") |
| 92 | + } |
| 93 | + switch n := v.(type) { |
| 94 | + case int: |
| 95 | + if n != 20 { |
| 96 | + t.Errorf("max_entities = %d, want 20", n) |
| 97 | + } |
| 98 | + case float64: |
| 99 | + if n != 20 { |
| 100 | + t.Errorf("max_entities = %v, want 20", n) |
| 101 | + } |
| 102 | + default: |
| 103 | + t.Errorf("unexpected type %T for max_entities", v) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestLoadConfig_MissingFileReturnsDefault(t *testing.T) { |
| 108 | + cfg, err := LoadConfig("/nonexistent/lint-config.yaml") |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("expected no error for missing file, got %v", err) |
| 111 | + } |
| 112 | + if len(cfg.ExcludeModules) != 0 || len(cfg.Rules) != 0 { |
| 113 | + t.Errorf("expected empty default config, got %+v", cfg) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestApplyConfig_DisablesRule(t *testing.T) { |
| 118 | + path := writeYAML(t, ` |
| 119 | +rules: |
| 120 | + MPR001: |
| 121 | + enabled: false |
| 122 | +`) |
| 123 | + cfg, _ := LoadConfig(path) |
| 124 | + |
| 125 | + lint := New(nil) |
| 126 | + lint.AddRule(&stubRule{"MPR001"}) |
| 127 | + lint.AddRule(&stubRule{"MPR002"}) |
| 128 | + cfg.ApplyConfig(lint) |
| 129 | + |
| 130 | + configs := lint.configs |
| 131 | + if c, ok := configs["MPR001"]; !ok || c.Enabled { |
| 132 | + t.Errorf("MPR001 should be disabled, got %+v", c) |
| 133 | + } |
| 134 | + if c, ok := configs["MPR002"]; ok && !c.Enabled { |
| 135 | + t.Errorf("MPR002 should not be disabled, got %+v", c) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestApplyConfig_OverridesSeverity(t *testing.T) { |
| 140 | + path := writeYAML(t, ` |
| 141 | +rules: |
| 142 | + MPR001: |
| 143 | + severity: error |
| 144 | +`) |
| 145 | + cfg, _ := LoadConfig(path) |
| 146 | + |
| 147 | + lint := New(nil) |
| 148 | + lint.AddRule(&stubRule{"MPR001"}) |
| 149 | + cfg.ApplyConfig(lint) |
| 150 | + |
| 151 | + if c := lint.configs["MPR001"]; c.Severity != SeverityError { |
| 152 | + t.Errorf("expected SeverityError, got %v", c.Severity) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestApplyConfig_PassesOptions(t *testing.T) { |
| 157 | + path := writeYAML(t, ` |
| 158 | +rules: |
| 159 | + MPR003: |
| 160 | + options: |
| 161 | + max_entities: 25 |
| 162 | +`) |
| 163 | + cfg, _ := LoadConfig(path) |
| 164 | + |
| 165 | + lint := New(nil) |
| 166 | + lint.AddRule(&stubRule{"MPR003"}) |
| 167 | + cfg.ApplyConfig(lint) |
| 168 | + |
| 169 | + opts := lint.configs["MPR003"].Options |
| 170 | + if opts == nil { |
| 171 | + t.Fatal("expected options in RuleConfig, got nil") |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestFindConfigFile_LocatesRootFile(t *testing.T) { |
| 176 | + dir := t.TempDir() |
| 177 | + path := filepath.Join(dir, "lint-config.yaml") |
| 178 | + if err := os.WriteFile(path, []byte(""), 0o644); err != nil { |
| 179 | + t.Fatalf("write: %v", err) |
| 180 | + } |
| 181 | + got := FindConfigFile(dir) |
| 182 | + if got != path { |
| 183 | + t.Errorf("FindConfigFile = %q, want %q", got, path) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func TestFindConfigFile_LocatesDotClaudeFile(t *testing.T) { |
| 188 | + dir := t.TempDir() |
| 189 | + sub := filepath.Join(dir, ".claude") |
| 190 | + if err := os.MkdirAll(sub, 0o755); err != nil { |
| 191 | + t.Fatalf("mkdir: %v", err) |
| 192 | + } |
| 193 | + path := filepath.Join(sub, "lint-config.yaml") |
| 194 | + if err := os.WriteFile(path, []byte(""), 0o644); err != nil { |
| 195 | + t.Fatalf("write: %v", err) |
| 196 | + } |
| 197 | + got := FindConfigFile(dir) |
| 198 | + if got != path { |
| 199 | + t.Errorf("FindConfigFile = %q, want %q", got, path) |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +func TestFindConfigFile_ReturnsEmptyWhenAbsent(t *testing.T) { |
| 204 | + dir := t.TempDir() |
| 205 | + if got := FindConfigFile(dir); got != "" { |
| 206 | + t.Errorf("expected empty string, got %q", got) |
| 207 | + } |
| 208 | +} |
0 commit comments