Best Python code snippet using robotframework-pageobjects_python
test_cleanme.py
Source:test_cleanme.py  
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3""" Unit tests for makeproject cleanme4Copyright 2013-2022 by Rebecca Ann Heineman becky@burgerbecky.com5It is released under an MIT Open Source license. Please see LICENSE6for license details. Yes, you can use it in a7commercial title without paying anything, just give me a credit.8Please? It's not like I'm asking you for money!9"""10# pylint: disable=wrong-import-position11import sys12import unittest13import os14import tempfile15import shutil16from burger import save_text_file, delete_file, Interceptstdout17# Insert the location of makeprojects at the begining so it's the first18# to be processed19sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))20import makeprojects21# Line to import the burger library22_IMPORT_BURGER = "import burger"23# Entry point for clean24_DEF_CLEAN = "def clean(working_directory):"25# Return no error26_RETURN_ZERO = "\treturn 0"27# Return error28_RETURN_ONE = "\treturn 1"29# Return None30_RETURN_NONE = "\treturn None"31# Garbage string32_ABC = (33    "abc",34)35########################################36class TestCleanme(unittest.TestCase):37    """38    Test cleanme39    """40########################################41    def setUp(self):42        """43        Handle temporary directory44        """45        self.saved_cwd = os.getcwd()46        self.tmpdir = os.path.realpath(tempfile.mkdtemp())47        # Make sure anything left behind is removed48        self.addCleanup(shutil.rmtree, self.tmpdir)49########################################50    def tearDown(self):51        """52        Restore directory53        """54        # Restore the working directory, if the test did not.55        os.chdir(self.saved_cwd)56########################################57    @staticmethod58    def mkdir(path, *paths):59        """60        Create a directory and return the full pathname.61        """62        # Pass the arguments straight to os.path.join()63        result = os.path.join(path, *paths)64        # Make the directory65        os.mkdir(result)66        # Return the full pathname67        return result68########################################69    @staticmethod70    def save_text_file(path, *paths):71        """72        Create a directory and return the full pathname.73        """74        # Pass the arguments straight to os.path.join()75        result = os.path.join(path, *paths)76        # Write out a text file with a single dummy line77        save_text_file(result, _ABC)78        # Return the full pathname79        return result80########################################81    def test_cleanme(self):82        """83        Test to see if cleanme loads build_rules.py.84        """85        # The script will delete temp and bin but leave source alone86        # If this did not occur, the script did not execute87        # Create some temp folders88        temp_dir = self.mkdir(self.tmpdir, "temp")89        bin_dir = self.mkdir(self.tmpdir, "bin")90        # Create a folder that should not be deleted91        source_dir = self.mkdir(self.tmpdir, "source")92        # Write out the build_rules.py file93        build_rules_py = os.path.join(self.tmpdir, "build_rules.py")94        # First test is to check if clean() is callable95        save_text_file(build_rules_py, [96            "clean = \"clean\""]97        )98        # Make sure it was written99        self.assertTrue(os.path.isfile(build_rules_py))100        # Call cleanme using the directory with the build_rules.py file101        with Interceptstdout() as output:102            makeprojects.clean(self.tmpdir)103        self.assertTrue("not a callable function" in output[0])104        # Perform a test of the clean() function105        save_text_file(build_rules_py, [106            _IMPORT_BURGER,107            _DEF_CLEAN,108            ("\tburger.clean_directories(working_directory, "109            "(\"temp\", \"bin\"))"),110            _RETURN_NONE]111        )112        # Make sure it was written113        self.assertTrue(os.path.isfile(build_rules_py))114        # Call cleanme using the directory with the build_rules.py file115        makeprojects.clean(self.tmpdir)116        # temp and bin should disappear, but not the others117        self.assertFalse(os.path.isdir(temp_dir))118        self.assertFalse(os.path.isdir(bin_dir))119        self.assertTrue(os.path.isdir(source_dir))120########################################121    def test_cleanme_generic(self):122        """123        Test to see if cleanme handles CLEANME_GENERIC.124        """125        # Create some temp folders126        a_dir = self.mkdir(self.tmpdir, "a")127        # Write out the test files128        a_foo_txt = self.save_text_file(a_dir, "foo.txt")129        a_foo_cpp = self.save_text_file(a_dir, "foo.cpp")130        # Write out the build_rules.py files131        build_rules = os.path.join(self.tmpdir, "build_rules.py")132        save_text_file(build_rules, [133            "CLEANME_GENERIC = True",134            _IMPORT_BURGER,135            _DEF_CLEAN,136            "\tburger.clean_files(working_directory, \"*.txt\")",137            _RETURN_NONE]138        )139        # "a" doesn't have a build_rules.py file, so it should140        # check the parent folder141        makeprojects.clean(a_dir)142        # Check if foo.txt is missing but the .cpp is fine143        self.assertFalse(os.path.isfile(a_foo_txt))144        self.assertTrue(os.path.isfile(a_foo_cpp))145        # Try again, but without the generic flag146        # If called, force an exception147        a_foo_txt = self.save_text_file(a_dir, "foo.txt")148        save_text_file(build_rules, [149            'CLEANME_GENERIC = False',150            _IMPORT_BURGER,151            _DEF_CLEAN,152            '\traise RuntimeError("CLEANME_GENERIC = False not handled")',153            _RETURN_NONE]154        )155        makeprojects.clean(a_dir)156        # Test the default state157        save_text_file(build_rules, [158            _IMPORT_BURGER,159            _DEF_CLEAN,160            '\traise RuntimeError("CLEANME_GENERIC missing not handled")',161            _RETURN_NONE]162        )163        makeprojects.clean(a_dir)164########################################165    def test_cleanme_continue(self):166        """167        Test to see if cleanme handles CLEANME_CONTINUE.168        """169        # Create some temp folders170        a_dir = self.mkdir(self.tmpdir, 'a')171        # Write out the test files172        foo_txt = self.save_text_file(self.tmpdir, 'foo.txt')173        foo_cpp = self.save_text_file(self.tmpdir, 'foo.cpp')174        a_foo_txt = self.save_text_file(a_dir, 'foo.txt')175        a_foo_cpp = self.save_text_file(a_dir, 'foo.cpp')176        # Write out the build_rules.py files177        build_rules = os.path.join(self.tmpdir, 'build_rules.py')178        a_build_rules = os.path.join(a_dir, 'build_rules.py')179        # Test CLEANME_CONTINUE = False180        save_text_file(build_rules, [181            'CLEANME_GENERIC = True',182            _IMPORT_BURGER,183            _DEF_CLEAN,184            '\traise RuntimeError("CLEANME_CONTINUE False not handled")',185            _RETURN_NONE]186        )187        save_text_file(a_build_rules, [188            "CLEANME_CONTINUE = False",189            _IMPORT_BURGER,190            _DEF_CLEAN,191            '\tburger.clean_files(working_directory, "*.cpp")',192            _RETURN_NONE]193        )194        makeprojects.clean(a_dir)195        self.assertTrue(os.path.isfile(foo_txt))196        self.assertTrue(os.path.isfile(foo_cpp))197        self.assertTrue(os.path.isfile(a_foo_txt))198        self.assertFalse(os.path.isfile(a_foo_cpp))199        # Test CLEANME_GENERIC = True / CLEANME_CONTINUE = True200        # Reset201        a_foo_cpp = self.save_text_file(a_dir, 'foo.cpp')202        save_text_file(build_rules, [203            "CLEANME_GENERIC = True",204            _IMPORT_BURGER,205            _DEF_CLEAN,206            '\tburger.clean_files(working_directory, "*.txt")',207            _RETURN_NONE]208        )209        save_text_file(a_build_rules, [210            "CLEANME_CONTINUE = True",211            _IMPORT_BURGER,212            _DEF_CLEAN,213            '\tburger.clean_files(working_directory, "*.cpp")',214            _RETURN_NONE]215        )216        makeprojects.clean(a_dir)217        # "a" should delete *.txt and tmp should delete *.cpp218        self.assertTrue(os.path.isfile(foo_txt))219        self.assertTrue(os.path.isfile(foo_cpp))220        self.assertFalse(os.path.isfile(a_foo_txt))221        self.assertFalse(os.path.isfile(a_foo_cpp))222        # Test CLEANME_CONTINUE = not present223        # Reset224        a_foo_txt = self.save_text_file(a_dir, "foo.txt")225        a_foo_cpp = self.save_text_file(a_dir, "foo.cpp")226        save_text_file(build_rules, [227            "CLEANME_GENERIC = True",228            _IMPORT_BURGER,229            _DEF_CLEAN,230            '\traise RuntimeError("CLEANME_CONTINUE not present")',231            _RETURN_NONE]232        )233        save_text_file(a_build_rules, [234            _IMPORT_BURGER,235            _DEF_CLEAN,236            '\tburger.clean_files(working_directory, "*.cpp")',237            _RETURN_NONE]238        )239        makeprojects.clean(a_dir)240        self.assertTrue(os.path.isfile(foo_txt))241        self.assertTrue(os.path.isfile(foo_cpp))242        self.assertTrue(os.path.isfile(a_foo_txt))243        self.assertFalse(os.path.isfile(a_foo_cpp))244        # Test for return value abort245        save_text_file(build_rules, [246            "CLEANME_GENERIC = True",247            _DEF_CLEAN,248            "\traise RuntimeError(\"RETURN 0 didn't abort clean()\")",249            _RETURN_NONE]250        )251        save_text_file(a_build_rules, [252            "CONTINUE = True",253            _DEF_CLEAN,254            _RETURN_ZERO]255        )256        makeprojects.clean(a_dir)257        # Test for returning an error code258        save_text_file(build_rules, [259            "CLEANME_GENERIC = True",260            _DEF_CLEAN,261            "\traise RuntimeError(\"RETURN 0 didn't abort clean()\")",262            _RETURN_NONE]263        )264        save_text_file(a_build_rules, [265            "CONTINUE = True",266            _DEF_CLEAN,267            _RETURN_ONE]268        )269        makeprojects.clean(a_dir)270########################################271    def test_cleanme_dependencies(self):272        """273        Test to see if cleanme handles CLEANME_DEPENDENCIES.274        """275        # Create some temp folders276        a_dir = self.mkdir(self.tmpdir, "a")277        # Write out the test files278        foo_txt = self.save_text_file(self.tmpdir, "foo.txt")279        foo_cpp = self.save_text_file(self.tmpdir, "foo.cpp")280        a_foo_txt = self.save_text_file(a_dir, "foo.txt")281        a_foo_cpp = self.save_text_file(a_dir, "foo.cpp")282        # Write out the build_rules.py files283        build_rules = os.path.join(self.tmpdir, "build_rules.py")284        a_build_rules = os.path.join(a_dir, "build_rules.py")285        # Test CLEANME_CONTINUE = False286        save_text_file(build_rules, [287            'CLEANME_DEPENDENCIES = ["a"]',288            'CLEANME_GENERIC = True',289            _IMPORT_BURGER,290            _DEF_CLEAN,291            '\tburger.clean_files(working_directory, "*.cpp")',292            _RETURN_NONE]293        )294        makeprojects.clean(self.tmpdir)295        self.assertTrue(os.path.isfile(foo_txt))296        self.assertFalse(os.path.isfile(foo_cpp))297        self.assertTrue(os.path.isfile(a_foo_txt))298        self.assertFalse(os.path.isfile(a_foo_cpp))299        # Test CLEANME_CONTINUE = False300        foo_cpp = self.save_text_file(self.tmpdir, 'foo.cpp')301        a_foo_cpp = self.save_text_file(a_dir, 'foo.cpp')302        save_text_file(build_rules, [303            _IMPORT_BURGER,304            _DEF_CLEAN,305            '\tburger.clean_files(working_directory, "*.txt")',306            _RETURN_NONE]307        )308        save_text_file(a_build_rules, [309            'CLEANME_DEPENDENCIES = [".."]',310            _IMPORT_BURGER,311            _DEF_CLEAN,312            '\tburger.clean_files(working_directory, "*.cpp")',313            _RETURN_NONE]314        )315        makeprojects.clean(a_dir)316        self.assertFalse(os.path.isfile(foo_txt))317        self.assertTrue(os.path.isfile(foo_cpp))318        self.assertTrue(os.path.isfile(a_foo_txt))319        self.assertFalse(os.path.isfile(a_foo_cpp))320########################################321    def test_cleanme_no_recurse(self):322        """323        Test to see if cleanme handles CLEANME_NO_RECURSE.324        """325        # Create some temp folders326        a_dir = self.mkdir(self.tmpdir, "a")327        # Write out the test files328        foo_txt = self.save_text_file(self.tmpdir, "foo.txt")329        foo_cpp = self.save_text_file(self.tmpdir, "foo.cpp")330        a_foo_txt = self.save_text_file(a_dir, "foo.txt")331        a_foo_cpp = self.save_text_file(a_dir, "foo.cpp")332        # Write out the build_rules.py files333        build_rules = os.path.join(self.tmpdir, "build_rules.py")334        a_build_rules = os.path.join(a_dir, "build_rules.py")335        # Test CLEANME_NO_RECURSE = True336        save_text_file(build_rules, [337            "CLEANME_NO_RECURSE = True",338            _IMPORT_BURGER,339            _DEF_CLEAN,340            '\tburger.clean_files(working_directory, "*.cpp")',341            _RETURN_NONE]342        )343        save_text_file(a_build_rules, [344            _IMPORT_BURGER,345            _DEF_CLEAN,346            '\traise RuntimeError("CLEANME_NO_RECURSE not parsed")',347            _RETURN_NONE]348        )349        makeprojects.clean(self.tmpdir, ["-r"])350        self.assertTrue(os.path.isfile(foo_txt))351        self.assertFalse(os.path.isfile(foo_cpp))352        self.assertTrue(os.path.isfile(a_foo_txt))353        self.assertTrue(os.path.isfile(a_foo_cpp))354        # Test CLEANME_NO_RECURSE = False355        foo_cpp = self.save_text_file(self.tmpdir, "foo.cpp")356        save_text_file(build_rules, [357            "CLEANME_NO_RECURSE = False",358            _IMPORT_BURGER,359            _DEF_CLEAN,360            '\tburger.clean_files(working_directory, "*.cpp")',361            _RETURN_NONE]362        )363        save_text_file(a_build_rules, [364            _IMPORT_BURGER,365            _DEF_CLEAN,366            '\tburger.clean_files(working_directory, "*.txt")',367            _RETURN_NONE]368        )369        makeprojects.clean(self.tmpdir, ["-r"])370        self.assertTrue(os.path.isfile(foo_txt))371        self.assertFalse(os.path.isfile(foo_cpp))372        self.assertFalse(os.path.isfile(a_foo_txt))373        self.assertTrue(os.path.isfile(a_foo_cpp))374        # Test if a generic rules file prevents recursion375        # No rules in a, failure in b and generic in root.376        foo_cpp = self.save_text_file(self.tmpdir, "foo.cpp")377        a_foo_txt = self.save_text_file(a_dir, "foo.txt")378        b_dir = self.mkdir(a_dir, "b")379        b_build_rules = os.path.join(b_dir, "build_rules.py")380        b_foo_txt = self.save_text_file(b_dir, "foo.txt")381        b_foo_cpp = self.save_text_file(b_dir, "foo.cpp")382        save_text_file(build_rules, [383            "CLEANME_NO_RECURSE = True",384            "CLEANME_GENERIC = True",385            _IMPORT_BURGER,386            _DEF_CLEAN,387            '\tburger.clean_files(working_directory, "*.cpp")',388            _RETURN_NONE]389        )390        delete_file(a_build_rules)391        save_text_file(b_build_rules, [392            _IMPORT_BURGER,393            _DEF_CLEAN,394            '\traise RuntimeError("CLEANME_NO_RECURSE not parsed")',395            _RETURN_NONE]396        )397        makeprojects.clean(a_dir, ["-r"])398        self.assertTrue(os.path.isfile(foo_txt))399        self.assertTrue(os.path.isfile(foo_cpp))400        self.assertTrue(os.path.isfile(a_foo_txt))401        self.assertFalse(os.path.isfile(a_foo_cpp))402        self.assertTrue(os.path.isfile(b_foo_txt))403        self.assertTrue(os.path.isfile(b_foo_cpp))404########################################405    def test_cleanme_missing_clean(self):406        """407        Test for a special case where a file doesn't have the clean() function.408        It was found that if a build_rules.py file did not have a clean()409        function, cleanme would report "clean is not a callable410        function". This is a bug. It should skip the build_rules file.411        """412        # Create a temp folder413        a_dir = self.mkdir(self.tmpdir, "a")414        a_foo_cpp = self.save_text_file(a_dir, "foo.cpp")415        # Write out the build_rules.py files416        build_rules = os.path.join(self.tmpdir, "build_rules.py")417        a_build_rules = os.path.join(a_dir, "build_rules.py")418        # Test CLEANME_NO_RECURSE = True419        save_text_file(build_rules, [420            _IMPORT_BURGER,421            "GENERIC = True",422            _DEF_CLEAN,423            '\tburger.clean_files(working_directory, "*.cpp")',424            _RETURN_ONE]425        )426        save_text_file(a_build_rules, [427            "CONTINUE = True"]428        )429        self.assertEqual(makeprojects.clean(a_dir), 1)430        self.assertFalse(os.path.isfile(a_foo_cpp))431########################################432if __name__ == "__main__":...test_utils.py
Source:test_utils.py  
1from mock import MagicMock2from pyAEATsii import callback_utils3def _return_arg(x):4    return x5def _return_none(x):6    return None7def test_fixed_value():8    fixed_value = callback_utils.fixed_value('RETURN')9    assert fixed_value(None) == 'RETURN'10def test_coalesce_1():11    coalesce = callback_utils.coalesce([12        _return_arg13    ])14    assert coalesce('RETURN') == 'RETURN'15def test_coalesce_0():16    coalesce = callback_utils.coalesce([17    ])18    assert coalesce(None) is None19def test_coalesce_else():...helpers.py
Source:helpers.py  
1from typing import Callable2from .. import config3async def _return_none() -> None:4    return None5def _req_id_owner(func: Callable) -> Callable:6    """A function wrapper that only runs the wrapped function if7    kwargs["message"].author.id == config.BOT_OWNER_ID.8    The wrapped function will return None if message.author does not meet requirements.9    """10    def wrapper(**kwargs):11        message = kwargs["message"]12        if message.author.id == config.BOT_OWNER_ID:13            return func(**kwargs)14        return _return_none()15    return wrapper16def _req_perm_admin(func: Callable) -> Callable:17    """A function wrapper that only runs the wrapped function if18    kwargs["message"].author has the administrator permission.19    The wrapped function will return None if message.author does not meet requirements.20    """21    def wrapper(**kwargs):22        message = kwargs["message"]23        perms = message.author.permissions_in(message.channel)24        if getattr(perms, "administrator", False):25            return func(**kwargs)26        return _return_none()...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!!
