def test_find_program(self): # If we add the path, we should found the env/executable instead of ori/executable # Watch out! 'cmd' returns all the paths where the executable is found, so we need to # take into account the first match (iterate in reverse order) generator = VirtualEnvGenerator(ConanFileMock()) generator.env = {"PATH": [self.env_path], } stdout, environment = self._run_virtualenv(generator) cpaths = dict(l.split("=", 1) for l in reversed(stdout.splitlines()) if l.startswith("__conan_")) self.assertEqual(cpaths["__conan_pre_path__"], cpaths["__conan_post_path__"]) self.assertEqual(cpaths["__conan_env_path__"], cpaths["__conan_post_path__"]) epaths = dict(l.split("=", 1) for l in reversed(stdout.splitlines()) if l.startswith("__exec_")) self.assertEqual(epaths["__exec_pre_path__"], epaths["__exec_post_path__"]) self.assertEqual(epaths["__exec_env_path__"], os.path.join(self.env_path, self.app)) # With any other path, we keep finding the original one generator = VirtualEnvGenerator(ConanFileMock()) generator.env = {"PATH": [os.path.join(self.test_folder, "wrong")], } stdout, environment = self._run_virtualenv(generator) epaths = dict(l.split("=", 1) for l in reversed(stdout.splitlines()) if l.startswith("__exec_")) self.assertEqual(epaths["__exec_pre_path__"], epaths["__exec_post_path__"]) self.assertEqual(epaths["__exec_env_path__"], epaths["__exec_post_path__"])
def test_list_variable(self): self.assertNotIn("WHATEVER", os.environ) self.assertIn("PATH", os.environ) existing_path = os.environ.get("PATH") # Avoid duplicates in the path existing_path = os.pathsep.join( OrderedDict.fromkeys(existing_path.split(os.pathsep))) generator = VirtualEnvGenerator(ConanFileMock()) generator.env = { "PATH": [os.path.join(self.test_folder, "bin"), r'other\path'], "WHATEVER": ["list", "other"] } _, environment = self._run_virtualenv(generator) self.assertEqual( environment["PATH"], os.pathsep.join([ os.path.join(self.test_folder, "bin"), r'other\path', self.ori_path, existing_path ])) # FIXME: extra separator in Windows extra_separator = os.pathsep if platform.system() == "Windows" else "" self.assertEqual( environment["WHATEVER"], "{}{}{}{}".format("list", os.pathsep, "other", extra_separator))
def test_find_program(self): # If we add the path, we should found the env/executable instead of ori/executable generator = VirtualEnvGenerator(ConanFileMock()) generator.env = { "PATH": [self.env_path], } stdout, environment = self._run_virtualenv(generator) cpaths = dict( l.split("=", 1) for l in stdout.splitlines() if l.startswith("__conan_")) self.assertEqual(cpaths["__conan_pre_path__"], cpaths["__conan_post_path__"]) self.assertEqual(cpaths["__conan_env_path__"], cpaths["__conan_post_path__"]) epaths = dict( l.split("=", 1) for l in stdout.splitlines() if l.startswith("__exec_")) self.assertEqual(epaths["__exec_pre_path__"], epaths["__exec_post_path__"]) if self.commands.id == "cmd": # FIXME: This is a bug, it doesn't take into account the new path self.assertNotEqual(epaths["__exec_env_path__"], os.path.join(self.env_path, self.app)) else: self.assertEqual(epaths["__exec_env_path__"], os.path.join(self.env_path, self.app)) # With any other path, we keep finding the original one generator = VirtualEnvGenerator(ConanFileMock()) generator.env = { "PATH": [os.path.join(self.test_folder, "wrong")], } stdout, environment = self._run_virtualenv(generator) epaths = dict( l.split("=", 1) for l in stdout.splitlines() if l.startswith("__exec_")) self.assertEqual(epaths["__exec_pre_path__"], epaths["__exec_post_path__"]) self.assertEqual(epaths["__exec_env_path__"], epaths["__exec_post_path__"])
def test_empty_undefined_list_variable(self): self.assertNotIn("WHATEVER", os.environ) generator = VirtualEnvGenerator(ConanFileMock()) generator.env = {"WHATEVER": ["list", "other"]} _, environment = self._run_virtualenv(generator) # FIXME: extra separator in Windows extra_separator = os.pathsep if platform.system() == "Windows" else "" self.assertEqual(environment["WHATEVER"], "{}{}{}{}".format("list", os.pathsep, "other", extra_separator))
def test_empty_defined_list_variable(self): self.assertNotIn("WHATEVER", os.environ) try: os.environ["WHATEVER"] = "" generator = VirtualEnvGenerator(ConanFileMock()) generator.env = {"WHATEVER": ["list", "other"]} _, environment = self._run_virtualenv(generator) self.assertEqual(environment["WHATEVER"], "{}{}{}".format("list", os.pathsep, "other")) finally: del os.environ["WHATEVER"]
def test_basic_variable(self): generator = VirtualEnvGenerator(ConanFileMock()) generator.env = {"USER_VAR": r"some value with space and \ (backslash)", "ANOTHER": "data"} _, environment = self._run_virtualenv(generator) self.assertEqual(environment["USER_VAR"], r"some value with space and \ (backslash)") self.assertEqual(environment["ANOTHER"], "data") with environment_append({"USER_VAR": "existing value", "ANOTHER": "existing_value"}): _, environment = self._run_virtualenv(generator) self.assertEqual(environment["USER_VAR"], r"some value with space and \ (backslash)") self.assertEqual(environment["ANOTHER"], "data")
def test_list_with_spaces(self): generator = VirtualEnvGenerator(ConanFileMock()) self.assertIn("CFLAGS", VirtualEnvGenerator.append_with_spaces) self.assertIn("CL", VirtualEnvGenerator.append_with_spaces) generator.env = {"CFLAGS": ["-O2"], "CL": ["-MD", "-DNDEBUG", "-O2", "-Ob2"]} _, environment = self._run_virtualenv(generator) self.assertEqual(environment["CFLAGS"], "-O2 ") # FIXME: Trailing blank self.assertEqual(environment["CL"], "-MD -DNDEBUG -O2 -Ob2 ") # FIXME: Trailing blank with environment_append({"CFLAGS": "cflags", "CL": "cl"}): _, environment = self._run_virtualenv(generator) extra_blank = " " if platform.system() != "Windows" else "" # FIXME: Extra blank self.assertEqual(environment["CFLAGS"], "-O2 {}cflags".format(extra_blank)) self.assertEqual(environment["CL"], "-MD -DNDEBUG -O2 -Ob2 {}cl".format(extra_blank))
def test_list_variable(self): self.assertIn("PATH", os.environ) existing_path = os.environ.get("PATH") # Avoid duplicates in the path existing_path = os.pathsep.join(OrderedDict.fromkeys(existing_path.split(os.pathsep))) generator = VirtualEnvGenerator(ConanFileMock()) generator.env = {"PATH": [os.path.join(self.test_folder, "bin"), r'other\path']} _, environment = self._run_virtualenv(generator) self.assertEqual(environment["PATH"], os.pathsep.join([ os.path.join(self.test_folder, "bin"), r'other\path', self.ori_path, existing_path ]))