-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use PATH from session to look for programs (#555)
If `PATH` is present in the session, use it to look for the program paths. This is especially important for situation when within a session a virtual env is created and consecutive code blocks should be executed within it. Fixes #552 Port this change to #548.
- Loading branch information
Showing
12 changed files
with
194 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package system | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
var Default = newDefault() | ||
|
||
func newDefault() *System { | ||
return &System{ | ||
getPathEnv: func() string { return os.Getenv("PATH") }, | ||
} | ||
} | ||
|
||
type Option func(*System) | ||
|
||
func WithPathEnvGetter(fn func() string) Option { | ||
return func(s *System) { | ||
s.getPathEnv = fn | ||
} | ||
} | ||
|
||
type System struct { | ||
getPathEnv func() string | ||
} | ||
|
||
func New(opts ...Option) *System { | ||
s := newDefault() | ||
|
||
for _, opt := range opts { | ||
opt(s) | ||
} | ||
|
||
return s | ||
} | ||
|
||
func (s *System) LookPath(file string) (string, error) { | ||
return lookPath(s.getPathEnv(), file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//go:build unix | ||
|
||
// TODO(adamb): remove the build flag when [System.LookPath] is implemented for Windows. | ||
|
||
package system | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLookPath(t *testing.T) { | ||
tmp := t.TempDir() | ||
myBinaryPath := filepath.Join(tmp, "my-binary") | ||
|
||
// Create an empty file with execute permission. | ||
err := os.WriteFile(myBinaryPath, []byte{}, 0o111) | ||
require.NoError(t, err) | ||
|
||
s := New( | ||
WithPathEnvGetter(func() string { return tmp }), | ||
) | ||
path, err := s.LookPath("my-binary") | ||
require.NoError(t, err) | ||
assert.Equal(t, myBinaryPath, path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//go:build unix | ||
|
||
package system | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
func lookPath(pathEnv, file string) (string, error) { | ||
if strings.Contains(file, "/") { | ||
err := findExecutable(file) | ||
if err == nil { | ||
return file, nil | ||
} | ||
return "", &exec.Error{Name: file, Err: err} | ||
} | ||
for _, dir := range filepath.SplitList(pathEnv) { | ||
if dir == "" { | ||
// Unix shell semantics: path element "" means "." | ||
dir = "." | ||
} | ||
path := filepath.Join(dir, file) | ||
if err := findExecutable(path); err == nil { | ||
if !filepath.IsAbs(path) { | ||
return path, &exec.Error{Name: file, Err: exec.ErrDot} | ||
} | ||
return path, nil | ||
} | ||
} | ||
return "", &exec.Error{Name: file, Err: exec.ErrNotFound} | ||
} | ||
|
||
func findExecutable(file string) error { | ||
d, err := os.Stat(file) | ||
if err != nil { | ||
return err | ||
} | ||
m := d.Mode() | ||
if m.IsDir() { | ||
return syscall.EISDIR | ||
} | ||
if m&0o111 != 0 { | ||
return nil | ||
} | ||
return fs.ErrPermission | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package system | ||
|
||
import "os/exec" | ||
|
||
func lookPath(_, file string) (string, error) { | ||
// TODO(adamb): implement this for Windows. | ||
// Check out https://github.com/golang/go/blob/master/src/os/exec/lp_windows.go. | ||
return exec.LookPath(file) | ||
} |