v0.2.0 92ac699

config_test.go RawHistoryFolder

1package config
2
3import (
4	"os"
5	"path/filepath"
6	"testing"
7)
8
9func TestLoad(t *testing.T) {
10	tests := []struct {
11		name    string
12		yaml    string
13		wantErr bool
14		check   func(t *testing.T, cfg *Config)
15	}{
16		{
17			name: "valid config",
18			yaml: `version: 1
19components:
20  core:
21    paths: [internal/domain/**]
22    role: core
23  adapters:
24    paths: [internal/infrastructure/**]
25    role: adapter
26rules:
27  no-adapter-imports-in-core: error
28allow:
29  - rule: no-local-fakes-for-ports
30    path: internal/foo_test.go
31    reason: stateful fake
32`,
33			check: func(t *testing.T, cfg *Config) {
34				t.Helper()
35				if cfg.Version != 1 {
36					t.Fatalf("Version = %d", cfg.Version)
37				}
38				if cfg.Components["core"].Role != RoleCore {
39					t.Fatalf("core role = %q", cfg.Components["core"].Role)
40				}
41				if cfg.Severity("no-adapter-imports-in-core") != SeverityError {
42					t.Fatalf("severity not parsed")
43				}
44				if !cfg.IsAllowed("no-local-fakes-for-ports", "internal/foo_test.go") {
45					t.Fatalf("allow entry not matched")
46				}
47			},
48		},
49		{
50			name: "invalid role",
51			yaml: `version: 1
52components:
53  bad:
54    paths: [internal/**]
55    role: wat
56`,
57			wantErr: true,
58		},
59	}
60
61	for _, tt := range tests {
62		t.Run(tt.name, func(t *testing.T) {
63			dir := t.TempDir()
64			path := filepath.Join(dir, ".hexcheck.yaml")
65			if err := os.WriteFile(path, []byte(tt.yaml), 0o600); err != nil {
66				t.Fatal(err)
67			}
68			cfg, err := Load(path)
69			if tt.wantErr {
70				if err == nil {
71					t.Fatal("expected error")
72				}
73				return
74			}
75			if err != nil {
76				t.Fatal(err)
77			}
78			if tt.check != nil {
79				tt.check(t, cfg)
80			}
81		})
82	}
83}
84
85func TestLoadRejectsExplicitZeroVersion(t *testing.T) {
86	tests := []struct {
87		name string
88		yaml string
89	}{
90		{name: "zero", yaml: "version: 0\n"},
91		{name: "positive zero", yaml: "version: +0\n"},
92		{name: "double zero", yaml: "version: 00\n"},
93	}
94	for _, tt := range tests {
95		t.Run(tt.name, func(t *testing.T) {
96			dir := t.TempDir()
97			path := filepath.Join(dir, ".hexcheck.yaml")
98			data := []byte(tt.yaml + `components:
99  adapters:
100    paths: [internal/infrastructure/**]
101    role: adapter
102`)
103			if err := os.WriteFile(path, data, 0o600); err != nil {
104				t.Fatal(err)
105			}
106			if _, err := Load(path); err == nil {
107				t.Fatal("expected explicit zero version to be rejected")
108			}
109		})
110	}
111}
112
113func TestLoadHonorsExplicitExcludeTestFilesFalse(t *testing.T) {
114	dir := t.TempDir()
115	path := filepath.Join(dir, ".hexcheck.yaml")
116	data := []byte(`version: 1
117components:
118  adapters:
119    paths: [internal/infrastructure/**]
120    role: adapter
121heuristics:
122  excludeTestFiles: false
123`)
124	if err := os.WriteFile(path, data, 0o600); err != nil {
125		t.Fatal(err)
126	}
127	cfg, err := Load(path)
128	if err != nil {
129		t.Fatal(err)
130	}
131	if cfg.Heuristics.ExcludeTestFiles == nil || *cfg.Heuristics.ExcludeTestFiles {
132		t.Fatal("excludeTestFiles=false was not preserved")
133	}
134	if !filepath.IsAbs(cfg.Root) {
135		t.Fatalf("Root = %q, want absolute path", cfg.Root)
136	}
137}
138
139func TestValidateRejectsInvalidHeuristicKnobs(t *testing.T) {
140	tests := []struct {
141		name   string
142		mutate func(*Config)
143	}{
144		{name: "negative threshold", mutate: func(c *Config) { c.Heuristics.BusinessLogicThreshold = intPtr(-1) }},
145		{name: "negative strong", mutate: func(c *Config) { c.Heuristics.BusinessLogicMinStrongSignals = intPtr(-1) }},
146		{name: "negative weak", mutate: func(c *Config) { c.Heuristics.BusinessLogicMinWeakSignals = intPtr(-1) }},
147		{name: "zero max nodes", mutate: func(c *Config) { c.Heuristics.BusinessLogicMaxFunctionNodes = intPtr(0) }},
148		{name: "zero max diagnostics", mutate: func(c *Config) { c.Heuristics.BusinessLogicMaxDiagnosticsPerPackage = intPtr(0) }},
149		{name: "invalid business logic mode", mutate: func(c *Config) { c.Heuristics.BusinessLogicMode = "loud" }},
150		{name: "invalid business logic min confidence", mutate: func(c *Config) { c.Heuristics.BusinessLogicMinConfidence = "sure" }},
151	}
152	for _, tt := range tests {
153		t.Run(tt.name, func(t *testing.T) {
154			cfg := &Config{Version: 1, Components: map[string]Component{"adapters": {Role: RoleAdapter, Paths: []string{"internal/**"}}}}
155			cfg.applyDefaults()
156			tt.mutate(cfg)
157			if err := cfg.Validate(); err == nil {
158				t.Fatal("expected validation error")
159			}
160		})
161	}
162}
163
164func TestComponentForPathPrecedence(t *testing.T) {
165	cfg := &Config{Components: map[string]Component{
166		"all":     {Role: RoleCore, Paths: []string{"internal/**"}},
167		"adapter": {Role: RoleAdapter, Paths: []string{"internal/infrastructure/**"}},
168		"ignore":  {Role: RoleIgnore, Paths: []string{"internal/infrastructure/generated/**"}},
169	}}
170	cfg.applyDefaults()
171
172	tests := []struct {
173		name string
174		path string
175		want Role
176	}{
177		{name: "specific adapter beats broad core", path: "internal/infrastructure/postgres/repo.go", want: RoleAdapter},
178		{name: "ignore beats adapter", path: "internal/infrastructure/generated/foo.go", want: RoleIgnore},
179	}
180	for _, tt := range tests {
181		t.Run(tt.name, func(t *testing.T) {
182			match, ok := cfg.ComponentForPath(tt.path)
183			if !ok || match.Role != tt.want {
184				t.Fatalf("match = %#v, %v; want role %q", match, ok, tt.want)
185			}
186		})
187	}
188}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 5

↑↓ moveenter openesc close