go-process/program_test.go
Claude 861c88b8e8
fix(ax): AX compliance sweep — banned imports, naming, test coverage
- pkg/api/provider.go: remove banned os/syscall imports; delegate to
  new process.KillPID and process.IsPIDAlive exported helpers
- service.go: rename `sr` → `startResult`; add KillPID/IsPIDAlive exports
- runner.go: rename `aggResult` → `aggregate` in all three RunXxx methods;
  add usage-example comments on all exported functions
- process.go: replace prose doc-comments with usage-example comments
- buffer.go, registry.go, health.go: replace prose comments with examples
- buffer_test.go: rename TestRingBuffer_Basics_Good → TestBuffer_{Write,String,Reset}_{Good,Bad,Ugly}
- All test files: add missing _Bad and _Ugly variants for all functions
  (daemon, health, pidfile, registry, runner, process, program, exec, pkg/api)

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-31 08:15:47 +01:00

99 lines
2.4 KiB
Go

package process_test
import (
"context"
"os"
"testing"
"time"
"dappco.re/go/core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
process "dappco.re/go/core/process"
)
func testCtx(t *testing.T) context.Context {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
return ctx
}
func TestProgram_Find_Good(t *testing.T) {
p := &process.Program{Name: "echo"}
require.NoError(t, p.Find())
assert.NotEmpty(t, p.Path)
}
func TestProgram_FindUnknown_Bad(t *testing.T) {
p := &process.Program{Name: "no-such-binary-xyzzy-42"}
err := p.Find()
require.Error(t, err)
assert.ErrorIs(t, err, process.ErrProgramNotFound)
}
func TestProgram_FindEmpty_Bad(t *testing.T) {
p := &process.Program{}
require.Error(t, p.Find())
}
func TestProgram_Run_Good(t *testing.T) {
p := &process.Program{Name: "echo"}
require.NoError(t, p.Find())
out, err := p.Run(testCtx(t), "hello")
require.NoError(t, err)
assert.Equal(t, "hello", out)
}
func TestProgram_RunFallback_Good(t *testing.T) {
// Path is empty; RunDir should fall back to Name for OS PATH resolution.
p := &process.Program{Name: "echo"}
out, err := p.Run(testCtx(t), "fallback")
require.NoError(t, err)
assert.Equal(t, "fallback", out)
}
func TestProgram_RunDir_Good(t *testing.T) {
p := &process.Program{Name: "pwd"}
require.NoError(t, p.Find())
dir := t.TempDir()
out, err := p.RunDir(testCtx(t), dir)
require.NoError(t, err)
dirInfo, err := os.Stat(dir)
require.NoError(t, err)
outInfo, err := os.Stat(core.Trim(out))
require.NoError(t, err)
assert.True(t, os.SameFile(dirInfo, outInfo))
}
func TestProgram_RunFailure_Bad(t *testing.T) {
p := &process.Program{Name: "false"}
require.NoError(t, p.Find())
_, err := p.Run(testCtx(t))
require.Error(t, err)
}
func TestProgram_Find_Ugly(t *testing.T) {
t.Run("find then run in non-existent dir returns error", func(t *testing.T) {
p := &process.Program{Name: "echo"}
require.NoError(t, p.Find())
_, err := p.RunDir(testCtx(t), "/nonexistent-dir-xyz-abc")
assert.Error(t, err)
})
t.Run("path set directly skips PATH lookup", func(t *testing.T) {
p := &process.Program{Name: "echo", Path: "/bin/echo"}
out, err := p.Run(testCtx(t), "direct")
// Only assert no panic; binary may be at different location on some systems
if err == nil {
assert.Equal(t, "direct", out)
}
})
}