def test_file_not_in_path_not_found(self): """Check that executables not in PATH are not found.""" temp_dir = tempfile.mkdtemp(prefix=os.path.join(os.getcwd(), "executable_path")) self.addCleanup(lambda: util.force_remove_tree(temp_dir)) with tempfile.NamedTemporaryFile(mode="wt", dir=temp_dir) as temp_file: temp_file.write("#!/usr/bin/env python\nprint(\"Test\")") os.chmod(temp_file.name, 755) os.environ["PATH"] = "" self.assertEqual(None, util.which(os.path.basename(temp_file.name)))
def test_non_executable_file_not_found(self): """Don't find a non executable file in the current PATH.""" if platform.system() == "Windows": self.skipTest("no such thing as execute permission on Windows") with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir: os.environ["PATH"] = (temp_dir + os.pathsep) with tempfile.NamedTemporaryFile(mode="wt", dir=temp_dir) as temp_file: temp_file.write("#!/usr/bin/env python\nprint(\"Test\")") self.assertEqual(None, util.which(os.path.basename(temp_file.name)))
def test_find_executable_file_in_path(self): """Find an executable file in the current PATH.""" with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir: os.environ["PATH"] = (temp_dir + os.pathsep + (os.environ.get("PATH") or "")) with tempfile.NamedTemporaryFile(mode="wt", dir=temp_dir) as temp_file: temp_file.write("#!/usr/bin/env python\nprint(\"Test\")") os.chmod(temp_file.name, os.stat(temp_file.name).st_mode | stat.S_IRWXU) which_result = util.which(os.path.basename(temp_file.name)) self.assertEqual(temp_file.name.lower(), which_result.lower())
def test_resolve_relative_paths(self): """Resolve relative paths in PATH.""" with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir: path_var = (os.environ.get("PATH") or "") base = os.path.basename(temp_dir) os.environ["PATH"] = "{0}/../{1}{2}{3}".format(temp_dir, base, os.pathsep, path_var) with tempfile.NamedTemporaryFile(mode="wt", dir=temp_dir) as temp_file: temp_file.write("#!/usr/bin/env python\nprint(\"Test\")") os.chmod(temp_file.name, 755) which_result = util.which(os.path.basename(temp_file.name)) self.assertEqual(temp_file.name.lower(), which_result.lower())
def test_find_executable_file_using_pathext(self): """Find an executable file using PATHEXT.""" with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir: os.environ["PATH"] = (temp_dir + os.pathsep + (os.environ.get("PATH") or "")) os.environ["PATHEXT"] = ".exe" with tempfile.NamedTemporaryFile(mode="wt", dir=temp_dir, suffix=".exe") as temp_file: temp_file.write("#!/usr/bin/env python\nprint(\"Test\")") os.chmod(temp_file.name, 755) name = os.path.splitext(os.path.basename(temp_file.name))[0] which_result = util.which(name) self.assertEqual(temp_file.name.lower(), which_result.lower())
def _copytree_ignore_notfound(src, dst): """Copy an entire directory tree. This is effectively a workaround for situations where shutil.copytree is unable to copy some files. Just shell out to rsync, as rsync usually always gets it right in complicated cases. Where rsync isn't available, then we'll need to fallback to shutil. """ if util.which("rsync"): subprocess.check_call(["rsync", "-az", "--chmod=ugo=rwX", src + os.path.sep, dst + os.path.sep]) else: try: shutil.copytree(src, dst) except shutil.Error: # suppress(pointless-except) pass
def test_symlinks_in_path_get_resolved(self): """Returned executable path has symlinks resolved.""" if platform.system() == "Windows": self.skipTest("symlinks not supported on Windows") with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir: link = os.path.join(temp_dir, "link") linked = os.path.join(temp_dir, "linked") os.mkdir(linked) os.symlink(linked, link) path_var = (os.environ.get("PATH") or "") os.environ["PATH"] = link + os.pathsep + path_var with tempfile.NamedTemporaryFile(mode="wt", dir=linked) as temp_file: temp_file.write("#!/usr/bin/env python\nprint(\"Test\")") os.chmod(temp_file.name, 755) self.assertEqual(temp_file.name, util.which(os.path.basename(temp_file.name)))
def test_program_is_available_in_python_script(self, program): """Executable {0} is available after running setup.""" temp_dir = self.__class__.container_temp_dir with self.__class__.lang_container.activated(util): self.assertThat(util.which(program), IsInSubdirectoryOf(temp_dir))
def _require(self, req): """Skip test if req is not available.""" if util.which(req) is None: self.skipTest("""{0} is required to run this test.""".format(req))