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 setUpClass(cls): conanfile = ConanFileMock() conanfile.settings = MockSettings({"compiler": "gcc", "build_type": "Release"}) cls.generator = VirtualBuildEnvGenerator(conanfile) cls.result = cls.generator.content
def target_triple_test(self): conan_file = ConanFileMock() deps_cpp_info = namedtuple("Deps", "libs, include_paths, lib_paths, defines, cflags, " "cxxflags, sharedlinkflags, exelinkflags, sysroot") conan_file.deps_cpp_info = deps_cpp_info([], [], [], [], [], [], [], [], "") conan_file.settings = MockSettings({"os_target":"Linux", "arch_target":"x86_64"}) be = AutoToolsBuildEnvironment(conan_file) expected = "x86_64-linux-gnu" self.assertEqual(be.target, expected)
def warn_when_no_triplet_test(self): conan_file = ConanFileMock() deps_cpp_info = namedtuple("Deps", "libs, include_paths, lib_paths, defines, cflags, " "cxxflags, sharedlinkflags, exelinkflags, sysroot") conan_file.deps_cpp_info = deps_cpp_info([], [], [], [], [], [], [], [], "") conan_file.settings = MockSettings({"arch": "UNKNOWN_ARCH", "os": "Linux"}) AutoToolsBuildEnvironment(conan_file) self.assertIn("Unknown 'UNKNOWN_ARCH' machine, Conan doesn't know " "how to translate it to the GNU triplet", conan_file.output)
def target_triple_test(self): conan_file = ConanFileMock() conan_file.deps_cpp_info = self._creat_deps_cpp_info() conan_file.settings = MockSettings({ "os_target": "Linux", "arch_target": "x86_64" }) be = AutoToolsBuildEnvironment(conan_file) expected = "x86_64-linux-gnu" self.assertEqual(be.target, expected)
def _get_conanfile(settings, frameworks=False, system_libs=False): conan_file = ConanFileMock() conan_file.settings = settings conan_file.source_folder = "my_cache_source_folder" conan_file.build_folder = "my_cache_build_folder" conan_file.deps_env_info = DepsEnvInfo() conan_file.deps_user_info = DepsUserInfo() conan_file.deps_cpp_info = DepsCppInfo() cpp_info = CppInfo("/root") cpp_info.include_paths.append("path/to/include1") cpp_info.lib_paths.append("path/to/lib1") cpp_info.libs.append("mylib") cpp_info.bindirs = "path/to/bin1" cpp_info.cflags.append("c_flag1") cpp_info.cxxflags.append("cxx_flag1") cpp_info.defines.append("mydefine1") if system_libs: cpp_info.system_libs.append("system_lib1") if frameworks: cpp_info.frameworks = ["AVFoundation", "VideoToolbox"] cpp_info.framework_paths.extend(['path/to/Frameworks1', 'path/to/Frameworks2']) conan_file.deps_cpp_info.update(cpp_info, "zlib") conan_file.env_info = EnvInfo() return conan_file
def warn_when_no_triplet_test(self): conan_file = ConanFileMock() conan_file.deps_cpp_info = self._creat_deps_cpp_info() conan_file.settings = MockSettings({ "arch": "UNKNOWN_ARCH", "os": "Linux" }) AutoToolsBuildEnvironment(conan_file) self.assertIn( "Unknown 'UNKNOWN_ARCH' machine, Conan doesn't know " "how to translate it to the GNU triplet", conan_file.output)
def no_prefix_test(self): conan_file = ConanFileMock() conan_file.settings = Settings() conan_file.package_folder = None meson = Meson(conan_file) meson.configure() self.assertNotIn('-Dprefix', conan_file.command) meson.build() self.assertIn("ninja -C", conan_file.command) with self.assertRaises(TypeError): meson.install()
def prefix_test(self): conan_file = ConanFileMock() conan_file.settings = Settings() conan_file.package_folder = os.getcwd() expected_prefix = '-Dprefix="%s"' % os.getcwd() meson = Meson(conan_file) meson.configure() self.assertIn(expected_prefix, conan_file.command) meson.build() self.assertIn("ninja -C", conan_file.command) meson.install() self.assertIn("ninja -C", conan_file.command)
def setUpClass(cls): conanfile = ConanFileMock() conanfile.deps_cpp_info["hello"].bin_paths = ["bin1", "bin2"] conanfile.deps_cpp_info["hello"].lib_paths = ["lib1", "lib2"] cls.generator = VirtualRunEnvGenerator(conanfile) cls.result = cls.generator.content
def flags_applied_test(self, the_os, compiler, version, arch, runtime, flag): settings = Settings.loads(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_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 run_vars_test(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_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 partial_build_test(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 _get_conanfile(self, settings): conan_file = ConanFileMock() conan_file.settings = settings conan_file.source_folder = "my_cache_source_folder" conan_file.build_folder = "my_cache_build_folder" conan_file.deps_env_info = DepsEnvInfo() conan_file.deps_user_info = DepsUserInfo() conan_file.deps_cpp_info = DepsCppInfo() cpp_info = CppInfo("/root") cpp_info.include_paths.append("path/to/include1") cpp_info.lib_paths.append("path/to/lib1") cpp_info.libs.append("mylib") cpp_info.bindirs = "path/to/bin1" cpp_info.cflags.append("c_flag1") cpp_info.cxxflags.append("cxx_flag1") cpp_info.defines.append("mydefine1") conan_file.deps_cpp_info.update(cpp_info, "zlib") conan_file.env_info = EnvInfo() return conan_file
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 partial_build_test(self): conan_file = ConanFileMock() deps_cpp_info = namedtuple("Deps", "libs, include_paths, lib_paths, defines, cflags, " "cxxflags, sharedlinkflags, exelinkflags, sysroot") conan_file.deps_cpp_info = 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_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 partial_build_test(self): conan_file = ConanFileMock() conan_file.settings = Settings() conan_file.should_configure = False conan_file.should_build = False conan_file.should_test = False conan_file.should_install = False conan_file.package_folder = os.path.join(self.tempdir, "my_cache_package_folder") meson = Meson(conan_file) meson.configure() self.assertIsNone(conan_file.command) meson.build() self.assertIsNone(conan_file.command) meson.test() self.assertIsNone(conan_file.command) meson.install() self.assertIsNone(conan_file.command) meson.meson_test() self.assertIsNone(conan_file.command) meson.meson_install() self.assertIsNone(conan_file.command)
def test_list_variable(self): self.assertNotIn("WHATEVER", os.environ) self.assertIn("PATH", os.environ) existing_path = os.environ.get("PATH") 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 visual_studio_extensions_test(self): settings = Settings.loads(get_default_settings_yml()) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "15" settings.arch = "x86" settings.build_type = "Release" conan_file = ConanFileMock() conan_file.settings = settings conan_file.deps_env_info = DepsEnvInfo() conan_file.deps_user_info = DepsUserInfo() conan_file.deps_cpp_info = DepsCppInfo() cpp_info = CppInfo("/root") cpp_info.libs.append("mylib") cpp_info.libs.append("other.lib") conan_file.deps_cpp_info.update(cpp_info, "zlib") conan_file.env_info = EnvInfo() gen = CompilerArgsGenerator(conan_file) self.assertEqual('-O2 -Ob2 -DNDEBUG -link mylib.lib other.lib', gen.content)
def folders_test(self): settings = Settings.loads(default_settings_yml) settings.os = "Linux" settings.compiler = "gcc" settings.compiler.version = "6.3" settings.arch = "x86" settings.build_type = "Release" package_folder = os.path.join(self.tempdir, "my_cache_package_folder") conan_file = ConanFileMock() conan_file.settings = settings conan_file.source_folder = os.path.join(self.tempdir, "my_cache_source_folder") conan_file.build_folder = os.path.join(self.tempdir, "my_cache_build_folder") conan_file.package_folder = package_folder meson = Meson(conan_file) defs = { 'default_library': 'shared', 'prefix': package_folder, 'libdir': 'lib', 'bindir': 'bin', 'sbindir': 'bin', 'libexecdir': 'bin', 'includedir': 'include', 'cpp_std': 'none' } meson.configure(source_dir=os.path.join(self.tempdir, "../subdir"), build_dir=os.path.join(self.tempdir, "build")) source_expected = os.path.join(self.tempdir, "../subdir") build_expected = os.path.join(self.tempdir, "build") cmd_expected = 'meson "%s" "%s" --backend=ninja %s --buildtype=release' \ % (source_expected, build_expected, defs_to_string(defs)) self._check_commands(cmd_expected, conan_file.command) meson.configure(build_dir=os.path.join(self.tempdir, "build")) source_expected = os.path.join(self.tempdir, "my_cache_source_folder") build_expected = os.path.join(self.tempdir, "build") cmd_expected = 'meson "%s" "%s" --backend=ninja %s --buildtype=release' \ % (source_expected, build_expected, defs_to_string(defs)) self._check_commands(cmd_expected, conan_file.command) meson.configure() source_expected = os.path.join(self.tempdir, "my_cache_source_folder") build_expected = os.path.join(self.tempdir, "my_cache_build_folder") cmd_expected = 'meson "%s" "%s" --backend=ninja %s --buildtype=release' \ % (source_expected, build_expected, defs_to_string(defs)) self._check_commands(cmd_expected, conan_file.command) meson.configure(source_folder="source", build_folder="build") build_expected = os.path.join(self.tempdir, "my_cache_build_folder", "build") source_expected = os.path.join(self.tempdir, "my_cache_source_folder", "source") cmd_expected = 'meson "%s" "%s" --backend=ninja %s --buildtype=release' \ % (source_expected, build_expected, defs_to_string(defs)) self._check_commands(cmd_expected, conan_file.command) conan_file.in_local_cache = True meson.configure(source_folder="source", build_folder="build", cache_build_folder="rel_only_cache") build_expected = os.path.join(self.tempdir, "my_cache_build_folder", "rel_only_cache") source_expected = os.path.join(self.tempdir, "my_cache_source_folder", "source") cmd_expected = 'meson "%s" "%s" --backend=ninja %s --buildtype=release' \ % (source_expected, build_expected, defs_to_string(defs)) self._check_commands(cmd_expected, conan_file.command) conan_file.in_local_cache = False meson.configure(source_folder="source", build_folder="build", cache_build_folder="rel_only_cache") build_expected = os.path.join(self.tempdir, "my_cache_build_folder", "build") source_expected = os.path.join(self.tempdir, "my_cache_source_folder", "source") cmd_expected = 'meson "%s" "%s" --backend=ninja %s --buildtype=release' \ % (source_expected, build_expected, defs_to_string(defs)) self._check_commands(cmd_expected, conan_file.command) conan_file.in_local_cache = True meson.configure(build_dir="build", cache_build_folder="rel_only_cache") build_expected = os.path.join(self.tempdir, "my_cache_build_folder", "rel_only_cache") source_expected = os.path.join(self.tempdir, "my_cache_source_folder") cmd_expected = 'meson "%s" "%s" --backend=ninja %s --buildtype=release' \ % (source_expected, build_expected, defs_to_string(defs)) self._check_commands(cmd_expected, conan_file.command) args = ['--werror', '--warnlevel 3'] defs['default_library'] = 'static' meson.configure(source_folder="source", build_folder="build", args=args, defs={'default_library': 'static'}) build_expected = os.path.join(self.tempdir, "my_cache_build_folder", "build") source_expected = os.path.join(self.tempdir, "my_cache_source_folder", "source") cmd_expected = 'meson "%s" "%s" --backend=ninja %s %s --buildtype=release' \ % (source_expected, build_expected, args_to_string(args), defs_to_string(defs)) self._check_commands(cmd_expected, conan_file.command) # Raise mixing with self.assertRaisesRegexp(ConanException, "Use 'build_folder'/'source_folder'"): meson.configure(source_folder="source", build_dir="build") meson.test() self.assertEquals( "ninja -C \"%s\" %s" % (build_expected, args_to_string(["test"])), conan_file.command) meson.install() self.assertEquals( "ninja -C \"%s\" %s" % (build_expected, args_to_string(["install"])), conan_file.command)
def self_collect_libs_test(self): conanfile = ConanFileMock() # Without package_folder conanfile.package_folder = None result = conanfile.collect_libs() self.assertEqual([], result) self.assertIn("'self.collect_libs' is deprecated, use 'tools.collect_libs(self)' instead", conanfile.output) # Default behavior conanfile.package_folder = temp_folder() mylib_path = os.path.join(conanfile.package_folder, "lib", "mylib.lib") save(mylib_path, "") conanfile.cpp_info = CppInfo("") result = conanfile.collect_libs() self.assertEqual(["mylib"], result) # Custom folder customlib_path = os.path.join(conanfile.package_folder, "custom_folder", "customlib.lib") save(customlib_path, "") result = conanfile.collect_libs(folder="custom_folder") self.assertEqual(["customlib"], result) # Custom folder doesn't exist result = conanfile.collect_libs(folder="fake_folder") self.assertEqual([], result) self.assertIn("Lib folder doesn't exist, can't collect libraries:", conanfile.output) # Use cpp_info.libdirs conanfile.cpp_info.libdirs = ["lib", "custom_folder"] result = conanfile.collect_libs() self.assertEqual(["customlib", "mylib"], result) # Custom folder with multiple libdirs should only collect from custom folder self.assertEqual(["lib", "custom_folder"], conanfile.cpp_info.libdirs) result = conanfile.collect_libs(folder="custom_folder") self.assertEqual(["customlib"], result) # Warn same lib different folders conanfile = ConanFileMock() conanfile.package_folder = temp_folder() conanfile.cpp_info = CppInfo("") custom_mylib_path = os.path.join(conanfile.package_folder, "custom_folder", "mylib.lib") lib_mylib_path = os.path.join(conanfile.package_folder, "lib", "mylib.lib") save(custom_mylib_path, "") save(lib_mylib_path, "") conanfile.cpp_info.libdirs = ["lib", "custom_folder"] result = conanfile.collect_libs() self.assertEqual(["mylib"], result) self.assertIn("Library 'mylib' was either already found in a previous " "'conanfile.cpp_info.libdirs' folder or appears several times with a " "different file extension", conanfile.output) # Warn lib folder does not exist with correct result conanfile = ConanFileMock() conanfile.package_folder = temp_folder() conanfile.cpp_info = CppInfo("") lib_mylib_path = os.path.join(conanfile.package_folder, "lib", "mylib.lib") save(lib_mylib_path, "") no_folder_path = os.path.join(conanfile.package_folder, "no_folder") conanfile.cpp_info.libdirs = ["no_folder", "lib"] # 'no_folder' does NOT exist result = conanfile.collect_libs() self.assertEqual(["mylib"], result) self.assertIn("WARN: Lib folder doesn't exist, can't collect libraries: %s" % no_folder_path, conanfile.output)
def _test_default_generator_cmake(self, settings, generator): conanfile = ConanFileMock() conanfile.settings = settings cmake = CMake(conanfile) self.assertEquals(generator, cmake.generator) self.assertIn('-G "{}"'.format(generator), cmake.command_line)