Permalink
Browse files

Fix bug in last commit.

lstat() should only be used for -h and -L.

Addresses issue #58.
  • Loading branch information...
Andy Chu
Andy Chu committed Jan 1, 2018
1 parent ce229de commit 52a4d11532a81a8a3ebdab51166e4270b202b9d1
Showing with 25 additions and 9 deletions.
  1. +11 −5 core/expr_eval.py
  2. +14 −4 spec/builtin-test.test.sh
View
@@ -478,9 +478,18 @@ def Eval(self, node):
# Now dispatch on arg type
arg_type = BOOL_OPS[op_id] # could be static in the LST?
if arg_type == OperandType.Path:
# Only use lstat if we're testing for a symlink.
if op_id in (Id.BoolUnary_h, Id.BoolUnary_L):
try:
mode = os.lstat(s).st_mode
except OSError as e:
return False
return stat.S_ISLNK(mode)
try:
mode = os.lstat(s).st_mode
except OSError as e: # Python 3: FileNotFoundError
mode = os.stat(s).st_mode
except OSError as e:
# TODO: Signal extra debug information?
#self._AddErrorContext("Error from stat(%r): %s" % (s, e))
return False
@@ -503,9 +512,6 @@ def Eval(self, node):
if op_id == Id.BoolUnary_w:
return os.access(s, os.W_OK)
if op_id in (Id.BoolUnary_h, Id.BoolUnary_L):
return stat.S_ISLNK(mode)
raise NotImplementedError(op_id)
if arg_type == OperandType.Str:
View
@@ -199,14 +199,24 @@ test -w $TMP/testw_no || echo 'no'
# stdout-json: "yes\nno\n"
### -h and -L test for symlink
touch $TMP/zz
ln -s -f $TMP/zz $TMP/symlink
ln -s -f $TMP/__nonexistent_ZZ__ $TMP/dangling
test -L $TMP/zz || echo no
test -h $TMP/zz || echo no
test -L $TMP/symlink && echo yes
test -h $TMP/symlink && echo yes
test -f $TMP/symlink && echo is-file
test -L $TMP/symlink && echo symlink
test -h $TMP/symlink && echo symlink
test -L $TMP/dangling && echo dangling
test -h $TMP/dangling && echo dangling
test -f $TMP/dangling || echo 'dangling is not file'
## STDOUT:
no
no
yes
yes
is-file
symlink
symlink
dangling
dangling
dangling is not file
## END

0 comments on commit 52a4d11

Please sign in to comment.