Best Python code snippet using lisa_python
test_cmake.py
Source:test_cmake.py  
...110            self.client.run("install .. %s %s" % (settings, options))111            install_out = self.client.out112            self.client.run("build ..")113        return install_out114    def _modify_code(self):115        lib_cpp = gen_function_cpp(name="app", msg="AppImproved", includes=["hello"],116                                   calls=["hello"], preprocessor=["MYVAR", "MYVAR_CONFIG",117                                                                  "MYDEFINE", "MYDEFINE_CONFIG"])118        self.client.save({"app_lib.cpp": lib_cpp})119        content = self.client.load("CMakeLists.txt")120        content = content.replace(">>", "++>>")121        self.client.save({"CMakeLists.txt": content})122    def _incremental_build(self, build_type=None):123        build_directory = os.path.join(self.client.current_folder, "build").replace("\\", "/")124        with self.client.chdir(build_directory):125            config = "--config %s" % build_type if build_type else ""126            self.client.run_command("cmake --build . %s" % config)127    def _run_app(self, build_type, bin_folder=False, msg="App", dyld_path=None):128        if dyld_path:129            build_directory = os.path.join(self.client.current_folder, "build").replace("\\", "/")130            command_str = 'DYLD_LIBRARY_PATH="%s" build/app' % build_directory131        else:132            command_str = "build/%s/app.exe" % build_type if bin_folder else "build/app"133            if platform.system() == "Windows":134                command_str = command_str.replace("/", "\\")135        self.client.run_command(command_str)136        self.assertIn("Hello: %s" % build_type, self.client.out)137        self.assertIn("%s: %s!" % (msg, build_type), self.client.out)138        self.assertIn("MYVAR: MYVAR_VALUE", self.client.out)139        self.assertIn("MYVAR_CONFIG: MYVAR_%s" % build_type.upper(), self.client.out)140        self.assertIn("MYDEFINE: MYDEF_VALUE", self.client.out)141        self.assertIn("MYDEFINE_CONFIG: MYDEF_%s" % build_type.upper(), self.client.out)142@pytest.mark.skipif(platform.system() != "Windows", reason="Only for windows")143class WinTest(Base):144    @parameterized.expand([("Visual Studio", "Debug", "MTd", "15", "14", "x86", "v140", True),145                           ("Visual Studio", "Release", "MD", "15", "17", "x86_64", "", False),146                           ("msvc", "Debug", "static", "19.1", "14", "x86", None, True),147                           ("msvc", "Release", "dynamic", "19.1", "17", "x86_64", None, False)]148                          )149    def test_toolchain_win(self, compiler, build_type, runtime, version, cppstd, arch, toolset,150                           shared):151        settings = {"compiler": compiler,152                    "compiler.version": version,153                    "compiler.toolset": toolset,154                    "compiler.runtime": runtime,155                    "compiler.cppstd": cppstd,156                    "arch": arch,157                    "build_type": build_type,158                    }159        options = {"shared": shared}160        install_out = self._run_build(settings, options)161        self.assertIn("WARN: Toolchain: Ignoring fPIC option defined for Windows", install_out)162        # FIXME: Hardcoded VS version and partial toolset check163        self.assertIn('CMake command: cmake -G "Visual Studio 15 2017" '164                      '-DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake"', self.client.out)165        if toolset == "v140":166            self.assertIn("Microsoft Visual Studio 14.0", self.client.out)167        else:168            self.assertIn("Microsoft Visual Studio/2017", self.client.out)169        generator_platform = "x64" if arch == "x86_64" else "Win32"170        arch_flag = "x64" if arch == "x86_64" else "X86"171        shared_str = "ON" if shared else "OFF"172        vals = {"CMAKE_GENERATOR_PLATFORM": generator_platform,173                "CMAKE_BUILD_TYPE": "",174                "CMAKE_CXX_FLAGS": "/MP1 /DWIN32 /D_WINDOWS /GR /EHsc",175                "CMAKE_CXX_FLAGS_DEBUG": "/Zi /Ob0 /Od /RTC1",176                "CMAKE_CXX_FLAGS_RELEASE": "/O2 /Ob2 /DNDEBUG",177                "CMAKE_C_FLAGS": "/MP1 /DWIN32 /D_WINDOWS",178                "CMAKE_C_FLAGS_DEBUG": "/Zi /Ob0 /Od /RTC1",179                "CMAKE_C_FLAGS_RELEASE": "/O2 /Ob2 /DNDEBUG",180                "CMAKE_SHARED_LINKER_FLAGS": "/machine:%s" % arch_flag,181                "CMAKE_EXE_LINKER_FLAGS": "/machine:%s" % arch_flag,182                "CMAKE_CXX_STANDARD": cppstd,183                "CMAKE_CXX_EXTENSIONS": "OFF",184                "BUILD_SHARED_LIBS": shared_str}185        def _verify_out(marker=">>"):186            if shared:187                self.assertIn("app_lib.dll", self.client.out)188            else:189                self.assertNotIn("app_lib.dll", self.client.out)190            out = str(self.client.out).splitlines()191            for k, v in vals.items():192                self.assertIn("%s %s: %s" % (marker, k, v), out)193        _verify_out()194        opposite_build_type = "Release" if build_type == "Debug" else "Debug"195        settings["build_type"] = opposite_build_type196        if runtime == "MTd":197            settings["compiler.runtime"] = "MT"198        if runtime == "MD":199            settings["compiler.runtime"] = "MDd"200        self._run_build(settings, options)201        self._run_app("Release", bin_folder=True)202        if compiler == "msvc":203            visual_version = version204        else:205            visual_version = "19.0" if toolset == "v140" else "19.1"206        check_exe_run(self.client.out, "main", "msvc", visual_version, "Release", arch, cppstd,207                      {"MYVAR": "MYVAR_VALUE",208                       "MYVAR_CONFIG": "MYVAR_RELEASE",209                       "MYDEFINE": "MYDEF_VALUE",210                       "MYDEFINE_CONFIG": "MYDEF_RELEASE"211                       })212        self._run_app("Debug", bin_folder=True)213        check_exe_run(self.client.out, "main", "msvc", visual_version, "Debug", arch, cppstd,214                      {"MYVAR": "MYVAR_VALUE",215                       "MYVAR_CONFIG": "MYVAR_DEBUG",216                       "MYDEFINE": "MYDEF_VALUE",217                       "MYDEFINE_CONFIG": "MYDEF_DEBUG"218                       })219        static = (runtime == "static" or "MT" in runtime)220        check_vs_runtime("build/Release/app.exe", self.client, "15", build_type="Release",221                         static=static)222        check_vs_runtime("build/Debug/app.exe", self.client, "15", build_type="Debug",223                         static=static)224        self._modify_code()225        time.sleep(1)226        self._incremental_build(build_type=build_type)227        _verify_out(marker="++>>")228        self._run_app(build_type, bin_folder=True, msg="AppImproved")229        self._incremental_build(build_type=opposite_build_type)230        self._run_app(opposite_build_type, bin_folder=True, msg="AppImproved")231    @parameterized.expand([("Debug", "libstdc++", "4.9", "98", "x86_64", True),232                           ("Release", "libstdc++", "4.9", "11", "x86_64", False)])233    def test_toolchain_mingw_win(self, build_type, libcxx, version, cppstd, arch, shared):234        # FIXME: The version and cppstd are wrong, toolchain doesn't enforce it235        settings = {"compiler": "gcc",236                    "compiler.version": version,237                    "compiler.libcxx": libcxx,238                    "compiler.cppstd": cppstd,239                    "arch": arch,240                    "build_type": build_type,241                    }242        options = {"shared": shared}243        install_out = self._run_build(settings, options)244        self.assertIn("WARN: Toolchain: Ignoring fPIC option defined for Windows", install_out)245        self.assertIn("The C compiler identification is GNU", self.client.out)246        self.assertIn('CMake command: cmake -G "MinGW Makefiles" '247                      '-DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake"', self.client.out)248        def _verify_out(marker=">>"):249            cmake_vars = {"CMAKE_GENERATOR_PLATFORM": "",250                          "CMAKE_BUILD_TYPE": build_type,251                          "CMAKE_CXX_FLAGS": "-m64",252                          "CMAKE_CXX_FLAGS_DEBUG": "-g",253                          "CMAKE_CXX_FLAGS_RELEASE": "-O3 -DNDEBUG",254                          "CMAKE_C_FLAGS": "-m64",255                          "CMAKE_C_FLAGS_DEBUG": "-g",256                          "CMAKE_C_FLAGS_RELEASE": "-O3 -DNDEBUG",257                          "CMAKE_SHARED_LINKER_FLAGS": "-m64",258                          "CMAKE_EXE_LINKER_FLAGS": "-m64",259                          "CMAKE_CXX_STANDARD": cppstd,260                          "CMAKE_CXX_EXTENSIONS": "OFF",261                          "BUILD_SHARED_LIBS": "ON" if shared else "OFF"}262            if shared:263                self.assertIn("app_lib.dll", self.client.out)264            else:265                self.assertNotIn("app_lib.dll", self.client.out)266            out = str(self.client.out).splitlines()267            for k, v in cmake_vars.items():268                self.assertIn("%s %s: %s" % (marker, k, v), out)269        _verify_out()270        self._run_app(build_type)271        check_exe_run(self.client.out, "main", "gcc", None, build_type, arch, None,272                      {"MYVAR": "MYVAR_VALUE",273                       "MYVAR_CONFIG": "MYVAR_{}".format(build_type.upper()),274                       "MYDEFINE": "MYDEF_VALUE",275                       "MYDEFINE_CONFIG": "MYDEF_{}".format(build_type.upper())276                       })277        self._modify_code()278        time.sleep(2)279        self._incremental_build()280        _verify_out(marker="++>>")281        self._run_app(build_type, msg="AppImproved")282@pytest.mark.skipif(platform.system() != "Linux", reason="Only for Linux")283class LinuxTest(Base):284    @parameterized.expand([("Debug",  "14", "x86", "libstdc++", True),285                           ("Release", "gnu14", "x86_64", "libstdc++11", False)])286    def test_toolchain_linux(self, build_type, cppstd, arch, libcxx, shared):287        settings = {"compiler": "gcc",288                    "compiler.cppstd": cppstd,289                    "compiler.libcxx": libcxx,290                    "arch": arch,291                    "build_type": build_type}292        self._run_build(settings, {"shared": shared})293        self.assertIn('CMake command: cmake -G "Unix Makefiles" '294                      '-DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake"', self.client.out)295        extensions_str = "ON" if "gnu" in cppstd else "OFF"296        pic_str = "" if shared else "ON"297        arch_str = "-m32" if arch == "x86" else "-m64"298        cxx11_abi_str = "1" if libcxx == "libstdc++11" else "0"299        defines = '_GLIBCXX_USE_CXX11_ABI=%s;MYDEFINE="MYDEF_VALUE";'\300                  'MYDEFINE_CONFIG=$<IF:$<CONFIG:debug>,"MYDEF_DEBUG",'\301                  '$<IF:$<CONFIG:release>,"MYDEF_RELEASE","">>' % cxx11_abi_str302        vals = {"CMAKE_CXX_STANDARD": "14",303                "CMAKE_CXX_EXTENSIONS": extensions_str,304                "CMAKE_BUILD_TYPE": build_type,305                "CMAKE_CXX_FLAGS": arch_str,306                "CMAKE_CXX_FLAGS_DEBUG": "-g",307                "CMAKE_CXX_FLAGS_RELEASE": "-O3 -DNDEBUG",308                "CMAKE_C_FLAGS": arch_str,309                "CMAKE_C_FLAGS_DEBUG": "-g",310                "CMAKE_C_FLAGS_RELEASE": "-O3 -DNDEBUG",311                "CMAKE_SHARED_LINKER_FLAGS": arch_str,312                "CMAKE_EXE_LINKER_FLAGS": arch_str,313                "COMPILE_DEFINITIONS": defines,314                "CMAKE_POSITION_INDEPENDENT_CODE": pic_str315                }316        def _verify_out(marker=">>"):317            if shared:318                self.assertIn("libapp_lib.so", self.client.out)319            else:320                self.assertIn("libapp_lib.a", self.client.out)321            out = str(self.client.out).splitlines()322            for k, v in vals.items():323                self.assertIn("%s %s: %s" % (marker, k, v), out)324        _verify_out()325        self._run_app(build_type)326        self._modify_code()327        self._incremental_build()328        _verify_out(marker="++>>")329        self._run_app(build_type, msg="AppImproved")330@pytest.mark.skipif(platform.system() != "Darwin", reason="Only for Apple")331class AppleTest(Base):332    @parameterized.expand([("Debug",  "14",  True),333                           ("Release", "", False)])334    def test_toolchain_apple(self, build_type, cppstd, shared):335        settings = {"compiler": "apple-clang",336                    "compiler.cppstd": cppstd,337                    "build_type": build_type}338        self._run_build(settings, {"shared": shared})339        self.assertIn('CMake command: cmake -G "Unix Makefiles" '340                      '-DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake"', self.client.out)341        extensions_str = "OFF" if cppstd else ""342        vals = {"CMAKE_CXX_STANDARD": cppstd,343                "CMAKE_CXX_EXTENSIONS": extensions_str,344                "CMAKE_BUILD_TYPE": build_type,345                "CMAKE_CXX_FLAGS": "-m64 -stdlib=libc++",346                "CMAKE_CXX_FLAGS_DEBUG": "-g",347                "CMAKE_CXX_FLAGS_RELEASE": "-O3 -DNDEBUG",348                "CMAKE_C_FLAGS": "-m64",349                "CMAKE_C_FLAGS_DEBUG": "-g",350                "CMAKE_C_FLAGS_RELEASE": "-O3 -DNDEBUG",351                "CMAKE_SHARED_LINKER_FLAGS": "-m64",352                "CMAKE_EXE_LINKER_FLAGS": "-m64",353                "CMAKE_SKIP_RPATH": "1",354                "CMAKE_INSTALL_NAME_DIR": ""355                }356        def _verify_out(marker=">>"):357            if shared:358                self.assertIn("libapp_lib.dylib", self.client.out)359            else:360                if marker == ">>":361                    self.assertIn("libapp_lib.a", self.client.out)362                else:  # Incremental build not the same msg363                    self.assertIn("Built target app_lib", self.client.out)364            out = str(self.client.out).splitlines()365            for k, v in vals.items():366                self.assertIn("%s %s: %s" % (marker, k, v), out)367        _verify_out()368        self._run_app(build_type, dyld_path=shared)369        self._modify_code()370        time.sleep(1)371        self._incremental_build()372        _verify_out(marker="++>>")373        self._run_app(build_type, dyld_path=shared, msg="AppImproved")374@pytest.mark.toolchain375@pytest.mark.tool_cmake376class CMakeInstallTest(unittest.TestCase):377    def test_install(self):378        conanfile = textwrap.dedent("""379            from conans import ConanFile380            from conan.tools.cmake import CMake, CMakeToolchain381            class App(ConanFile):382                settings = "os", "arch", "compiler", "build_type"383                exports_sources = "CMakeLists.txt", "header.h"...migration_writer.py
Source:migration_writer.py  
...9    _skip_code_format = True10INDENT = '    '11class FormattedMigrationWriter(MigrationWriter):12    def as_string(self):13        return self._modify_code(14            super().as_string(),15            self._add_docstring,16            self._format_code,17        )18    def _add_docstring(self, code: str) -> str:19        add_after = 'class Migration(migrations.Migration):\n'20        doc = self.migration.name.split('_', maxsplit=1)[1]21        docstring_line = f'{INDENT}"""{doc}."""\n'22        return code.replace(add_after, add_after + docstring_line)23    def _format_code(self, code):24        if _skip_code_format:25            return code26        code = fix_code(code)27        return fix_commas(code, min_version=(3, 6))28    def _modify_code(self, code, *funcs):29        for func in funcs:30            code = func(code)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
