def test_win(self): install_dir = "C:\\Intel" with mock.patch("platform.system", mock.MagicMock(return_value="Windows")),\ mock.patch("conans.client.tools.intel.intel_installation_path", mock.MagicMock(return_value=install_dir)): settings = Settings.loads(get_default_settings_yml()) settings.os = "Windows" settings.compiler = "intel" settings.compiler.base = "Visual Studio" settings.arch = "ppc32" with self.assertRaises(ConanException): intel_compilervars_command(MockConanfile(settings)) path = os.path.join(install_dir, "bin", "compilervars.bat") settings.arch = "x86" cvars = intel_compilervars_command(MockConanfile(settings)) expected = '"%s" -arch ia32' % path self.assertEqual(expected, cvars) settings.compiler.base.version = "16" cvars = intel_compilervars_command(MockConanfile(settings)) expected = '"%s" -arch ia32 vs2019' % path self.assertEqual(expected, cvars) settings.arch = "x86_64" expected = '"%s" -arch intel64 vs2019' % path cvars = intel_compilervars_command(MockConanfile(settings)) self.assertEqual(expected, cvars)
def test_mac(self): install_dir = "/opt/intel" with mock.patch("platform.system", mock.MagicMock(return_value="Darwin")),\ mock.patch("conans.client.tools.intel.intel_installation_path", mock.MagicMock(return_value="/opt/intel")): settings = Settings.loads(get_default_settings_yml()) settings.os = "Macos" settings.compiler = "intel" settings.compiler.base = "apple-clang" settings.arch = "ppc32" with self.assertRaises(ConanException): intel_compilervars_command(MockConanfile(settings)) path = os.path.join(install_dir, "bin", "compilervars.sh") settings.arch = "x86" cvars = intel_compilervars_command(MockConanfile(settings)) expected = 'COMPILERVARS_PLATFORM=mac COMPILERVARS_ARCHITECTURE=ia32 . ' \ '"%s" -arch ia32 -platform mac' % path self.assertEqual(expected, cvars) settings.arch = "x86_64" expected = 'COMPILERVARS_PLATFORM=mac COMPILERVARS_ARCHITECTURE=intel64 . ' \ '"%s" -arch intel64 -platform mac' % path cvars = intel_compilervars_command(MockConanfile(settings)) self.assertEqual(expected, cvars)
def test_autotools_configure_vars(self): from mock import patch runner = RunnerMock() settings = MockSettings({"build_type": "Debug", "arch": "x86_64", "compiler": "gcc", "compiler.libcxx": "libstdc++"}) conanfile = MockConanfile(settings, None, runner) conanfile.settings = settings self._set_deps_info(conanfile) def custom_configure(obj, configure_dir=None, args=None, build=None, host=None, target=None, pkg_config_paths=None, vars=None): # @UnusedVariable self.assertNotEqual(obj.vars, vars) return vars or obj.vars with patch.object(AutoToolsBuildEnvironment, 'configure', new=custom_configure): be = AutoToolsBuildEnvironment(conanfile) # Get vars and modify them my_vars = be.vars my_vars["fake_var"] = "fake" my_vars["super_fake_var"] = "fakefake" # TEST with default vars mocked_result = be.configure() self.assertEqual(mocked_result, be.vars) # TEST with custom vars mocked_result = be.configure(vars=my_vars) self.assertEqual(mocked_result, my_vars)
def test_environment_append(self): settings = MockSettings({"build_type": "Debug", "arch": "x86_64", "compiler": "gcc", "compiler.libcxx": "libstdc++"}) conanfile = MockConanfile(settings) conanfile.settings = settings self._set_deps_info(conanfile) env_vars = {"CFLAGS": "-additionalcflag", "CXXFLAGS": "-additionalcxxflag", "LDFLAGS": "-additionalldflag", "LIBS": "-additionallibs", "CPPFLAGS": "-additionalcppflag"} with(tools.environment_append(env_vars)): be = AutoToolsBuildEnvironment(conanfile) expected = {'CPPFLAGS': '-Ipath/includes -Iother/include/path -Donedefinition -' 'Dtwodefinition -D_GLIBCXX_USE_CXX11_ABI=0 -additionalcppflag', 'CXXFLAGS': 'a_c_flag -m64 -g --sysroot=/path/to/folder a_cxx_flag -additionalcxxflag', 'LIBS': '-lonelib -ltwolib -lonesystemlib -ltwosystemlib -additionallibs', 'LDFLAGS': 'shared_link_flag exe_link_flag -framework oneframework ' '-framework twoframework -F one/framework/path -m64 ' '--sysroot=/path/to/folder -Lone/lib/path -additionalldflag', 'CFLAGS': 'a_c_flag -m64 -g --sysroot=/path/to/folder -additionalcflag'} self.assertEqual(be.vars, expected)
def test_build_with_custom_configuration(self): conanfile = MockConanfile( MockSettings({'os': 'Linux', 'compiler': 'gcc'}), runner=RunnerMock()) conanfile.source_folder = '.' conanfile.build_folder = '.' build_helper = qbs.Qbs(conanfile) config_name = 'debug' config_values = { 'product.App.boolProperty': True, 'product.App.intProperty': 1337, 'product.App.stringProperty': 'Hello World', 'product.App.stringListProperty': ['Hello', 'World'] } build_helper.add_configuration(config_name, config_values) build_helper.build() self.assertEqual( conanfile.runner.command_called, ('qbs build --no-install --build-directory %s ' '--file %s --jobs %s profile:%s ' 'config:%s %s:%s %s:%s %s:%s %s:%s') % ( conanfile.build_folder, build_helper._project_file, build_helper.jobs, build_helper.use_toolchain_profile, config_name, 'product.App.boolProperty', 'true', 'product.App.intProperty', config_values['product.App.intProperty'], 'product.App.stringProperty', config_values['product.App.stringProperty'], 'product.App.stringListProperty', config_values['product.App.stringListProperty']))
def test_construct_build_helper_without_project_file(self): conanfile = MockConanfile( MockSettings({'os': 'Linux', 'compiler': 'gcc'})) conanfile.source_folder = '.' build_helper = qbs.Qbs(conanfile) self.assertEqual(build_helper.jobs, tools.cpu_count()) self.assertEqual(build_helper._project_file, conanfile.source_folder)
def test_mac_version_min(self): options = MockOptions({}) settings = MockSettings({"os": "Macos"}) conanfile = MockConanfile(settings, options) be = AutoToolsBuildEnvironment(conanfile) expected = be.vars["CXXFLAGS"] self.assertEqual("", expected) settings = MockSettings({"os": "Macos", "os.version": "10.13", "compiler.version": "12.0"}) conanfile = MockConanfile(settings, options) be = AutoToolsBuildEnvironment(conanfile) expected = be.vars["CXXFLAGS"] self.assertIn("10.13", expected) with tools.environment_append({"CFLAGS": "-mmacosx-version-min=10.9"}): be = AutoToolsBuildEnvironment(conanfile) expected = be.vars["CFLAGS"] self.assertIn("10.9", expected) self.assertNotIn("10.13", expected) with tools.environment_append({"CXXFLAGS": "-mmacosx-version-min=10.9"}): be = AutoToolsBuildEnvironment(conanfile) expected = be.vars["CFLAGS"] self.assertNotIn("10.13", expected)
def test_skip_toolset(self): settings = MockSettings({"build_type": "Debug", "compiler": "Visual Studio", "compiler.version": "15", "arch": "x86_64"}) class Runner(object): def __init__(self): self.commands = [] def __call__(self, *args, **kwargs): self.commands.append(args[0]) with chdir(tools.mkdir_tmp()): runner = Runner() conanfile = MockConanfile(settings, runner=runner) msbuild = MSBuild(conanfile) msbuild.build("myproject", toolset=False) self.assertEqual(len(runner.commands), 1) self.assertNotIn("PlatformToolset", runner.commands[0]) runner = Runner() conanfile = MockConanfile(settings, runner=runner) msbuild = MSBuild(conanfile) msbuild.build("myproject", toolset="mytoolset") self.assertEqual(len(runner.commands), 1) self.assertIn('/p:PlatformToolset="mytoolset"', runner.commands[0])
def test_modify_values(self): settings = MockSettings({ "build_type": "Debug", "arch": "x86_64", "compiler": "gcc", "compiler.libcxx": "libstdc++" }) conanfile = MockConanfile(settings) conanfile.settings = settings self._set_deps_info(conanfile) be = AutoToolsBuildEnvironment(conanfile) # Alter some things be.defines.append("OtherDefinition=23") be.link_flags = ["-inventedflag"] be.cxx_flags.append("-onlycxx") be.fpic = True be.flags.append("cucucu") expected = { 'CFLAGS': 'a_c_flag -m64 -g --sysroot=/path/to/folder cucucu -fPIC', 'CPPFLAGS': '-Ipath/includes -Iother/include/path -Donedefinition -Dtwodefinition' ' -D_GLIBCXX_USE_CXX11_ABI=0 -DOtherDefinition=23', 'CXXFLAGS': 'a_c_flag -m64 -g --sysroot=/path/to/folder cucucu -fPIC a_cxx_flag -onlycxx', 'LDFLAGS': '-inventedflag -Lone/lib/path', 'LIBS': '-lonelib -ltwolib -lonesystemlib -ltwosystemlib' } self.assertEqual(be.vars, expected)
def test_rpath_optin(self): settings = MockSettings({ "os_build": "Linux", "build_type": "Release", "arch": "x86_64", "compiler": "gcc", "compiler.libcxx": "libstdc++11" }) conanfile = MockConanfile(settings) conanfile.settings = settings self._set_deps_info(conanfile) expected = { 'CFLAGS': 'a_c_flag -m64 -O3 -s --sysroot=/path/to/folder', 'CPPFLAGS': '-Ipath/includes -Iother/include/path -Donedefinition -Dtwodefinition -DNDEBUG ' '-D_GLIBCXX_USE_CXX11_ABI=1', 'CXXFLAGS': 'a_c_flag -m64 -O3 -s --sysroot=/path/to/folder a_cxx_flag', 'LDFLAGS': 'shared_link_flag exe_link_flag -framework oneframework -framework twoframework ' '-F one/framework/path -m64 --sysroot=/path/to/folder ' '-Wl,-rpath,"one/lib/path" -Lone/lib/path', 'LIBS': '-lonelib -ltwolib -lonesystemlib -ltwosystemlib' } be = AutoToolsBuildEnvironment(conanfile, include_rpath_flags=True) self.assertEqual(be.vars, expected)
def test_cross_build_command(self): runner = RunnerMock() conanfile = MockConanfile(MockSettings({}), None, runner) ab = AutoToolsBuildEnvironment(conanfile) self.assertFalse(ab.build) self.assertFalse(ab.host) self.assertFalse(ab.target) ab.configure() self.assertEqual(runner.command_called, "./configure ") ab.configure(host="x86_64-apple-darwin") self.assertEqual(runner.command_called, "./configure --host=x86_64-apple-darwin") ab.configure(build="arm-apple-darwin") self.assertEqual(runner.command_called, "./configure --build=arm-apple-darwin") ab.configure(target="i686-apple-darwin") self.assertEqual(runner.command_called, "./configure --target=i686-apple-darwin") conanfile = MockConanfile(MockSettings({"build_type": "Debug", "arch": "x86_64", "os": "Windows", "compiler": "gcc", "compiler.libcxx": "libstdc++"}), None, runner) ab = AutoToolsBuildEnvironment(conanfile) ab.configure() if platform.system() == "Windows": # Not crossbuilding self.assertFalse(ab.host) self.assertFalse(ab.build) self.assertIn("./configure", runner.command_called) self.assertNotIn("--build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32", runner.command_called) elif platform.system() == "Linux": self.assertIn("x86_64-w64-mingw32", ab.host) self.assertIn("x86_64-linux-gnu", ab.build) self.assertIn("./configure --build=x86_64-linux-gnu --host=x86_64-w64-mingw32", runner.command_called) else: self.assertIn("x86_64-w64-mingw32", ab.host) self.assertIn("x86_64-apple-darwin", ab.build) self.assertIn("./configure --build=x86_64-apple-darwin --host=x86_64-w64-mingw32", runner.command_called) ab.configure(build="fake_build_triplet", host="fake_host_triplet") self.assertIn("./configure --build=fake_build_triplet --host=fake_host_triplet", runner.command_called) ab.build = "superfake_build_triplet" ab.host = "superfake_host_triplet" ab.configure() self.assertIn("./configure --build=superfake_build_triplet --host=superfake_host_triplet", runner.command_called)
def test_add_configuration(self): conanfile = MockConanfile( MockSettings({'os': 'Linux', 'compiler': 'gcc'})) conanfile.source_folder = '.' build_helper = qbs.Qbs(conanfile) configurations = { 'debug': {'products.MyLib.specialFlags': ['-frtti', '-fexceptions']}, 'release': {'products.MyLib.specialFlags': ['-fno-exceptions', '-fno-rtti']} } for name, config in configurations.items(): build_helper.add_configuration(name, config) self.assertEqual(build_helper._configuration, configurations)
def test_build_all(self): conanfile = MockConanfile( MockSettings({'os': 'Linux', 'compiler': 'gcc'}), runner=RunnerMock()) conanfile.source_folder = '.' conanfile.build_folder = '.' build_helper = qbs.Qbs(conanfile) build_helper.build_all() self.assertEqual( conanfile.runner.command_called, ('qbs build --no-install --build-directory %s ' '--file %s --all-products --jobs %s profile:%s') % ( conanfile.build_folder, build_helper._project_file, build_helper.jobs, build_helper.use_toolchain_profile))
def test_change_branch_in_root_commit(): """ https://github.com/conan-io/conan/issues/10971#issuecomment-1089316912 """ c = TestClient() conanfile = MockConanfile({}) c.save({"root.txt": "", "subfolder/subfolder.txt": ""}) c.run_command("git init .") c.run_command('git config user.name myname') c.run_command('git config user.email [email protected]') c.run_command("git add .") c.run_command('git commit -m "initial commit"') c.run_command("git checkout -b change_branch") c.save({"subfolder/subfolder.txt": "CHANGED"}) c.run_command("git add .") c.run_command('git commit -m "second commit"') c.run_command("git checkout master") c.run_command('git merge --no-ff change_branch -m "Merge branch"') git = Git(conanfile, folder=c.current_folder) commit_conan = git.get_commit() c.run_command("git rev-parse HEAD") commit_real = str(c.out).splitlines()[0] assert commit_conan == commit_real
def test_construct_build_helper_with_project_file(self): conanfile = MockConanfile( MockSettings({'os': 'Linux', 'compiler': 'gcc'})) # just asume that the test is called from repo root profile_file_path = temp_folder() build_helper = qbs.Qbs(conanfile, project_file=profile_file_path) self.assertEqual(build_helper._project_file, profile_file_path)
def test_replace_in_file(): conanfile = MockConanfile({}) tmp = temp_folder() file_path = os.path.join(tmp, "file.txt") # By default utf-8 is used save(conanfile, file_path, "你很重,伙計") replace_in_file(conanfile, file_path, "重", "0") contents = load(conanfile, file_path) assert contents == "你很0,伙計" # Replacing with other encodings is also possible save(conanfile, file_path, "Ö¼", encoding="cp1252") replace_in_file(conanfile, file_path, "¼", "0", encoding="cp1252") contents = load(conanfile, file_path, encoding="cp1252") assert contents == "Ö0" save(conanfile, file_path, bytes("Ö¼", "ISO-8859-1")) replace_in_file(conanfile, file_path, "¼", "0", encoding="ISO-8859-1") contents = load(conanfile, file_path, encoding="ISO-8859-1") assert contents == "Ö0" # Replacing utf-16 is also possible but using "utf-16LE" (without BOM) to search and replace # otherwise the "search" string is not found because it contains also a BOM (header) save(conanfile, file_path, bytes("你很重,伙計", "utf-16")) replace_in_file(conanfile, file_path, "重", "0", encoding="utf-16") contents = load(conanfile, file_path, encoding="utf-16") assert contents == "你很0,伙計"
def test_install(self): conanfile = MockConanfile(MockSettings({ 'os': 'Linux', 'compiler': 'gcc' }), runner=RunnerMock()) conanfile.source_folder = '.' conanfile.package_folder = 'pkg' build_helper = qbs.Qbs(conanfile) build_helper.install() self.assertEqual( conanfile.runner.command_called, ('qbs install --no-build --install-root %s ' '--file %s') % (conanfile.package_folder, build_helper._project_file))
def test_verbosity_explicit(self): settings = MockSettings({"build_type": "Debug", "compiler": "Visual Studio", "arch": "x86_64"}) conanfile = MockConanfile(settings) msbuild = MSBuild(conanfile) command = msbuild.get_command("projecshould_flags_testt_file.sln", verbosity="quiet") self.assertIn('/verbosity:quiet', command)
def test_construct_build_helper_with_wrong_project_file_path(self): conanfile = MockConanfile( MockSettings({ 'os': 'Linux', 'compiler': 'gcc' })) with self.assertRaises(ConanException): qbs.Qbs(conanfile, project_file='random/file/path')
def test_convert_compiler_name_to_qbs_compiler_name(self): for settings in self._settings_to_test_against(): def expected(): return settings['qbs_compiler'] conanfile = MockConanfile(MockSettings(settings)) self.assertEqual(qbs._default_compiler_name(conanfile), expected())
def test_without_runtime(self): settings = MockSettings({"build_type": "Debug", "compiler": "Visual Studio", "arch": "x86_64"}) conanfile = MockConanfile(settings) msbuild = MSBuild(conanfile) template = msbuild._get_props_file_contents() self.assertNotIn("<RuntimeLibrary>", template)
def test_check_cppstd_type(self): """ cppstd must be a number """ conanfile = MockConanfile(MockSettings({})) with self.assertRaises(ConanException) as raises: check_min_cppstd(conanfile, "gnu17", False) self.assertEqual("cppstd parameter must be a number", str(raises.exception))
def test_verbosity_env(self): settings = MockSettings({"build_type": "Debug", "compiler": "Visual Studio", "arch": "x86_64"}) with tools.environment_append({"CONAN_MSBUILD_VERBOSITY": "detailed"}): conanfile = MockConanfile(settings) msbuild = MSBuild(conanfile) command = msbuild.get_command("projecshould_flags_testt_file.sln") self.assertIn('/verbosity:detailed', command)
def test_previous_env(self): settings = MockSettings({"arch": "x86", "os": "Linux", "compiler": "gcc"}) conanfile = MockConanfile(settings) with tools.environment_append({"CPPFLAGS": "MyCppFlag"}): be = AutoToolsBuildEnvironment(conanfile) self.assertEqual(be.vars["CPPFLAGS"], "MyCppFlag")
def test_explicit_toolset(self, expected_toolset): settings = MockSettings({"build_type": "Debug", "compiler": "Visual Studio", "compiler.version": "15", "arch": "x86_64"}) conanfile = MockConanfile(settings) msbuild = MSBuild(conanfile) command = msbuild.get_command("project_should_flags_test_file.sln", toolset=expected_toolset) self.assertIn('/p:PlatformToolset="%s"' % expected_toolset, command)
def test_custom_properties(self): settings = MockSettings({"build_type": "Debug", "compiler": "Visual Studio", "arch": "x86_64"}) conanfile = MockConanfile(settings) msbuild = MSBuild(conanfile) command = msbuild.get_command("project_file.sln", properties={"MyProp1": "MyValue1", "MyProp2": "MyValue2"}) self.assertIn('/p:MyProp1="MyValue1"', command) self.assertIn('/p:MyProp2="MyValue2"', command)
def verbosity_default_test(self): settings = MockSettings({ "build_type": "Debug", "compiler": "Visual Studio", "arch": "x86_64" }) conanfile = MockConanfile(settings) msbuild = MSBuild(conanfile) command = msbuild.get_command("projecshould_flags_testt_file.sln") self.assertIn('/verbosity:minimal', command)
def test_binary_logging_on(self): settings = MockSettings({"build_type": "Debug", "compiler": "Visual Studio", "compiler.version": "15", "arch": "x86_64", "compiler.runtime": "MDd"}) conanfile = MockConanfile(settings) msbuild = MSBuild(conanfile) command = msbuild.get_command("dummy.sln", output_binary_log=True) self.assertIn("/bl", command)
def test_windows_ce(self): settings = MockSettings({"build_type": "Debug", "compiler": "Visual Studio", "compiler.version": "9", "os": "WindowsCE", "os.platform": "YOUR PLATFORM SDK (ARMV4)", "arch": "armv4"}) conanfile = MockConanfile(settings) msbuild = MSBuild(conanfile) command = msbuild.get_command("test.sln") self.assertIn('/p:Platform="YOUR PLATFORM SDK (ARMV4)"', command)
def test_skip_only_none_definitions(self): # https://github.com/conan-io/conan/issues/6728 settings = MockSettings({"build_type": "Debug", "compiler": "Visual Studio", "arch": "x86_64", "compiler.runtime": "MDd"}) conanfile = MockConanfile(settings) msbuild = MSBuild(conanfile) template = msbuild._get_props_file_contents(definitions={"foo": 0, "bar": False}) self.assertIn("<PreprocessorDefinitions>foo=0;bar=False;%(PreprocessorDefinitions)", template)