def test_path_search_which(self):
" which() finds an executable in $PATH and returns its abspath. "
fname = 'gcc'
bin_dir = tempfile.mkdtemp()
bin_path = os.path.join(bin_dir, fname)
save_path = os.environ['PATH']
try:
# setup
os.environ['PATH'] = bin_dir
with open(bin_path, 'w') as fp:
pass
# given non-executable,
os.chmod(bin_path, 0o400)
# exercise absolute and relative,
assert pexpect.which(bin_path) is None
assert pexpect.which(fname) is None
# given executable,
os.chmod(bin_path, 0o700)
# exercise absolute and relative,
assert pexpect.which(bin_path) == bin_path
assert pexpect.which(fname) == bin_path
finally:
# restore,
os.environ['PATH'] = save_path
# destroy scratch files and folders,
if os.path.exists(bin_path):
os.unlink(bin_path)
if os.path.exists(bin_dir):
os.rmdir(bin_dir)
评论列表
文章目录