Permalink
Browse files

'cd' without arguments should change to the home directory.

Addresses issue #24.
  • Loading branch information...
Andy Chu
Andy Chu committed Aug 6, 2017
1 parent fee9067 commit 2fbc3608696e0be7929bfed3082cc1c05e6fcd0e
Showing with 16 additions and 3 deletions.
  1. +8 −2 core/builtin.py
  2. +1 −1 core/cmd_exec.py
  3. +5 −0 spec/builtins.test.sh
  4. +2 −0 test/sh_spec.py
View
@@ -401,9 +401,15 @@ def Shift(argv, mem):
return mem.Shift(n)
def _Cd(argv, mem):
def Cd(argv, mem):
# TODO: Parse flags, error checking, etc.
dest_dir = argv[0]
try:
dest_dir = argv[0]
except IndexError:
# NOTE: This is equivalent to 'cd ~', but bash/mksh only seem to respect
# $HOME, and not /etc/passwd. OSH behavior is different but better.

This comment has been minimized.

Show comment
Hide comment
@lheckemann

lheckemann Aug 8, 2017

Contributor

Is it really better though? I quite like for HOME to come from passwd on login, then everything else gets it from the environment variable. This allows overriding it consistently in a process subtree — simply by setting the environment variable — without root.

@lheckemann

lheckemann Aug 8, 2017

Contributor

Is it really better though? I quite like for HOME to come from passwd on login, then everything else gets it from the environment variable. This allows overriding it consistently in a process subtree — simply by setting the environment variable — without root.

This comment has been minimized.

Show comment
Hide comment
@andychu

andychu Aug 8, 2017

Contributor

I noticed that bash and mksh throw an error if $HOME is not set.

I intended for it to use $HOME and then fall back on /etc/passwd. But that's not what this does! I'll change it, thanks for noticing.

@andychu

andychu Aug 8, 2017

Contributor

I noticed that bash and mksh throw an error if $HOME is not set.

I intended for it to use $HOME and then fall back on /etc/passwd. But that's not what this does! I'll change it, thanks for noticing.

dest_dir = util.GetHomeDir()
if dest_dir == '-':
old = mem.GetVar('OLDPWD', scope.GlobalOnly)
if old.tag == value_e.Undef:
View
@@ -211,7 +211,7 @@ def _RunBuiltin(self, builtin_id, argv):
status = builtin.Shift(argv, self.mem)
elif builtin_id == EBuiltin.CD:
status = builtin._Cd(argv, self.mem)
status = builtin.Cd(argv, self.mem)
elif builtin_id == EBuiltin.SET:
status = builtin.Set(argv, self.exec_opts, self.mem)
View
@@ -48,6 +48,11 @@ echo "old: $OLDPWD"
cd -
# stdout-json: "old: /\n/\n"
### cd with no arguments
cd
test $(pwd) = ~ && echo OK
# stdout: OK
### pushd/popd
set -o errexit
cd /
View
@@ -858,6 +858,8 @@ def main(argv):
env = {
'TMP': opts.tmp_env,
'PATH': opts.path_env,
# The normal case for ~ and 'cd' is that $HOME is defined.
'HOME': os.environ['HOME'],
}
stats = RunCases(cases, case_predicate, shell_pairs, env, out)
out.EndCases(stats)

0 comments on commit 2fbc360

Please sign in to comment.