Six mechanical fixes from `golangci-lint run ./...` (30 findings total, 23
deliberately left -- see the next commit's message for the one that grew
into something bigger, and the following note for the rest):
- apply.go: %v -> %w for the wrapped yaml error, matching the identical
double-%w pattern already used in decode.go/discovery.go/loader.go in the
same package (errorlint).
- contract_test.go: filepath.Join("provider.go") with a single argument is a
no-op join; replaced with the literal (gocritic).
- inprocess_test.go, yaegi_test.go: two goroutines whose whole purpose is
timing (does Execute return promptly on cancellation / does Run block
forever), where the call's result is irrelevant -- explicitly discarded
with `_, _ =` and a comment saying why, rather than silently ignored
(errcheck).
- find_test.go: an unchecked os.MkdirAll now matches the file's own
createTestFile helper, which already checks and t.Fatals on the same
operation (errcheck).
Not touched, and why: 18 cyclop/gocognit/gocyclo/funlen/nestif findings
require extracting helpers to reduce complexity -- the exact shape of two
regressions already recorded in CLAUDE.md's Lint-driven changes section
(one from --fix silently dropping a predicate, one from a nestif extraction
that doubled data), so a blind sweep across 9 unfamiliar files was declined
rather than risk a third. 2 godox findings (storage.go, worktree.go) mark
real missing work; deleting the comment would hide debt, not fix it. 3
recvcheck findings (Duration, TaskEnvelope, Provenance) each mix a
mandatory-pointer-receiver method (UnmarshalText/UnmarshalJSON's interface
contract, or genuine mutation) with correctly-value-receiver read-only
methods -- forcing consistency would either break interface conformance or
force pointer receivers onto call sites where the value isn't addressable.
Adversarially reviewed by a fresh subagent: all six confirmed
behavior-neutral or behavior-improving, all touched packages pass -race,
golangci-lint confirms exactly 23 issues remain (not more, not fewer), and
none are new.
176 lines
5.0 KiB
Go
176 lines
5.0 KiB
Go
package builtin
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestFindFallback(t *testing.T) {
|
|
// Create a temporary directory tree.
|
|
tmp := t.TempDir()
|
|
createTestFile(t, tmp, "foo.go", "package main")
|
|
createTestFile(t, tmp, "bar.go", "package bar")
|
|
createTestFile(t, tmp, "README.md", "# readme")
|
|
createTestFile(t, tmp, "subdir/baz.go", "package baz")
|
|
createTestFile(t, tmp, "subdir/nested/deep.txt", "deep")
|
|
createTestFile(t, tmp, "vendor/mod.go", "package mod")
|
|
if err := os.MkdirAll(filepath.Join(tmp, "hiddendir"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
createTestFile(t, tmp, "hiddendir/visible.txt", "text")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
params FindParams
|
|
want []string
|
|
wantEmpty bool
|
|
}{
|
|
{
|
|
name: "glob pattern *.go (matches all depths)",
|
|
params: FindParams{Path: tmp, Pattern: "*.go", Type: "file"},
|
|
want: []string{"bar.go", "foo.go", "subdir/baz.go", "vendor/mod.go"},
|
|
},
|
|
{
|
|
name: "match directories (nested shown)",
|
|
params: FindParams{Path: tmp, Type: "directory"},
|
|
want: []string{"hiddendir", "subdir", "subdir/nested", "vendor"},
|
|
},
|
|
{
|
|
name: "max depth 1",
|
|
params: FindParams{Path: tmp, Type: "file", MaxDepth: 1},
|
|
want: []string{"bar.go", "foo.go", "README.md"},
|
|
},
|
|
{
|
|
name: "max depth 0 unlimited",
|
|
params: FindParams{Path: tmp, Type: "file", MaxDepth: 0},
|
|
want: []string{
|
|
"bar.go", "foo.go", "README.md",
|
|
"hiddendir/visible.txt", "subdir/baz.go",
|
|
"subdir/nested/deep.txt", "vendor/mod.go",
|
|
},
|
|
},
|
|
{
|
|
name: "exclude vendor",
|
|
params: FindParams{Path: tmp, Type: "file", Exclude: "vendor"},
|
|
want: []string{
|
|
"bar.go", "foo.go", "README.md",
|
|
"hiddendir/visible.txt", "subdir/baz.go",
|
|
"subdir/nested/deep.txt",
|
|
},
|
|
},
|
|
{
|
|
name: "exclude by glob",
|
|
params: FindParams{Path: tmp, Type: "file", Exclude: "*.md"},
|
|
want: []string{
|
|
"bar.go", "foo.go",
|
|
"hiddendir/visible.txt", "subdir/baz.go",
|
|
"subdir/nested/deep.txt", "vendor/mod.go",
|
|
},
|
|
},
|
|
{
|
|
name: "no matches",
|
|
params: FindParams{Path: tmp, Pattern: "nonexistent*", Type: "file"},
|
|
wantEmpty: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
res, err := runFindGoFallback(ctx, tmp, tmp, tc.params)
|
|
if err != nil {
|
|
t.Fatalf("runFindGoFallback returned error: %v", err)
|
|
}
|
|
if tc.wantEmpty {
|
|
if res.Content != "no matches found" {
|
|
t.Fatalf("expected empty, got:\n%s", res.Content)
|
|
}
|
|
return
|
|
}
|
|
gotLines := strings.Split(strings.TrimSpace(res.Content), "\n")
|
|
if len(gotLines) == 1 && gotLines[0] == "" {
|
|
gotLines = []string{}
|
|
}
|
|
sort.Strings(gotLines)
|
|
sort.Strings(tc.want)
|
|
if len(gotLines) != len(tc.want) {
|
|
t.Fatalf("got %d results, want %d\ngot: %v\nwant: %v", len(gotLines), len(tc.want), gotLines, tc.want)
|
|
}
|
|
for i := range tc.want {
|
|
if gotLines[i] != tc.want[i] {
|
|
t.Fatalf("result mismatch at %d: got %q, want %q", i, gotLines[i], tc.want[i])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFindFallback_NonExistentPath(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
res, _ := runFindGoFallback(ctx, "/nonexistent/path", "/nonexistent/path", FindParams{})
|
|
if !res.IsError {
|
|
t.Fatal("expected error for non-existent path")
|
|
}
|
|
}
|
|
|
|
func TestFindFallback_NotADirectory(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
f := filepath.Join(tmp, "file.txt")
|
|
if err := os.WriteFile(f, []byte("hello"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
res, _ := runFindGoFallback(ctx, tmp, f, FindParams{})
|
|
if !res.IsError {
|
|
t.Fatal("expected error when path is a file")
|
|
}
|
|
}
|
|
|
|
// A cancelled or timed-out walk must be reported as an error, not silently
|
|
// returned as "no matches found" (or as whatever partial results happened to
|
|
// accumulate before cancellation). The WalkDir callback swallows every
|
|
// per-entry error and returns nil, so the only error that can ever reach the
|
|
// caller is ctx.Err() from the cancellation select -- the old guard compared
|
|
// that against itself and never fired.
|
|
func TestFindFallback_CancelledWalkReportsError(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
createTestFile(t, tmp, "foo.go", "package main")
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
res, err := runFindGoFallback(ctx, tmp, tmp, FindParams{})
|
|
if err != nil {
|
|
t.Fatalf("runFindGoFallback returned an error: %v", err)
|
|
}
|
|
if !res.IsError {
|
|
t.Fatalf("expected an error result for a cancelled walk, got: %q", res.Content)
|
|
}
|
|
if res.Content == "no matches found" {
|
|
t.Fatal("cancelled walk reported as 'no matches found': the search never finished")
|
|
}
|
|
}
|
|
|
|
func createTestFile(t *testing.T, base, relPath, content string) {
|
|
t.Helper()
|
|
full := filepath.Join(base, relPath)
|
|
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(full, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|