Skip to content

Commit b98fea1

Browse files
author
Test User
committed
fix: handle unchecked error returns in test files
Add explicit error handling for os.WriteFile, os.MkdirAll, os.Chdir, os.Chmod, os.Symlink, io.Copy, and formatter.Format calls in test files to satisfy errcheck linter. Also removes unused fields and fixes empty branch warnings from staticcheck.
1 parent 32fe8dc commit b98fea1

8 files changed

Lines changed: 183 additions & 185 deletions

File tree

internal/cli/generic_linter_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ type mockLinter struct {
211211
specificErrs []cue.ValidationError
212212
preValidErrs []cue.ValidationError
213213
bestPracErrs []cue.ValidationError
214-
score int
215-
improvements []ImprovementRecommendation
216214
postProcessed bool
217215
}
218216

@@ -316,8 +314,9 @@ func TestLintComponent(t *testing.T) {
316314
t.Errorf("lintComponent() errors = %d, want %d", len(result.Errors), tt.wantErrCount)
317315
}
318316

319-
if tt.linter.postProcessed {
320-
// PostProcess was called
317+
// Verify PostProcess was called for successful parsing
318+
if tt.wantSuccess && !tt.linter.postProcessed {
319+
t.Error("PostProcess was not called")
321320
}
322321
})
323322
}
@@ -329,7 +328,6 @@ func TestValidateAllowedToolsShared(t *testing.T) {
329328
"allowed-tools": "Read, Write",
330329
}
331330
errors := ValidateAllowedToolsShared(data, "test.md", "")
332-
if errors == nil {
333-
// Function exists and returns
334-
}
331+
// Verify function exists and returns without panic
332+
_ = errors
335333
}

