Skip to content

Commit

Permalink
Add support for pwd -P
Browse files Browse the repository at this point in the history
  • Loading branch information
amancevice committed Sep 10, 2021
1 parent 0d07ab0 commit 8acb7b1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
22 changes: 21 additions & 1 deletion interp/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,27 @@ func (r *Runner) builtinCode(ctx context.Context, pos syntax.Pos, name string, a
return 2
}
case "pwd":
r.outf("%s\n", r.envGet("PWD"))
switch len(args) {
case 0:
r.outf("%s\n", r.envGet("PWD"))
case 1:
switch args[0] {
case "-L":
r.outf("%s\n", r.envGet("PWD"))
case "-P":
if path, err := filepath.EvalSymlinks(r.envGet("PWD")); err == nil {
r.outf("%s\n", path)
} else {
r.setErr(err)
}
default:
r.errf("invalid option: %q\n", args[0])
return 2
}
default:
r.errf("pwd cannot take multiple arguments\n")
return 1
}
case "cd":
var path string
switch len(args) {
Expand Down
16 changes: 16 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,22 @@ var runTests = []runTest{
`mkdir a; ln -s a b; [[ $(cd a && pwd) == "$(cd b && pwd)" ]]; echo $?`,
"1\n",
},
{
`pwd -a`,
"invalid option: \"-a\"\nexit status 2",
},
{
`pwd -L -P`,
"pwd cannot take multiple arguments\nexit status 1",
},
{
`mkdir a; ln -s a b; [[ "$(cd a && pwd -P)" == "$(cd b && pwd -P)" ]]`,
"",
},
{
`[[ "$(pwd)" == "$(pwd -L)" ]]`,
"",
},

// dirs/pushd/popd
{"set -- $(dirs); echo $# ${#DIRSTACK[@]}", "1 1\n"},
Expand Down

0 comments on commit 8acb7b1

Please sign in to comment.