2026-03-18 15:07:03 +00:00
|
|
|
package process_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-03-30 00:45:36 +00:00
|
|
|
"os"
|
2026-03-31 19:56:13 +00:00
|
|
|
"path/filepath"
|
2026-03-18 15:07:03 +00:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
"dappco.re/go/core"
|
2026-03-18 15:07:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2026-03-21 23:49:08 +00:00
|
|
|
process "dappco.re/go/core/process"
|
2026-03-18 15:07:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func testCtx(t *testing.T) context.Context {
|
|
|
|
|
t.Helper()
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
return ctx
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestProgram_Find_Good(t *testing.T) {
|
2026-03-18 15:07:03 +00:00
|
|
|
p := &process.Program{Name: "echo"}
|
|
|
|
|
require.NoError(t, p.Find())
|
|
|
|
|
assert.NotEmpty(t, p.Path)
|
2026-03-31 19:56:13 +00:00
|
|
|
assert.True(t, filepath.IsAbs(p.Path))
|
2026-03-18 15:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestProgram_FindUnknown_Bad(t *testing.T) {
|
2026-03-18 15:07:03 +00:00
|
|
|
p := &process.Program{Name: "no-such-binary-xyzzy-42"}
|
|
|
|
|
err := p.Find()
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
assert.ErrorIs(t, err, process.ErrProgramNotFound)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestProgram_FindEmpty_Bad(t *testing.T) {
|
2026-03-18 15:07:03 +00:00
|
|
|
p := &process.Program{}
|
|
|
|
|
require.Error(t, p.Find())
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestProgram_Run_Good(t *testing.T) {
|
2026-03-18 15:07:03 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestProgram_RunFallback_Good(t *testing.T) {
|
2026-03-18 15:07:03 +00:00
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 19:22:49 +00:00
|
|
|
func TestProgram_RunNilContext_Good(t *testing.T) {
|
|
|
|
|
p := &process.Program{Name: "echo"}
|
|
|
|
|
|
|
|
|
|
out, err := p.Run(nil, "nil-context")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, "nil-context", out)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestProgram_RunDir_Good(t *testing.T) {
|
2026-03-18 15:07:03 +00:00
|
|
|
p := &process.Program{Name: "pwd"}
|
|
|
|
|
require.NoError(t, p.Find())
|
|
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
|
|
out, err := p.RunDir(testCtx(t), dir)
|
|
|
|
|
require.NoError(t, err)
|
2026-03-30 00:45:36 +00:00
|
|
|
dirInfo, err := os.Stat(dir)
|
2026-03-18 15:07:03 +00:00
|
|
|
require.NoError(t, err)
|
2026-03-30 00:45:36 +00:00
|
|
|
outInfo, err := os.Stat(core.Trim(out))
|
2026-03-18 15:07:03 +00:00
|
|
|
require.NoError(t, err)
|
2026-03-30 00:45:36 +00:00
|
|
|
assert.True(t, os.SameFile(dirInfo, outInfo))
|
2026-03-18 15:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestProgram_RunFailure_Bad(t *testing.T) {
|
2026-03-18 15:07:03 +00:00
|
|
|
p := &process.Program{Name: "false"}
|
|
|
|
|
require.NoError(t, p.Find())
|
|
|
|
|
|
|
|
|
|
_, err := p.Run(testCtx(t))
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
}
|