internal/cli/lintcontext_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestLinterContextAutoDiscoverRoot(t *testing.T) {
193193
if err != nil {
194194
t.Fatal(err)
195195
}
196-
defer os.Chdir(oldWd)
196+
defer func() { _ = os.Chdir(oldWd) }()
197197

198198
if err := os.Chdir(tmpDir); err != nil {
199199
t.Fatal(err)

internal/cli/singlefile_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ func TestFindProjectRootForFileEdgeCases(t *testing.T) {
473473
name: "file in .claude/agents",
474474
setup: func() string {
475475
claudeDir := filepath.Join(tmpDir, ".claude", "agents")
476-
os.MkdirAll(claudeDir, 0755)
476+
_ = os.MkdirAll(claudeDir, 0755)
477477
return filepath.Join(claudeDir, "test.md")
478478
},
479479
wantContain: ".claude",
@@ -482,7 +482,7 @@ func TestFindProjectRootForFileEdgeCases(t *testing.T) {
482482
name: "file in agents/",
483483
setup: func() string {
484484
agentsDir := filepath.Join(tmpDir, "subproject", "agents")
485-
os.MkdirAll(agentsDir, 0755)
485+
_ = os.MkdirAll(agentsDir, 0755)
486486
return filepath.Join(agentsDir, "test.md")
487487
},
488488
wantContain: "subproject",
@@ -491,7 +491,7 @@ func TestFindProjectRootForFileEdgeCases(t *testing.T) {
491491
name: "file in commands/",
492492
setup: func() string {
493493
commandsDir := filepath.Join(tmpDir, "proj", "commands")
494-
os.MkdirAll(commandsDir, 0755)
494+
_ = os.MkdirAll(commandsDir, 0755)
495495
return filepath.Join(commandsDir, "test.md")
496496
},
497497
wantContain: "proj",
@@ -500,7 +500,7 @@ func TestFindProjectRootForFileEdgeCases(t *testing.T) {
500500
name: "file in skills/",
501501
setup: func() string {
502502
skillsDir := filepath.Join(tmpDir, "myproject", "skills", "test")
503-
os.MkdirAll(skillsDir, 0755)
503+
_ = os.MkdirAll(skillsDir, 0755)
504504
return filepath.Join(skillsDir, "SKILL.md")
505505
},
506506
wantContain: "myproject",
@@ -509,7 +509,7 @@ func TestFindProjectRootForFileEdgeCases(t *testing.T) {
509509
name: "fallback to parent directory",
510510
setup: func() string {
511511
randomDir := filepath.Join(tmpDir, "random")
512-
os.MkdirAll(randomDir, 0755)
512+
_ = os.MkdirAll(randomDir, 0755)
513513
return filepath.Join(randomDir, "test.md")
514514
},
515515
wantContain: "random",

internal/config/config_test.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestLoadConfigDefaults(t *testing.T) {
3737
require.NoError(t, err)
3838
require.NoError(t, os.Chdir(tmpDir))
3939
t.Cleanup(func() {
40-
os.Chdir(oldWd)
40+
_ = os.Chdir(oldWd)
4141
})
4242

4343
config, err := LoadConfig("")
@@ -102,7 +102,7 @@ func TestLoadConfigFromJSON(t *testing.T) {
102102
require.NoError(t, err)
103103
require.NoError(t, os.Chdir(tmpDir))
104104
t.Cleanup(func() {
105-
os.Chdir(oldWd)
105+
_ = os.Chdir(oldWd)
106106
})
107107

108108
config, err := LoadConfig("")
@@ -166,7 +166,7 @@ schemas:
166166
require.NoError(t, err)
167167
require.NoError(t, os.Chdir(tmpDir))
168168
t.Cleanup(func() {
169-
os.Chdir(oldWd)
169+
_ = os.Chdir(oldWd)
170170
})
171171

172172
config, err := LoadConfig("")
@@ -210,7 +210,7 @@ output: report.json
210210
require.NoError(t, err)
211211
require.NoError(t, os.Chdir(tmpDir))
212212
t.Cleanup(func() {
213-
os.Chdir(oldWd)
213+
_ = os.Chdir(oldWd)
214214
})
215215

216216
config, err := LoadConfig("")
@@ -238,7 +238,7 @@ func TestLoadConfigRootPathOverride(t *testing.T) {
238238
require.NoError(t, err)
239239
require.NoError(t, os.Chdir(tmpDir))
240240
t.Cleanup(func() {
241-
os.Chdir(oldWd)
241+
_ = os.Chdir(oldWd)
242242
})
243243

244244
// Load with override
@@ -274,7 +274,7 @@ func TestLoadConfigEnvironmentVariables(t *testing.T) {
274274
require.NoError(t, err)
275275
require.NoError(t, os.Chdir(tmpDir))
276276
t.Cleanup(func() {
277-
os.Chdir(oldWd)
277+
_ = os.Chdir(oldWd)
278278
})
279279

280280
config, err := LoadConfig("")
@@ -298,14 +298,14 @@ func TestLoadConfigConfigFilePriority(t *testing.T) {
298298
// Create multiple config files
299299
jsonConfig := map[string]interface{}{"root": "/json/root"}
300300
jsonData, _ := json.MarshalIndent(jsonConfig, "", " ")
301-
os.WriteFile(filepath.Join(tmpDir, ".cclintrc.json"), jsonData, 0644)
301+
_ = os.WriteFile(filepath.Join(tmpDir, ".cclintrc.json"), jsonData, 0644)
302302

303303
yamlContent := "root: /yaml/root\n"
304-
os.WriteFile(filepath.Join(tmpDir, ".cclintrc.yaml"), []byte(yamlContent), 0644)
304+
_ = os.WriteFile(filepath.Join(tmpDir, ".cclintrc.yaml"), []byte(yamlContent), 0644)
305305

306306
oldWd, _ := os.Getwd()
307-
os.Chdir(tmpDir)
308-
defer os.Chdir(oldWd)
307+
_ = os.Chdir(tmpDir)
308+
defer func() { _ = os.Chdir(oldWd) }()
309309

310310
config, err := LoadConfig("")
311311
require.NoError(t, err)
@@ -500,7 +500,7 @@ func TestSaveConfigInvalidPath(t *testing.T) {
500500
// Try to save to an invalid path (file as directory)
501501
tmpDir := setupTestDir(t)
502502
filePath := filepath.Join(tmpDir, "file")
503-
os.WriteFile(filePath, []byte("test"), 0644)
503+
_ = os.WriteFile(filePath, []byte("test"), 0644)
504504

505505
invalidPath := filepath.Join(filePath, "config.json")
506506
err := SaveConfig(config, invalidPath)
@@ -518,8 +518,8 @@ func TestLoadConfigUnmarshalError(t *testing.T) {
518518
require.NoError(t, os.WriteFile(configPath, []byte(invalidJSON), 0644))
519519

520520
oldWd, _ := os.Getwd()
521-
os.Chdir(tmpDir)
522-
defer os.Chdir(oldWd)
521+
_ = os.Chdir(tmpDir)
522+
defer func() { _ = os.Chdir(oldWd) }()
523523

524524
config, err := LoadConfig("")
525525
assert.Error(t, err)
@@ -539,11 +539,11 @@ func TestLoadConfigValidationError(t *testing.T) {
539539

540540
configPath := filepath.Join(tmpDir, ".cclintrc.json")
541541
jsonData, _ := json.MarshalIndent(configData, "", " ")
542-
os.WriteFile(configPath, jsonData, 0644)
542+
_ = os.WriteFile(configPath, jsonData, 0644)
543543

544544
oldWd, _ := os.Getwd()
545-
os.Chdir(tmpDir)
546-
defer os.Chdir(oldWd)
545+
_ = os.Chdir(tmpDir)
546+
defer func() { _ = os.Chdir(oldWd) }()
547547

548548
config, err := LoadConfig("")
549549
assert.Error(t, err)
@@ -637,11 +637,11 @@ func TestLoadConfigWithEmptyExclude(t *testing.T) {
637637

638638
configPath := filepath.Join(tmpDir, ".cclintrc.json")
639639
jsonData, _ := json.MarshalIndent(configData, "", " ")
640-
os.WriteFile(configPath, jsonData, 0644)
640+
_ = os.WriteFile(configPath, jsonData, 0644)
641641

642642
oldWd, _ := os.Getwd()
643-
os.Chdir(tmpDir)
644-
defer os.Chdir(oldWd)
643+
_ = os.Chdir(tmpDir)
644+
defer func() { _ = os.Chdir(oldWd) }()
645645

646646
config, err := LoadConfig("")
647647
require.NoError(t, err)
@@ -662,11 +662,11 @@ func TestLoadConfigWithNullExtensions(t *testing.T) {
662662

663663
configPath := filepath.Join(tmpDir, ".cclintrc.json")
664664
jsonData, _ := json.MarshalIndent(configData, "", " ")
665-
os.WriteFile(configPath, jsonData, 0644)
665+
_ = os.WriteFile(configPath, jsonData, 0644)
666666

667667
oldWd, _ := os.Getwd()
668-
os.Chdir(tmpDir)
669-
defer os.Chdir(oldWd)
668+
_ = os.Chdir(tmpDir)
669+
defer func() { _ = os.Chdir(oldWd) }()
670670

671671
config, err := LoadConfig("")
672672
require.NoError(t, err)
@@ -685,11 +685,11 @@ func TestLoadConfigEmptyRootPath(t *testing.T) {
685685

686686
configPath := filepath.Join(tmpDir, ".cclintrc.json")
687687
jsonData, _ := json.MarshalIndent(configData, "", " ")
688-
os.WriteFile(configPath, jsonData, 0644)
688+
_ = os.WriteFile(configPath, jsonData, 0644)
689689

690690
oldWd, _ := os.Getwd()
691-
os.Chdir(tmpDir)
692-
defer os.Chdir(oldWd)
691+
_ = os.Chdir(tmpDir)
692+
defer func() { _ = os.Chdir(oldWd) }()
693693

694694
// Pass empty string for rootPath
695695
config, err := LoadConfig("")
@@ -808,13 +808,13 @@ func TestLoadConfigNoConfigFiles(t *testing.T) {
808808
tmpDir := setupTestDir(t)
809809

810810
// Ensure no config files exist
811-
os.Remove(filepath.Join(tmpDir, ".cclintrc.json"))
812-
os.Remove(filepath.Join(tmpDir, ".cclintrc.yaml"))
813-
os.Remove(filepath.Join(tmpDir, ".cclintrc.yml"))
811+
_ = os.Remove(filepath.Join(tmpDir, ".cclintrc.json"))
812+
_ = os.Remove(filepath.Join(tmpDir, ".cclintrc.yaml"))
813+
_ = os.Remove(filepath.Join(tmpDir, ".cclintrc.yml"))
814814

815815
oldWd, _ := os.Getwd()
816-
os.Chdir(tmpDir)
817-
defer os.Chdir(oldWd)
816+
_ = os.Chdir(tmpDir)
817+
defer func() { _ = os.Chdir(oldWd) }()
818818

819819
// Should succeed with defaults
820820
config, err := LoadConfig("")
@@ -838,11 +838,11 @@ func TestLoadConfigPartialConfig(t *testing.T) {
838838

839839
configPath := filepath.Join(tmpDir, ".cclintrc.json")
840840
jsonData, _ := json.MarshalIndent(configData, "", " ")
841-
os.WriteFile(configPath, jsonData, 0644)
841+
_ = os.WriteFile(configPath, jsonData, 0644)
842842

843843
oldWd, _ := os.Getwd()
844-
os.Chdir(tmpDir)
845-
defer os.Chdir(oldWd)
844+
_ = os.Chdir(tmpDir)
845+
defer func() { _ = os.Chdir(oldWd) }()
846846

847847
config, err := LoadConfig("")
848848
require.NoError(t, err)

0 commit comments

Comments
 (0)