Files
archie/internal/skillscript/yaegi_test.go
T
sam 0ae6440a66 fix: address simple findings from a repo-wide golangci-lint sweep
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.
2026-08-22 02:03:11 +10:00

131 lines
3.0 KiB
Go

package skillscript
import (
"os"
"path/filepath"
"testing"
"time"
)
func writeScript(t *testing.T, src string) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "script.go")
if err := os.WriteFile(path, []byte(src), 0o644); err != nil {
t.Fatal(err)
}
return path
}
func TestRunCapturesStdout(t *testing.T) {
path := writeScript(t, `package main
import "fmt"
func main() {
fmt.Println("hello from a skill script")
}
`)
out, err := Run(path)
if err != nil {
t.Fatalf("Run() error = %v", err)
}
if out != "hello from a skill script\n" {
t.Fatalf("Run() = %q", out)
}
}
func TestRunWrapsExternalCommand(t *testing.T) {
path := writeScript(t, `package main
import (
"fmt"
"os/exec"
"context"
)
func main() {
ctx := context.Background()
out, _ := exec.CommandContext(ctx, "sh", "-c", "echo wrapped").CombinedOutput()
fmt.Print(string(out))
}
`)
out, err := Run(path)
if err != nil {
t.Fatalf("Run() error = %v", err)
}
if out != "wrapped\n" {
t.Fatalf("Run() = %q", out)
}
}
func TestRunPanicRecovered(t *testing.T) {
path := writeScript(t, `package main
func main() {
var files []string
_ = files[5]
}
`)
if _, err := Run(path); err == nil {
t.Fatal("Run() error = nil, want a recovered-panic error")
}
}
func TestRunMissingMain(t *testing.T) {
// Yaegi's EvalPath, like `go build` without `go run`, doesn't
// complain about a missing main() -- it just never gets called.
path := writeScript(t, `package main
func notMain() {}
`)
out, err := Run(path)
if err != nil {
t.Fatalf("Run() error = %v", err)
}
if out != "" {
t.Fatalf("Run() = %q, want empty output (main was never called)", out)
}
}
func TestRunMissingFile(t *testing.T) {
if _, err := Run(filepath.Join(t.TempDir(), "missing.go")); err == nil {
t.Fatal("Run() error = nil, want an error for a missing file")
}
}
// TestRunBlocksIndefinitelyOnBlockingScript proves that skillscript.Run
// has no cancellation mechanism. A skill script that blocks forever
// (select{}) wedges the calling goroutine permanently. Every other
// execution path in archie-core supports context cancellation
// (exec.CommandContext in SubprocessRunner, context.Cause checks in
// InProcessRunner), but Run cannot be canceled or timed out because it
// takes no context.Context and calls i.EvalPath synchronously.
func TestRunBlocksIndefinitelyOnBlockingScript(t *testing.T) {
path := writeScript(t, `package main
func main() {
select {}
}
`)
done := make(chan struct{})
go func() {
// The result is irrelevant -- this goroutine only exists to prove
// Run never returns from a blocking script within the timeout.
_, _ = Run(path)
close(done)
}()
select {
case <-done:
// Run returned from a script that should block forever.
// This is unexpected — the script contains select{}.
t.Log("Run returned unexpectedly from a select{} script")
case <-time.After(2 * time.Second):
t.Error("skillscript.Run blocks forever on a select{} script: " +
"Run needs a context.Context parameter for cancellation " +
"(issue #45)")
}
}