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

Add support for pwd -P #728

Merged
merged 1 commit into from
Sep 10, 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
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)" ]]`,
"",
},
{
`mkdir a; ln -s a b; [[ "$(cd a && pwd -P)" == "$(cd b && pwd -L)" ]]; echo $?`,
"1\n",
},

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