def test_replace_in_file(self): tmp_dir = temp_folder() text_file = os.path.join(tmp_dir, "text.txt") save(text_file, "ONE TWO THREE") replace_in_file(text_file, "ONE TWO THREE", "FOUR FIVE SIX", output=TestBufferConanOutput()) self.assertEqual(load(text_file), "FOUR FIVE SIX")
def test_replace_in_file_readonly(self): tmp_dir = temp_folder() text_file = os.path.join(tmp_dir, "text.txt") save(text_file, "ONE TWO THREE") os.chmod( text_file, os.stat(text_file).st_mode & ~(stat.S_IWRITE | stat.S_IWGRP | stat.S_IWOTH)) mode_before_replace = os.stat(text_file).st_mode replace_in_file(text_file, "ONE TWO THREE", "FOUR FIVE SIX", output=TestBufferConanOutput()) self.assertEqual(load(text_file), "FOUR FIVE SIX") self.assertEqual(os.stat(text_file).st_mode, mode_before_replace) replace_path_in_file(text_file, "FOUR FIVE SIX", "SEVEN EIGHT NINE", output=TestBufferConanOutput()) self.assertEqual(load(text_file), "SEVEN EIGHT NINE") self.assertEqual(os.stat(text_file).st_mode, mode_before_replace)
def _set_revisions(self, value): current_conf = load(self.cache.conan_conf_path) if "revisions_enabled" in current_conf: # Invalidate any previous value to be sure replace_in_file(self.cache.conan_conf_path, "revisions_enabled", "#revisions_enabled", output=TestBufferConanOutput()) replace_in_file(self.cache.conan_conf_path, "[general]", "[general]\nrevisions_enabled = %s" % value, output=TestBufferConanOutput())
def __init__(self, base_folder=None, current_folder=None, servers=None, users=None, requester_class=None, runner=None, path_with_spaces=True, block_v2=None, revisions=None, cpu_count=1): """ storage_folder: Local storage path current_folder: Current execution folder servers: dict of {remote_name: TestServer} logins is a list of (user, password) for auto input in order if required==> [("lasote", "mypass"), ("other", "otherpass")] """ self.block_v2 = block_v2 or get_env("CONAN_API_V2_BLOCKED", True) if self.block_v2: self.revisions = False else: self.revisions = revisions or get_env("CONAN_CLIENT_REVISIONS_ENABLED", False) self.block_v2 = False self.all_output = "" # For debugging purpose, append all the run outputs self.users = users or {"default": [(TESTING_REMOTE_PRIVATE_USER, TESTING_REMOTE_PRIVATE_PASS)]} self.base_folder = base_folder or temp_folder(path_with_spaces) # Define storage_folder, if not, it will be read from conf file & pointed to real user home self.storage_folder = os.path.join(self.base_folder, ".conan", "data") self.cache = ClientCache(self.base_folder, self.storage_folder, TestBufferConanOutput()) self.requester_class = requester_class self.conan_runner = runner if cpu_count: self.cache.conan_config replace_in_file(self.cache.conan_conf_path, "# cpu_count = 1", "cpu_count = %s" % cpu_count, output=TestBufferConanOutput(), strict=not bool(base_folder)) # Invalidate the cached config self.cache.invalidate() if servers and len(servers) > 1 and not isinstance(servers, OrderedDict): raise Exception("""Testing framework error: Servers should be an OrderedDict. e.g: servers = OrderedDict() servers["r1"] = server servers["r2"] = TestServer() """) self.servers = servers or {} if servers is not False: # Do not mess with registry remotes self.update_servers() self.init_dynamic_vars() logger.debug("Client storage = %s" % self.storage_folder) self.current_folder = current_folder or temp_folder(path_with_spaces)
def test_replace_in_file(self): output = ConanOutput(sys.stdout) replace_in_file(self.win_file, "nis", "nus", output=output) replace_in_file(self.bytes_file, "nis", "nus", output=output) content = tools.load(self.win_file) self.assertNotIn("nis", content) self.assertIn("nus", content) content = tools.load(self.bytes_file) self.assertNotIn("nis", content) self.assertIn("nus", content)
def test_cache_config(self): file_path = os.path.join(temp_folder(), "whatever_cacert") save(file_path, "dummy") requester, mocked_requester, cache = self._create_requesters() replace_in_file(cache.conan_conf_path, "# cacert_path", "cacert_path={}".format(file_path), output=TestBufferConanOutput()) cache.invalidate() requester.get(url="bbbb", verify=True) self.assertEqual(mocked_requester.verify, file_path)
def test_cache_config(self): file_path = os.path.join(temp_folder(), "whatever_cacert") save(file_path, "") conan_conf = os.path.join(temp_folder(), "conan.conf") save(conan_conf, normalize(default_client_conf)) replace_in_file(conan_conf, "# cacert_path", "cacert_path={}".format(file_path), output=TestBufferConanOutput()) config = ConanClientConfigParser(conan_conf) mocked_requester = MockRequesterGet() requester = ConanRequester(config, mocked_requester) requester.get(url="bbbb", verify=True) self.assertEqual(mocked_requester.verify, file_path)
def tune_conan_conf(self, cache_folder, cpu_count, revisions_enabled): # Create the default cache = self.cache _ = cache.config if cpu_count: replace_in_file(cache.conan_conf_path, "# cpu_count = 1", "cpu_count = %s" % cpu_count, output=TestBufferConanOutput(), strict=not bool(cache_folder)) if revisions_enabled is not None: self._set_revisions(revisions_enabled) elif "TESTING_REVISIONS_ENABLED" in os.environ: value = get_env("TESTING_REVISIONS_ENABLED", True) self._set_revisions(value)
def test_cache_config(self): cache = ClientCache(temp_folder(), TestBufferConanOutput()) file_path = os.path.join(cache.cache_folder, "whatever_cacert") replace_in_file(cache.conan_conf_path, "# cacert_path", "cacert_path={}".format(file_path), output=TestBufferConanOutput()) save(file_path, "") cache.invalidate() requester = ConanRequester(cache.config) mocked_requester = MockRequesterGet() requester._http_requester = mocked_requester requester.get(url="bbbb", verify=True) self.assertEqual(mocked_requester.verify, file_path)
def tune_conan_conf(self, base_folder, cpu_count, revisions_enabled): # Create the default self.cache.config if cpu_count: replace_in_file(self.cache.conan_conf_path, "# cpu_count = 1", "cpu_count = %s" % cpu_count, output=TestBufferConanOutput(), strict=not bool(base_folder)) current_conf = load(self.cache.conan_conf_path) if "revisions_enabled" in current_conf: # Invalidate any previous value to be sure replace_in_file(self.cache.conan_conf_path, "revisions_enabled", "#revisions_enabled", output=TestBufferConanOutput()) if revisions_enabled: replace_in_file(self.cache.conan_conf_path, "[general]", "[general]\nrevisions_enabled = 1", output=TestBufferConanOutput()) # Invalidate the cached config self.cache.invalidate()
def double_alias_options_test(self, use_requires): # https://github.com/conan-io/conan/issues/2583 client = TestClient() if use_requires: conanfile = """from conans import ConanFile class Pkg(ConanFile): requires = "%s" options = {"myoption": [True, False]} default_options = "myoption=True" def package_info(self): self.output.info("MYOPTION: {} {}".format(self.name, self.options.myoption)) """ else: conanfile = """from conans import ConanFile class Pkg(ConanFile): options = {"myoption": [True, False]} default_options = "myoption=True" def configure(self): if self.name == "LibB": self.options["LibD"].myoption = False def requirements(self): req = "%s" if req: self.requires(req) def package_info(self): self.output.info("MYOPTION: {} {}".format(self.name, self.options.myoption)) """ client.save({"conanfile.py": conanfile % ""}, clean_first=True) client.run("export . LibD/0.1@user/testing") client.run("alias LibD/latest@user/testing LibD/0.1@user/testing") client.save({"conanfile.py": conanfile % "LibD/latest@user/testing"}) client.run("export . LibC/0.1@user/testing") client.run("alias LibC/latest@user/testing LibC/0.1@user/testing") client.save({"conanfile.py": conanfile % "LibC/latest@user/testing"}) replace_in_file(os.path.join(client.current_folder, "conanfile.py"), '"myoption=True"', '"myoption=True", "LibD:myoption=False"', output=client.out) client.run("export . LibB/0.1@user/testing") client.run("alias LibB/latest@user/testing LibB/0.1@user/testing") client.save({"conanfile.py": conanfile % "LibC/latest@user/testing"}) client.run("export . LibA/0.1@user/testing") client.run("alias LibA/latest@user/testing LibA/0.1@user/testing") client.save( { "conanfile.txt": "[requires]\nLibA/latest@user/testing\nLibB/latest@user/testing" }, clean_first=True) client.run("info conanfile.txt --graph=file.dot") graphfile = load(os.path.join(client.current_folder, "file.dot")) self.assertIn('"LibA/0.1@user/testing" -> {"LibC/0.1@user/testing"}', graphfile) self.assertIn('"LibB/0.1@user/testing" -> {"LibC/0.1@user/testing"}', graphfile) self.assertIn('"LibC/0.1@user/testing" -> {"LibD/0.1@user/testing"}', graphfile) self.assertTrue(( '"conanfile.txt" -> {"LibB/0.1@user/testing" "LibA/0.1@user/testing"}' in graphfile ) or ( '"conanfile.txt" -> {"LibA/0.1@user/testing" "LibB/0.1@user/testing"}' in graphfile)) client.run("install conanfile.txt --build=missing") self.assertIn("LibD/0.1@user/testing: MYOPTION: LibD False", client.out) self.assertIn("LibB/0.1@user/testing: MYOPTION: LibB True", client.out) self.assertIn("LibA/0.1@user/testing: MYOPTION: LibA True", client.out) self.assertIn("LibC/0.1@user/testing: MYOPTION: LibC True", client.out)
def test_build_vs_project(self): conan_build_vs = """ from conans import ConanFile, MSBuild class HelloConan(ConanFile): name = "Hello" version = "1.2.1" exports = "*" settings = "os", "build_type", "arch", "compiler", "cppstd" def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln", verbosity="normal") def package(self): self.copy(pattern="*.exe") """ client = TestClient() # Test cpp standard stuff files = get_vs_project_files(std="cpp17_2015") files[CONANFILE] = conan_build_vs client.save(files) client.run('create . Hello/1.2.1@lasote/stable -s cppstd=11 -s ' 'compiler="Visual Studio" -s compiler.version=14', assert_error=True) client.run('create . Hello/1.2.1@lasote/stable -s cppstd=17 ' '-s compiler="Visual Studio" -s compiler.version=14') self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) files = get_vs_project_files() files[CONANFILE] = conan_build_vs # Try to not update the project client.cache._config = None # Invalidate cached config replace_in_file(client.cache.conan_conf_path, "[general]", "[general]\nskip_vs_projects_upgrade = True", output=client.out) client.save(files, clean_first=True) client.run("create . Hello/1.2.1@lasote/stable --build") self.assertNotIn("devenv", client.out) self.assertIn("Skipped sln project upgrade", client.out) # Try with x86_64 client.save(files) client.run("export . lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86_64") self.assertIn("Release|x64", client.out) self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) # Try with x86 client.save(files, clean_first=True) client.run("export . lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86") self.assertIn("Release|x86", client.out) self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) # Try with x86 debug client.save(files, clean_first=True) client.run("export . lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86 -s build_type=Debug") self.assertIn("Debug|x86", client.out) self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) # Try with a custom property file name files[CONANFILE] = conan_build_vs.replace( 'msbuild.build("MyProject.sln", verbosity="normal")', 'msbuild.build("MyProject.sln", verbosity="normal", property_file_name="mp.props")') client.save(files, clean_first=True) client.run("create . Hello/1.2.1@lasote/stable --build -s arch=x86 -s build_type=Debug") self.assertIn("Debug|x86", client.out) self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) full_ref = "Hello/1.2.1@lasote/stable:b786e9ece960c3a76378ca4d5b0d0e922f4cedc1" pref = PackageReference.loads(full_ref) build_folder = client.cache.package_layout(pref.ref).build(pref) self.assertTrue(os.path.exists(os.path.join(build_folder, "mp.props")))
def replace_in_file(*args, **kwargs): return tools_files.replace_in_file(output=_global_output, *args, **kwargs)
def double_alias_options_test(self, use_requires): # https://github.com/conan-io/conan/issues/2583 client = TestClient() if use_requires: conanfile = """from conans import ConanFile class Pkg(ConanFile): requires = "%s" options = {"myoption": [True, False]} default_options = "myoption=True" def package_info(self): self.output.info("MYOPTION: {} {}".format(self.name, self.options.myoption)) """ else: conanfile = """from conans import ConanFile class Pkg(ConanFile): options = {"myoption": [True, False]} default_options = "myoption=True" def configure(self): if self.name == "LibB": self.options["LibD"].myoption = False def requirements(self): req = "%s" if req: self.requires(req) def package_info(self): self.output.info("MYOPTION: {} {}".format(self.name, self.options.myoption)) """ client.save({"conanfile.py": conanfile % ""}, clean_first=True) client.run("export . LibD/0.1@user/testing") client.run("alias LibD/latest@user/testing LibD/0.1@user/testing") client.save({"conanfile.py": conanfile % "LibD/latest@user/testing"}) client.run("export . LibC/0.1@user/testing") client.run("alias LibC/latest@user/testing LibC/0.1@user/testing") client.save({"conanfile.py": conanfile % "LibC/latest@user/testing"}) replace_in_file(os.path.join(client.current_folder, "conanfile.py"), '"myoption=True"', '"myoption=True", "LibD:myoption=False"') client.run("export . LibB/0.1@user/testing") client.run("alias LibB/latest@user/testing LibB/0.1@user/testing") client.save({"conanfile.py": conanfile % "LibC/latest@user/testing"}) client.run("export . LibA/0.1@user/testing") client.run("alias LibA/latest@user/testing LibA/0.1@user/testing") client.save({"conanfile.txt": "[requires]\nLibA/latest@user/testing\nLibB/latest@user/testing"}, clean_first=True) client.run("info conanfile.txt --graph=file.dot") graphfile = load(os.path.join(client.current_folder, "file.dot")) self.assertIn('"LibA/0.1@user/testing" -> {"LibC/0.1@user/testing"}', graphfile) self.assertIn('"LibB/0.1@user/testing" -> {"LibC/0.1@user/testing"}', graphfile) self.assertIn('"LibC/0.1@user/testing" -> {"LibD/0.1@user/testing"}', graphfile) self.assertTrue(('"PROJECT" -> {"LibB/0.1@user/testing" "LibA/0.1@user/testing"}' in graphfile) or ('"PROJECT" -> {"LibA/0.1@user/testing" "LibB/0.1@user/testing"}' in graphfile)) client.run("install conanfile.txt --build=missing") self.assertIn("LibD/0.1@user/testing: MYOPTION: LibD False", client.out) self.assertIn("LibB/0.1@user/testing: MYOPTION: LibB True", client.out) self.assertIn("LibA/0.1@user/testing: MYOPTION: LibA True", client.out) self.assertIn("LibC/0.1@user/testing: MYOPTION: LibC True", client.out)