diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-23 08:45:33 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-23 08:45:33 (GMT) |
commit | e9a63600b3a781f1ca744eaaa380fb4b05d43115 (patch) | |
tree | 30fe6e7781e58b8299239a842e4c566865e5494e /Lib/test/test_shutil.py | |
parent | 605a95ae4403223fd6f62bcd313ce2ca72a9dbdb (diff) | |
parent | 8bea200b98356c931aed6ae3ddfe2e1f5400ab71 (diff) | |
download | cpython-e9a63600b3a781f1ca744eaaa380fb4b05d43115.zip cpython-e9a63600b3a781f1ca744eaaa380fb4b05d43115.tar.gz cpython-e9a63600b3a781f1ca744eaaa380fb4b05d43115.tar.bz2 |
Issue #16957: shutil.which() no longer searches a bare file name in the
current directory on Unix and no longer searches a relative file path with
a directory part in PATH directories. Patch by Thomas Kluyver.
Diffstat (limited to 'Lib/test/test_shutil.py')
-rw-r--r-- | Lib/test/test_shutil.py | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index ce37d3b..23fc1f9 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -1296,11 +1296,36 @@ class TestWhich(unittest.TestCase): rv = shutil.which(self.file, path=self.dir) self.assertEqual(rv, self.temp_file.name) - def test_full_path_short_circuit(self): + def test_absolute_cmd(self): # When given the fully qualified path to an executable that exists, # it should be returned. rv = shutil.which(self.temp_file.name, path=self.temp_dir) - self.assertEqual(self.temp_file.name, rv) + self.assertEqual(rv, self.temp_file.name) + + def test_relative_cmd(self): + # When given the relative path with a directory part to an executable + # that exists, it should be returned. + base_dir, tail_dir = os.path.split(self.dir) + relpath = os.path.join(tail_dir, self.file) + with support.temp_cwd(path=base_dir): + rv = shutil.which(relpath, path=self.temp_dir) + self.assertEqual(rv, relpath) + # But it shouldn't be searched in PATH directories (issue #16957). + with support.temp_cwd(path=self.dir): + rv = shutil.which(relpath, path=base_dir) + self.assertIsNone(rv) + + def test_cwd(self): + # Issue #16957 + base_dir = os.path.dirname(self.dir) + with support.temp_cwd(path=self.dir): + rv = shutil.which(self.file, path=base_dir) + if sys.platform == "win32": + # Windows: current directory implicitly on PATH + self.assertEqual(rv, os.path.join(os.curdir, self.file)) + else: + # Other platforms: shouldn't match in the current directory. + self.assertIsNone(rv) def test_non_matching_mode(self): # Set the file read-only and ask for writeable files. @@ -1308,15 +1333,11 @@ class TestWhich(unittest.TestCase): rv = shutil.which(self.file, path=self.dir, mode=os.W_OK) self.assertIsNone(rv) - def test_relative(self): - old_cwd = os.getcwd() + def test_relative_path(self): base_dir, tail_dir = os.path.split(self.dir) - os.chdir(base_dir) - try: + with support.temp_cwd(path=base_dir): rv = shutil.which(self.file, path=tail_dir) self.assertEqual(rv, os.path.join(tail_dir, self.file)) - finally: - os.chdir(old_cwd) def test_nonexistent_file(self): # Return None when no matching executable file is found on the path. |