From f1baa70a7832dc41a203ff3e23f64321378b0e41 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Fri, 16 Oct 2020 19:35:38 -0700 Subject: [PATCH] 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%. --- pkg/eval/pwd.go | 8 ++++++-- pkg/eval/pwd_test.go | 29 +++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/pkg/eval/pwd.go b/pkg/eval/pwd.go index b46f1d5b..2becdc8f 100644 --- a/pkg/eval/pwd.go +++ b/pkg/eval/pwd.go @@ -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 diff --git a/pkg/eval/pwd_test.go b/pkg/eval/pwd_test.go index 0daa316d..ae1cf07e 100644 --- a/pkg/eval/pwd_test.go +++ b/pkg/eval/pwd_test.go @@ -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"),