Module path: forge.lthn.ai/core/go-process -> dappco.re/go/core/process Import path updates: - forge.lthn.ai/core/go-log -> dappco.re/go/core/log - forge.lthn.ai/core/go-io -> dappco.re/go/core/io - forge.lthn.ai/core/go-ws -> dappco.re/go/core/ws - forge.lthn.ai/core/go-process (self) -> dappco.re/go/core/process - forge.lthn.ai/core/api left as-is (not yet migrated) Local replace directives added until vanity URL server is configured. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package process_test
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"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_KnownBinary(t *testing.T) {
|
|
p := &process.Program{Name: "echo"}
|
|
require.NoError(t, p.Find())
|
|
assert.NotEmpty(t, p.Path)
|
|
}
|
|
|
|
func TestProgram_Find_UnknownBinary(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_Find_EmptyName(t *testing.T) {
|
|
p := &process.Program{}
|
|
require.Error(t, p.Find())
|
|
}
|
|
|
|
func TestProgram_Run_ReturnsOutput(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_Run_WithoutFind_FallsBackToName(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_UsesDirectory(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)
|
|
// Resolve symlinks on both sides for portability (macOS uses /private/ prefix).
|
|
canonicalDir, err := filepath.EvalSymlinks(dir)
|
|
require.NoError(t, err)
|
|
canonicalOut, err := filepath.EvalSymlinks(out)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, canonicalDir, canonicalOut)
|
|
}
|
|
|
|
func TestProgram_Run_FailingCommand(t *testing.T) {
|
|
p := &process.Program{Name: "false"}
|
|
require.NoError(t, p.Find())
|
|
|
|
_, err := p.Run(testCtx(t))
|
|
require.Error(t, err)
|
|
}
|