def test_which_follows_symlink(self):
" which() follows symlinks and returns its path. "
fname = 'original'
symname = 'extra-crispy'
bin_dir = tempfile.mkdtemp()
bin_path = os.path.join(bin_dir, fname)
sym_path = os.path.join(bin_dir, symname)
save_path = os.environ['PATH']
try:
# setup
os.environ['PATH'] = bin_dir
with open(bin_path, 'w') as fp:
pass
os.chmod(bin_path, 0o400)
os.symlink(bin_path, sym_path)
# should not be found because symlink points to non-executable
assert pexpect.which(symname) is None
# but now it should -- because it is executable
os.chmod(bin_path, 0o700)
assert pexpect.which(symname) == sym_path
finally:
# restore,
os.environ['PATH'] = save_path
# destroy scratch files, symlinks, and folders,
if os.path.exists(sym_path):
os.unlink(sym_path)
if os.path.exists(bin_path):
os.unlink(bin_path)
if os.path.exists(bin_dir):
os.rmdir(bin_dir)
评论列表
文章目录