Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interp: fix a PATH regression on Windows, fix tests on Wine #769

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions interp/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (
"math/rand"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -456,13 +454,6 @@ func (r *Runner) Reset() {
r.setVarString("IFS", " \t\n")
r.setVarString("OPTIND", "1")

if runtime.GOOS == "windows" {
// convert $PATH to a unix path list
path := r.writeEnv.Get("PATH").String()
path = strings.Join(filepath.SplitList(path), ":")
r.setVarString("PATH", path)
}

r.dirStack = append(r.dirStack, r.Dir)
r.didReset = true
}
Expand Down
40 changes: 4 additions & 36 deletions interp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,41 +183,6 @@ func findFile(dir, file string, _ []string) (string, error) {
return checkStat(dir, file, false)
}

func driveLetter(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}

// splitList is like filepath.SplitList, but always using the unix path
// list separator ':'. On Windows, it also makes sure not to split
// [A-Z]:[/\].
func splitList(path string) []string {
if path == "" {
return []string{""}
}
list := strings.Split(path, ":")
if runtime.GOOS != "windows" {
return list
}
// join "C", "/foo" into "C:/foo"
var fixed []string
for i := 0; i < len(list); i++ {
s := list[i]
switch {
case len(s) != 1, !driveLetter(s[0]):
case i+1 >= len(list):
// last element
case strings.IndexAny(list[i+1], `/\`) != 0:
// next element doesn't start with / or \
default:
fixed = append(fixed, s+":"+list[i+1])
i++
continue
}
fixed = append(fixed, s)
}
return fixed
}

// LookPath is deprecated. See LookPathDir.
func LookPath(env expand.Environ, file string) (string, error) {
return LookPathDir(env.Get("PWD").String(), env, file)
Expand All @@ -240,7 +205,10 @@ func lookPathDir(cwd string, env expand.Environ, file string, find findAny) (str
panic("no find function found")
}

pathList := splitList(env.Get("PATH").String())
pathList := filepath.SplitList(env.Get("PATH").String())
if len(pathList) == 0 {
pathList = []string{""}
}
chars := `/`
if runtime.GOOS == "windows" {
chars = `:\/`
Expand Down
42 changes: 42 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ func TestMain(m *testing.M) {
case "foo_null_bar":
fmt.Println("foo\x00bar")
os.Exit(1)
case "lookpath":
_, err := exec.LookPath(pathProg)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%s found\n", pathProg)
os.Exit(0)
}
r := strings.NewReader(os.Args[1])
file, err := syntax.NewParser().Parse(r, "")
Expand Down Expand Up @@ -2891,6 +2899,10 @@ var runTestsUnix = []runTest{
"mkdir c; echo '#!/bin/sh\necho b' >c/a; chmod 0755 c/a; c/a",
"b\n",
},
{
"GOSH_CMD=lookpath $GOSH_PROG",
"sh found\n",
},

// error strings which are too different on Windows
{
Expand Down Expand Up @@ -3107,6 +3119,10 @@ var runTestsWindows = []runTest{
{"[[ -n $PPID || $PPID -gt 0 ]]", ""}, // os.Getppid can be 0 on windows
{"cmd() { :; }; cmd /c 'echo foo'", ""},
{"cmd() { :; }; command cmd /c 'echo foo'", "foo\r\n"},
{
"GOSH_CMD=lookpath $GOSH_PROG",
"cmd found\n",
},
}

// These tests are specific to 64-bit architectures, and that's fine. We don't
Expand Down Expand Up @@ -3240,6 +3256,32 @@ var testBuiltinsMap = map[string]func(HandlerContext, []string) error{
}
return nil
},
"tr": func(hc HandlerContext, args []string) error {
if len(args) != 2 || len(args[1]) != 1 {
return fmt.Errorf("usage: tr [-s -d] [character]")
}
squeeze := args[0] == "-s"
char := args[1][0]
bs, err := io.ReadAll(hc.Stdin)
if err != nil {
return err
}
for {
i := bytes.IndexByte(bs, char)
if i < 0 {
hc.Stdout.Write(bs) // remaining
break
}
hc.Stdout.Write(bs[:i]) // up to char
bs = bs[i+1:]

bs = bytes.TrimLeft(bs, string(char)) // remove repeats
if squeeze {
hc.Stdout.Write([]byte{char})
}
}
return nil
},
"sort": func(hc HandlerContext, args []string) error {
lines, err := readLines(hc)
if err != nil {
Expand Down