def test_download(self, bottle_server): dest = os.path.join(temp_folder(), "manual.html") conanfile = ConanFileMock() conanfile._conan_requester = requests download(conanfile, "http://localhost:%s/manual.html" % bottle_server.port, dest, retry=3, retry_wait=0) content = load(dest) assert content == "this is some content" # Can re-download download(conanfile, "http://localhost:%s/manual.html" % bottle_server.port, dest, retry=3, retry_wait=0)
def test_sysrootflag(): """Even when no cross building it is adjusted because it could target a Mac version""" conanfile = ConanFileMock() conanfile.conf.define("tools.build:sysroot", "/path/to/sysroot") conanfile.settings = MockSettings({ "build_type": "Debug", "os": { "Darwin": "Macos" }.get(platform.system(), platform.system()), "arch": "x86_64" }) be = AutotoolsToolchain(conanfile) expected = "--sysroot /path/to/sysroot" assert be.sysroot_flag == expected env = be.vars() assert expected in env["CXXFLAGS"] assert expected in env["CFLAGS"] assert expected in env["LDFLAGS"]
def flags_applied_test(self, the_os, compiler, version, arch, runtime, flag): settings = Settings.loads(get_default_settings_yml()) settings.os = the_os settings.compiler = compiler settings.compiler.version = version settings.arch = arch if runtime: settings.compiler.runtime = runtime settings.build_type = "Release" conan_file = ConanFileMock() conan_file.deps_cpp_info = MockDepsCppInfo() conan_file.settings = settings conan_file.package_folder = None meson = Meson(conan_file) meson.configure() meson.build() self.assertIn(flag, conan_file.captured_env["CFLAGS"]) self.assertIn(flag, conan_file.captured_env["CXXFLAGS"])
def test_rename_folder_robocopy(): conanfile = ConanFileMock() tmp = temp_folder() old_folder = os.path.join(tmp, "old_folder") os.mkdir(old_folder) new_folder = os.path.join(tmp, "new_folder") rename(conanfile, old_folder, new_folder) assert not os.path.exists(old_folder) assert os.path.exists(new_folder)
def test_arch_override(self): settings = MockSettings({"build_type": "Release", "compiler": "Visual Studio", "compiler.version": "15", "compiler.runtime": "MDd", "os": "Windows", "arch": "x86_64"}) conanfile = ConanFileMock() conanfile.settings = settings props_file_path = os.path.join(temp_folder(), "conan_build.props") msbuild = MSBuild(conanfile) msbuild.build("project_file.sln", property_file_name=props_file_path) self.assertIn("vcvarsall.bat\" amd64", conanfile.command) self.assertIn("/p:Platform=\"x64\"", conanfile.command) msbuild.build("project_file.sln", arch="x86", property_file_name=props_file_path) self.assertIn("vcvarsall.bat\" x86", conanfile.command) self.assertIn("/p:Platform=\"x86\"", conanfile.command)
def test_modify_environment(): """We can alter the environment generated by the toolchain passing the env to the generate""" f = temp_folder() chdir(f) conanfile = ConanFileMock() conanfile.settings = MockSettings({"os": "Linux", "arch": "x86_64"}) conanfile.settings_build = MockSettings({"os": "Solaris", "arch": "x86"}) be = AutotoolsToolchain(conanfile) env = be.environment() env.define("foo", "var") # We can pass the env to the generate once we adjusted or injected anything be.generate(env) bat = "conanautotoolstoolchain.{}".format("bat" if platform.system() == "Windows" else "sh") with open(bat) as f: content = f.read() assert "foo" in content
def test_custom_cflags(): conanfile = ConanFileMock() conanfile.conf = {"tools.apple:sdk_path": "/path/to/sdk"} conanfile.settings = MockSettings({ "build_type": "RelWithDebInfo", "os": "iOS", "os.version": "14", "arch": "armv8" }) be = AutotoolsToolchain(conanfile) be.cflags = ["MyFlag1", "MyFlag2"] env = be.vars() assert "MyFlag1" in env["CFLAGS"] assert "MyFlag2" in env["CFLAGS"] assert "-mios-version-min=14" in env["CFLAGS"] assert "MyFlag" not in env["CXXFLAGS"] assert "MyFlag" not in env["LDFLAGS"]
def test_rename_file(): conanfile = ConanFileMock() tmp = temp_folder() save_files(tmp, {"file.txt": ""}) old_path = os.path.join(tmp, "file.txt") new_path = os.path.join(tmp, "kk.txt") rename(conanfile, old_path, new_path) assert not os.path.exists(old_path) assert os.path.exists(new_path)
def test_get_gnu_triplet_for_cross_building(): """ Testing AutotoolsToolchain and _get_gnu_triplet() function in case of having os=Windows and cross compiling """ # Issue: https://github.com/conan-io/conan/issues/10139 settings = MockSettings({ "build_type": "Release", "compiler": "gcc", "compiler.version": "10.2", "os": "Windows", "arch": "x86_64" }) conanfile = ConanFileMock() conanfile.settings = settings conanfile.settings_build = MockSettings({"os": "Solaris", "arch": "x86"}) autotoolschain = AutotoolsToolchain(conanfile) assert autotoolschain._host == "x86_64-w64-mingw32" assert autotoolschain._build == "i686-solaris"
def test_get_gnu_triplet_for_cross_building_raise_error(): """ Testing AutotoolsToolchain and _get_gnu_triplet() function raises an error in case of having os=Windows, cross compiling and not defined any compiler """ # Issue: https://github.com/conan-io/conan/issues/10139 settings = MockSettings({ "build_type": "Release", "os": "Windows", "arch": "x86_64" }) conanfile = ConanFileMock() conanfile.settings = settings conanfile.settings_build = MockSettings({"os": "Solaris", "arch": "x86"}) with pytest.raises(ConanException) as conan_error: AutotoolsToolchain(conanfile) msg = "'compiler' parameter for 'get_gnu_triplet()' is not specified and " \ "needed for os=Windows" assert msg == str(conan_error.value)
def test_extra_flags_via_conf(): conanfile = ConanFileMock() conanfile.conf.define("tools.build:cxxflags", ["--flag1", "--flag2"]) conanfile.conf.define("tools.build:cflags", ["--flag3", "--flag4"]) conanfile.conf.define("tools.build:sharedlinkflags", ["--flag5"]) conanfile.conf.define("tools.build:exelinkflags", ["--flag6"]) conanfile.conf.define("tools.build:defines", ["DEF1", "DEF2"]) conanfile.settings = MockSettings({ "build_type": "RelWithDebInfo", "os": "iOS", "os.version": "14", "arch": "armv8" }) be = AutotoolsToolchain(conanfile) env = be.vars() assert '-DNDEBUG -DDEF1 -DDEF2' in env["CPPFLAGS"] assert '-mios-version-min=14 --flag1 --flag2' in env["CXXFLAGS"] assert '-mios-version-min=14 --flag3 --flag4' in env["CFLAGS"] assert '-mios-version-min=14 --flag5 --flag6' in env["LDFLAGS"]
def test_target_triple(): f = temp_folder() chdir(f) conanfile = ConanFileMock() conanfile.settings = MockSettings({"os": "Linux", "arch": "x86_64"}) conanfile.settings_build = MockSettings({"os": "Solaris", "arch": "x86"}) conanfile.conf = Conf() conanfile.conf["tools.gnu:make_program"] = "my_make" conanfile.conf["tools.gnu.make:jobs"] = "23" be = AutotoolsToolchain(conanfile) be.make_args = ["foo", "var"] be.generate_args() with open(CONAN_TOOLCHAIN_ARGS_FILE) as f: obj = json.load(f) assert "--host=x86_64-linux-gnu" in obj["configure_args"] assert "--build=i686-solaris" in obj["configure_args"] assert obj["make_args"].replace("'", "") == "foo var"
def test_visual_runtime(runtime): """ Testing AutotoolsToolchain with the msvc compiler adjust the runtime """ # Issue: https://github.com/conan-io/conan/issues/10139 settings = MockSettings({"build_type": "Release", "compiler": "Visual Studio", "compiler.runtime": runtime, "os": "Windows", "arch": "x86_64"}) conanfile = ConanFileMock() conanfile.settings = settings conanfile.settings_build = settings autotoolschain = AutotoolsToolchain(conanfile) expected_flag = "-{}".format(runtime) assert autotoolschain.msvc_runtime_flag == expected_flag env = autotoolschain.environment().vars(conanfile) assert expected_flag in env["CFLAGS"] assert expected_flag in env["CXXFLAGS"]
def test_setvars_command_with_custom_arguments(platform_system, os_, call_command, setvars_file): platform_system.return_value = os_ conanfile = ConanFileMock() conanfile.settings = MockSettings({ "compiler.version": "2021.3", "compiler.mode": "icx", "os": os_ }) fake_path = "mysuper/path/to/intel/oneapi" args = "arg1 arg2 --force" conanfile.conf = ConfDefinition() conanfile.conf.loads( textwrap.dedent("""\ tools.intel:installation_path=%s tools.intel:setvars_args=%s """ % (fake_path, args))) expected = '%s "%s" %s' % (call_command, os.path.join(fake_path, setvars_file), args) assert IntelCC(conanfile).command == expected
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_compose(): env = Environment(ConanFileMock()) env.define("MyVar", "MyValue") env.define("MyVar2", "MyValue2") env.define("MyVar3", "MyValue3") env.define("MyVar4", "MyValue4") env.unset("MyVar5") env2 = Environment(ConanFileMock()) env2.define("MyVar", "MyNewValue") env2.append("MyVar2", "MyNewValue2") env2.prepend("MyVar3", "MyNewValue3") env2.unset("MyVar4") env2.define("MyVar5", "MyNewValue5") env.compose_env(env2) assert env.get("MyVar") == "MyValue" assert env.get("MyVar2") == 'MyValue2' assert env.get("MyVar3") == 'MyValue3' assert env.get("MyVar4") == "MyValue4" assert env.get("MyVar5") == ''
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_meson_build(): c = ConfDefinition() c.loads(textwrap.dedent("""\ tools.ninja:jobs=23 tools.build:processes=10 """)) settings = MockSettings({"build_type": "Release", "compiler": "gcc", "compiler.version": "7", "os": "Linux", "arch": "x86_64"}) conanfile = ConanFileMock() conanfile.settings = settings conanfile.display_name = 'test' conanfile.conf = c.get_conanfile_conf(None) meson = Meson(conanfile) meson.build() assert '-j23' in str(conanfile.command)
def test_run_vars(self): conanfile = ConanFileMock() conanfile.deps_cpp_info["one"].bin_paths.append("path/bin") conanfile.deps_cpp_info["two"].lib_paths.append("path/libs") be = RunEnvironment(conanfile) self.assertEqual( be.vars, { 'PATH': ['path/bin'], 'LD_LIBRARY_PATH': ['path/libs'], 'DYLD_LIBRARY_PATH': ['path/libs'] })
def test_cppstd(): # Using "cppstd" is discarded conanfile = ConanFileMock() conanfile.settings = MockSettings({ "build_type": "Release", "arch": "x86", "compiler": "gcc", "compiler.libcxx": "libstdc++11", "compiler.version": "7.1", "cppstd": "17" }) be = AutotoolsToolchain(conanfile) env = be.environment() assert not "-std=c++17" in env["CXXFLAGS"] # Using "compiler.cppstd" works conanfile.settings = MockSettings({ "build_type": "Release", "arch": "x86", "compiler": "gcc", "compiler.libcxx": "libstdc++11", "compiler.version": "7.1", "compiler.cppstd": "17" }) be = AutotoolsToolchain(conanfile) env = be.environment() assert "-std=c++17" in env["CXXFLAGS"] # With visual conanfile.settings = MockSettings({ "build_type": "Release", "arch": "x86", "compiler": "Visual Studio", "compiler.version": "14", "compiler.cppstd": "17" }) be = AutotoolsToolchain(conanfile) env = be.environment() assert "/std:c++latest" in env["CXXFLAGS"]
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 env(): # Append and define operation over the same variable in Windows preserve order env = Environment() env.define("MyVar", "MyValueA") env.define("MYVAR", "MyValueB") env.define("MyVar1", "MyValue1A") env.append("MYVAR1", "MyValue1B") env.define("MyVar2", "MyNewValue2") env = env.vars(ConanFileMock()) env._subsystem = WINDOWS return env
def test_msbuild_cpu_count(): c = ConfDefinition() c.loads( textwrap.dedent("""\ tools.microsoft.msbuild:max_cpu_count=23 """)) settings = MockSettings({ "build_type": "Release", "compiler": "gcc", "compiler.version": "7", "os": "Linux", "arch": "x86_64" }) conanfile = ConanFileMock() conanfile.settings = settings conanfile.conf = c.get_conanfile_conf(None) msbuild = MSBuild(conanfile) cmd = msbuild.command('project.sln') assert '/m:23' in cmd
def test_linkame_striproot_folder(self): tmp_folder = temp_folder() other_tmp_folder = temp_folder() save(os.path.join(other_tmp_folder, "foo.txt"), "") tgz_path = os.path.join(tmp_folder, "foo.tgz") with open(tgz_path, "wb") as tgz_handle: tgz = gzopen_without_timestamps("name", mode="w", fileobj=tgz_handle) # Regular file info = tarfile.TarInfo(name="common/foo.txt") info.name = "common/subfolder/foo.txt" info.path = "common/subfolder/foo.txt" with open(os.path.join(other_tmp_folder, "foo.txt"), 'rb') as file_handler: tgz.addfile(tarinfo=info, fileobj=file_handler) # A hardlink to the regular file info = tarfile.TarInfo(name="common/foo.txt") info.linkname = "common/subfolder/foo.txt" info.linkpath = "common/subfolder/foo.txt" info.name = "common/subfolder/bar/foo.txt" info.path = "common/subfolder/bar/foo.txt" info.type = b'1' # This indicates a hardlink to the tgz file "common/subfolder/foo.txt" tgz.addfile(tarinfo=info, fileobj=None) tgz.close() assert not os.path.exists( os.path.join(tmp_folder, "subfolder", "foo.txt")) assert not os.path.exists( os.path.join(tmp_folder, "subfolder", "bar", "foo.txt")) untargz(tgz_path, destination=tmp_folder, strip_root=True) assert os.path.exists(os.path.join(tmp_folder, "subfolder", "foo.txt")) assert os.path.exists( os.path.join(tmp_folder, "subfolder", "bar", "foo.txt")) # Check develop2 public unzip rmdir(os.path.join(tmp_folder, "subfolder")) assert not os.path.exists( os.path.join(tmp_folder, "subfolder", "foo.txt")) assert not os.path.exists( os.path.join(tmp_folder, "subfolder", "bar", "foo.txt")) unzip_dev2(ConanFileMock(), tgz_path, destination=tmp_folder, strip_root=True) assert os.path.exists(os.path.join(tmp_folder, "subfolder", "foo.txt")) assert os.path.exists( os.path.join(tmp_folder, "subfolder", "bar", "foo.txt"))
def test_cxx11_abi_define(): conanfile = ConanFileMock() conanfile.settings = MockSettings({ "build_type": "Release", "arch": "x86", "compiler": "gcc", "compiler.libcxx": "libstdc++", "compiler.version": "7.1", "compiler.cppstd": "17" }) be = AutotoolsToolchain(conanfile) assert be.gcc_cxx11_abi == "_GLIBCXX_USE_CXX11_ABI=0" env = be.vars() assert "-D_GLIBCXX_USE_CXX11_ABI=0" in env["CPPFLAGS"] conanfile.settings = MockSettings({ "build_type": "Release", "arch": "x86", "compiler": "gcc", "compiler.libcxx": "libstdc++11", "compiler.version": "7.1", "compiler.cppstd": "17" }) be = AutotoolsToolchain(conanfile) env = be.vars() assert be.gcc_cxx11_abi is None assert "GLIBCXX_USE_CXX11_ABI" not in env["CPPFLAGS"] # Force the GLIBCXX_USE_CXX11_ABI=1 for old distros is direct def f ``gcc_cxx11_abi`` be.gcc_cxx11_abi = "_GLIBCXX_USE_CXX11_ABI=1" env = be.vars() assert "-D_GLIBCXX_USE_CXX11_ABI=1" in env["CPPFLAGS"] # Also conf is possible conanfile.conf["tools.gnu:define_libcxx11_abi"] = True be = AutotoolsToolchain(conanfile) env = be.vars() assert "-D_GLIBCXX_USE_CXX11_ABI=1" in env["CPPFLAGS"]
def test_partial_build(self): conan_file = ConanFileMock() conan_file.deps_cpp_info = self._creat_deps_cpp_info() conan_file.settings = Settings() be = AutoToolsBuildEnvironment(conan_file) conan_file.should_configure = False conan_file.should_build = False conan_file.should_install = False be.configure() self.assertIsNone(conan_file.command) be.make() self.assertIsNone(conan_file.command)
def test_multiple_no_version(mock_patch_ng): conanfile = ConanFileMock() conanfile.display_name = 'mocked/ref' conanfile.conan_data = { 'patches': [{ 'patch_file': 'patches/0001-buildflatbuffers-cmake.patch', 'base_path': 'source_subfolder', }, { 'patch_file': 'patches/0002-implicit-copy-constructor.patch', 'base_path': 'source_subfolder', 'patch_type': 'backport', 'patch_source': 'https://github.com/google/flatbuffers/pull/5650', 'patch_description': 'Needed to build with modern clang compilers.' }] } apply_conandata_patches(conanfile) assert 'Apply patch (backport): Needed to build with modern clang compilers.\n' \ == str(conanfile.output)
def test_compose_path_combinations(op1, v1, op2, v2, result): env = Environment() if op1 != "unset": getattr(env, op1 + "_path")("MyVar", v1) else: env.unset("MyVar") env2 = Environment() if op2 != "unset": getattr(env2, op2 + "_path")("MyVar", v2) else: env2.unset("MyVar") env.compose_env(env2) env = env.vars(ConanFileMock()) assert env._values["MyVar"].get_str("{name}", None, pathsep=":") == result
def test_public_access(): env = Environment(ConanFileMock()) env.define("MyVar", "MyValue") env.append("MyVar", "MyValue2") env.define_path("MyPath", "c:/path/to/something") env.append_path("MyPath", "D:/Otherpath") for name, values in env.items(): if name == "MyVar": assert values == "MyValue MyValue2" if name == "MyPath": assert values == "c:/path/to/something{}D:/Otherpath".format( os.pathsep) env.remove("MyPath", "D:/Otherpath") assert env.get("MyPath") == "c:/path/to/something"
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")