fix(process): let Program.Find validate existing paths
This commit is contained in:
parent
f43e8a6e38
commit
c9deb8fdfd
2 changed files with 17 additions and 3 deletions
10
program.go
10
program.go
|
|
@ -44,12 +44,16 @@ type Program struct {
|
|||
//
|
||||
// if err := p.Find(); err != nil { return err }
|
||||
func (p *Program) Find() error {
|
||||
if p.Name == "" {
|
||||
target := p.Name
|
||||
if target == "" {
|
||||
target = p.Path
|
||||
}
|
||||
if target == "" {
|
||||
return coreerr.E("Program.Find", "program name is empty", nil)
|
||||
}
|
||||
path, err := exec.LookPath(p.Name)
|
||||
path, err := exec.LookPath(target)
|
||||
if err != nil {
|
||||
return coreerr.E("Program.Find", core.Sprintf("%q: not found in PATH", p.Name), ErrProgramNotFound)
|
||||
return coreerr.E("Program.Find", core.Sprintf("%q: not found in PATH", target), ErrProgramNotFound)
|
||||
}
|
||||
p.Path = path
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package process_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -32,6 +33,15 @@ func TestProgram_Find_UnknownBinary(t *testing.T) {
|
|||
assert.ErrorIs(t, err, process.ErrProgramNotFound)
|
||||
}
|
||||
|
||||
func TestProgram_Find_UsesExistingPath(t *testing.T) {
|
||||
path, err := exec.LookPath("echo")
|
||||
require.NoError(t, err)
|
||||
|
||||
p := &process.Program{Path: path}
|
||||
require.NoError(t, p.Find())
|
||||
assert.Equal(t, path, p.Path)
|
||||
}
|
||||
|
||||
func TestProgram_Find_EmptyName(t *testing.T) {
|
||||
p := &process.Program{}
|
||||
require.Error(t, p.Find())
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue