Fix the $pwd tests

Not sure what happened but my fix for issue #1120 resulted in the core
unit test module not being executed due to a bogus "// +build wtf"
comment. Apparently I was experimenting with "+build" directives and
forgot to remove that line. This also improves the test coverage of the
$pwd code from 44% to 100%.
This commit is contained in:
Kurtis Rader 2020-10-16 19:35:38 -07:00 committed by Qi Xiao
parent 80208d3972
commit f1baa70a78
2 changed files with 27 additions and 10 deletions

View File

@ -19,13 +19,17 @@ type pwdVar struct {
var _ vars.Var = pwdVar{}
// Getwd allows for unit test error injection.
var getwd func() (string, error) = os.Getwd
var Getwd func() (string, error) = os.Getwd
// Get returns the current working directory. It returns "/unknown/pwd" when
// it cannot be determined.
func (pwdVar) Get() interface{} {
pwd, err := getwd()
pwd, err := Getwd()
if err != nil {
// This should really use the path separator appropriate for the
// platform but in practice this hardcoded string works fine. Both
// because MS Windows supports forward slashes and this will very
// rarely occur.
return "/unknown/pwd"
}
return pwd

View File

@ -1,10 +1,9 @@
// +build wtf
package eval_test
import (
"errors"
"path/filepath"
"runtime"
"testing"
. "github.com/elves/elvish/pkg/eval"
@ -24,18 +23,32 @@ func TestBuiltinPwd(t *testing.T) {
Test(t,
That(`pwd=dir1 put $pwd; put $pwd`).Puts(dir1, tmpHome),
That(`cd ~/dir2; pwd=~ put $pwd; put $pwd`).Puts(tmpHome, dir2),
That(`cd ~/dir2; pwd=~/dir1 put $pwd; put $pwd`).Puts(dir1, dir2),
That(`cd ~/dir1; pwd=../dir2 put $pwd; put $pwd`).Puts(dir2, dir1),
That(`pwd=(float64 1) put $pwd`).Throws(ErrPathMustBeString, "pwd=(float64 1)"),
)
// We could separate these two test variants into separate unit test
// modules but that's overkill for this situation and makes the
// equivalence between the two environments harder to see.
if runtime.GOOS == "windows" {
Test(t,
That(`cd $E:HOME\dir2; pwd=$E:HOME put $pwd; put $pwd`).Puts(tmpHome, dir2),
That(`cd $E:HOME\dir2; pwd=..\dir1 put $pwd; put $pwd`).Puts(dir1, dir2),
That(`cd $E:HOME\dir1; pwd=..\dir2 put $pwd; put $pwd`).Puts(dir2, dir1),
)
} else {
Test(t,
That(`cd ~/dir2; pwd=~ put $pwd; put $pwd`).Puts(tmpHome, dir2),
That(`cd ~/dir2; pwd=~/dir1 put $pwd; put $pwd`).Puts(dir1, dir2),
That(`cd ~/dir1; pwd=../dir2 put $pwd; put $pwd`).Puts(dir2, dir1),
)
}
}
// Verify the behavior when the CWD cannot be determined.
func TestBuiltinPwd_GetwdError(t *testing.T) {
origGetwd := getwd
getwd = mockGetwdWithError
defer func() { getwd = origGetwd }()
origGetwd := Getwd
Getwd = mockGetwdWithError
defer func() { Getwd = origGetwd }()
Test(t,
That(`put $pwd`).Puts("/unknown/pwd"),