diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-26 18:16:11 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-26 18:16:11 +0300 |
| commit | c4b872c5ec54340b1e62d7578ace400340573ce2 (patch) | |
| tree | d224707b3dd25ac68693d44f084dbe85a0edd685 /internal/filelock | |
| parent | f77d73244fa6585af0456fd374988b8b04b72646 (diff) | |
test: bring every package above the 80% coverage target
Per-package coverage was below the AGENTS.md target in six packages:
cmd/ask 0.0% -> 83.3%
cmd/hexai-tmux-edit 10.0% -> 93.3%
cmd/hexai-tmux-action 27.8% -> 95.7%
cmd/hexai-mcp-server 41.9% -> 88.2%
internal/taskproxy 61.8% -> 98.2%
internal/filelock 77.3% -> 100.0%
The four cmd packages each had a main() that mixed flag parsing, struct
construction, and runtime delegation, so nothing called from a test
hit those statements. Each main() is now a one-line wrapper around a
testable runMain(args, stdin, stdout, stderr) int that uses
flag.NewFlagSet (instead of the global flag.Parse) so tests can drive
it repeatedly. The deprecation banner in hexai-mcp-server is now a
package-level constant, kept identical, so tests can assert on it
directly without redirecting os.Stderr.
The internal packages got new tests for paths that were previously
unreachable: filelock's retry-then-success and non-EWOULDBLOCK error
branches, and taskproxy's NewRunner / findTaskBinary / detectRepoRoot /
runTaskCommand helpers (the ones that shell out to git and task).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'internal/filelock')
| -rw-r--r-- | internal/filelock/filelock_test.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/internal/filelock/filelock_test.go b/internal/filelock/filelock_test.go index f1f5b65..a5ca520 100644 --- a/internal/filelock/filelock_test.go +++ b/internal/filelock/filelock_test.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "testing" + "time" ) func TestTryExclusive_SecondDescriptorWouldBlock(t *testing.T) { @@ -65,3 +66,71 @@ func TestAcquireExclusive_ContextCancelledWhileBlocked(t *testing.T) { t.Fatal(err) } } + +// Exercises the retry-then-success path inside AcquireExclusive: the lock is +// initially held, the helper releases it after a short wait, and the waiting +// caller must observe a Flock retry succeed and return a working unlock fn. +func TestAcquireExclusive_RetriesAndSucceeds(t *testing.T) { + dir := t.TempDir() + p := filepath.Join(dir, "lock") + + fHeld, err := os.OpenFile(p, os.O_CREATE|os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = fHeld.Close() }) + if err := TryExclusive(fHeld); err != nil { + t.Fatal(err) + } + + fWait, err := os.OpenFile(p, os.O_CREATE|os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = fWait.Close() }) + + released := make(chan struct{}) + go func() { + time.Sleep(20 * time.Millisecond) + _ = UnlockExclusive(fHeld) + close(released) + }() + + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + unlock, err := AcquireExclusive(ctx, fWait) + if err != nil { + t.Fatalf("AcquireExclusive = %v, want nil after retry", err) + } + <-released + if err := unlock(); err != nil { + t.Fatalf("unlock returned: %v", err) + } +} + +// Drives the non-EWOULDBLOCK error path: closing the file before calling +// AcquireExclusive yields EBADF, which must propagate up unchanged instead of +// being mapped to ErrWouldBlock or causing an infinite retry. +func TestAcquireExclusive_NonBlockingError_ReturnsImmediately(t *testing.T) { + dir := t.TempDir() + p := filepath.Join(dir, "lock") + f, err := os.OpenFile(p, os.O_CREATE|os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + if err := f.Close(); err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) + defer cancel() + _, err = AcquireExclusive(ctx, f) + if err == nil { + t.Fatalf("AcquireExclusive on closed fd: got nil, want non-nil error") + } + if errors.Is(err, ErrWouldBlock) { + t.Fatalf("AcquireExclusive on closed fd: got ErrWouldBlock, want underlying syscall error") + } + if errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("AcquireExclusive on closed fd: got DeadlineExceeded, want immediate syscall error") + } +} |
