elvish/pkg/eval/pwd.go
Kurtis Rader 0f6b4bb737 Test $pwd behavior
Related #1062
Resolves #1120
2020-09-03 04:25:38 +01:00

38 lines
795 B
Go

package eval
import (
"os"
"github.com/elves/elvish/pkg/eval/vars"
)
// PwdVariable is a variable whose value always reflects the current working
// directory. Setting it changes the current working directory.
type PwdVariable struct {
ev *Evaler
}
var _ vars.Var = PwdVariable{}
// Getwd allows for unit test error injection.
var getwd func() (string, error) = os.Getwd
// Get returns the current working directory. It returns "/unknown/pwd" when
// it cannot be determined.
func (PwdVariable) Get() interface{} {
pwd, err := getwd()
if err != nil {
return "/unknown/pwd"
}
return pwd
}
// Set changes the current working directory.
func (pwd PwdVariable) Set(v interface{}) error {
path, ok := v.(string)
if !ok {
return ErrPathMustBeString
}
return pwd.ev.Chdir(path)
}