Best Python code snippet using avocado_python
test_handle_install.py
Source:test_handle_install.py  
1#!/usr/bin/python32import unittest3from unittest.mock import patch4from keyman_config import KeymanComUrl, __tier__5from keyman_config.handle_install import download_and_install_package6class HandleInstallTests(unittest.TestCase):7    @patch('keyman_config.handle_install.is_zipfile')8    @patch('keyman_config.handle_install._install_package')9    def test_downloadAndInstallPackage_file(self, installPackageMethod, mockIsZipfile):10        # Setup11        mockIsZipfile.return_value = True12        # Execute13        download_and_install_package('/tmp/keyboard/sil_euro_latin.kmp')14        # Verify15        installPackageMethod.assert_called_with('/tmp/keyboard/sil_euro_latin.kmp', '')16    @patch('keyman_config.handle_install.is_zipfile')17    @patch('keyman_config.handle_install._install_package')18    def test_downloadAndInstallPackage_fileUrl(self, installPackageMethod, mockIsZipfile):19        # Setup20        mockIsZipfile.return_value = True21        # Execute22        download_and_install_package('file:///tmp/keyboard/sil_euro_latin.kmp')23        # Verify24        installPackageMethod.assert_called_with('/tmp/keyboard/sil_euro_latin.kmp', '')25    @patch('keyman_config.handle_install.is_zipfile')26    @patch('keyman_config.handle_install._install_package')27    def test_downloadAndInstallPackage_fileUrlWithBcp47(self, installPackageMethod,28                                                        mockIsZipfile):29        # Setup30        mockIsZipfile.return_value = True31        # Execute32        download_and_install_package('file:///tmp/keyboard/sil_euro_latin.kmp?bcp47=dyo')33        # Verify34        installPackageMethod.assert_called_with('/tmp/keyboard/sil_euro_latin.kmp', 'dyo')35    @patch('keyman_config.handle_install.is_zipfile')36    @patch('keyman_config.handle_install._install_package')37    def test_downloadAndInstallPackage_fileUrlWithBcp47AndVersion(self, installPackageMethod,38                                                                  mockIsZipfile):39        # Setup40        mockIsZipfile.return_value = True41        # Execute42        download_and_install_package('file:///tmp/keyboard/sil_euro_latin.kmp?bcp47=dyo&version=1')43        # Verify44        installPackageMethod.assert_called_with('/tmp/keyboard/sil_euro_latin.kmp', 'dyo')45    @patch('keyman_config.handle_install.is_zipfile')46    @patch('keyman_config.handle_install._install_package')47    def test_downloadAndInstallPackage_InvalidUrl(self, installPackageMethod, mockIsZipfile):48        # Setup49        mockIsZipfile.return_value = True50        # Execute51        download_and_install_package('http://localhost/keyboard/sil_euro_latin.kmp')52        # Verify53        installPackageMethod.assert_not_called()54    @patch('keyman_config.handle_install.is_zipfile')55    @patch('keyman_config.handle_install._install_package')56    @patch('keyman_config.get_kmp.download_kmp_file')57    def test_downloadAndInstallPackage_invalidUrl(self, downloadKmpFileMethod,58                                                  installPackageMethod, mockIsZipfile):59        for url in ['foo://download/keyboard/sil_euro_latin', 'keyman://keyboard/sil_euro_latin',60                    'keyman://download/sil_euro_latin', 'keyman://download/keyboard/']:61            with self.subTest(url=url):62                # Setup63                mockIsZipfile.return_value = True64                # Execute65                download_and_install_package(url)66                # Verify67                downloadKmpFileMethod.assert_not_called()68                installPackageMethod.assert_not_called()69    @patch('keyman_config.handle_install.is_zipfile')70    @patch('keyman_config.handle_install._install_package')71    @patch('keyman_config.get_kmp.keyman_cache_dir')72    @patch('keyman_config.handle_install.download_kmp_file')73    def test_downloadAndInstallPackage_keymanUrl(self, downloadKmpFileMethod,74                                                 keymanCacheDirMethod, installPackageMethod,75                                                 mockIsZipfile):76        # Setup77        mockPackagePath = '/tmp/sil_euro_latin'78        keymanCacheDirMethod.return_value = '/tmp'79        downloadKmpFileMethod.return_value = mockPackagePath80        mockIsZipfile.return_value = True81        # Execute82        download_and_install_package('keyman://download/keyboard/sil_euro_latin')83        # Verify84        downloadKmpFileMethod.assert_called_with(85            KeymanComUrl + '/go/package/download/sil_euro_latin?platform=linux&tier=' + __tier__,86            mockPackagePath)87        installPackageMethod.assert_called_with(mockPackagePath, '')88    @patch('keyman_config.handle_install.is_zipfile')89    @patch('keyman_config.handle_install._install_package')90    @patch('keyman_config.get_kmp.keyman_cache_dir')91    @patch('keyman_config.handle_install.download_kmp_file')92    def test_downloadAndInstallPackage_keymanUrlWithBcp47(self, downloadKmpFileMethod,93                                                          keymanCacheDirMethod,94                                                          installPackageMethod, mockIsZipfile):95        # Setup96        mockPackagePath = '/tmp/sil_euro_latin'97        keymanCacheDirMethod.return_value = '/tmp'98        downloadKmpFileMethod.return_value = mockPackagePath99        mockIsZipfile.return_value = True100        # Execute101        download_and_install_package('keyman://download/keyboard/sil_euro_latin?bcp47=de')102        # Verify103        downloadKmpFileMethod.assert_called_with(104            KeymanComUrl + '/go/package/download/sil_euro_latin?platform=linux&tier=' + __tier__,105            mockPackagePath)106        installPackageMethod.assert_called_with(mockPackagePath, 'de')107if __name__ == '__main__':...main.py
Source:main.py  
...18            print("\n".join(res))19        else:20            click.secho(" *** Could not find '{}' ***".format(p), fg="red")21    return values22def handle_install(req, packages, upgrade=False):23    """ Install or upgrade a package """24    originals = []25    values = []26    f = Freeze()27    # Get all of the original matching lines to output only those lines that28    # changed29    for p in packages:30        found = f.find(p)31        if not upgrade and found:32            click.secho("ERROR: '{}' seems to exist:".format(p), fg="red")33            click.echo("\n".join(found))34            click.secho("exiting...", fg="red")35            sys.exit(-1)36        originals.extend(found)37    cmd = ["pip", "install"]38    if upgrade:39        cmd.append("-U")40    for p in packages:41        command = copy.copy(cmd)42        command.append(p)43        output = subprocess.check_output(args=command, cwd=os.getcwd())44        f = Freeze()45        val = f.find(p)46        for v in val:47            if v not in originals:48                values.append(v)49    if not values:50        click.secho("No packages changed.", fg="red")51    return values52@click.command()53@click.option(54    "--upgrade",55    "-U",56    default=False,57    help="Upgrade if package already exists",58    is_flag=True,59)60@click.option(61    "--skip", "-s", default=False, help="Skip saving to requirements.txt", is_flag=True62)63@click.option(64    "--requirements",65    "-r",66    default=None,67    help="Path to requirements.txt file to update",68    type=click.Path(exists=True),69)70@click.argument("packages", nargs=-1)71def cli(upgrade, skip, requirements, packages):72    """73    Smart management of requirements.txt files74    """75    if not packages:76        click.secho("No packages listed, try pipup --help")77        sys.exit(-1)78    # Grab current requirements79    if requirements is not None:80        req = ReqFile(path=requirements)81    else:82        req = ReqFile()83    found_packages = []84    upgraded_packages = []85    installed_packages = []86    click.secho("Looking for '{}'".format(", ".join(packages)), fg="green")87    for pkg in packages:88        found = freeze.find(pkg)89        if found:90            found_packages.extend(found)91            click.secho("Already installed:", fg="green")92            for f in found:93                click.secho(f)94            if upgrade:95                click.secho("Upgrading:", fg="green")96                upgrades = handle_install(req=req, packages=[pkg], upgrade=upgrade)97                for u in upgrades:98                    click.secho(u)99                upgraded_packages.extend(upgrades)100        else:101            click.secho("Installing '{}'...".format(pkg), fg="green")102            installs = handle_install(req=req, packages=[pkg], upgrade=upgrade)103            for i in installs:104                click.secho(i)105            installed_packages.extend(installs)106        # Save unless we're told otherwise107        if not skip:108            all_packages = installed_packages + upgraded_packages109            if not req.exists:110                click.secho(111                    "Can't find a requirements.txt! You'll need to either create one or update yours manually.",112                    fg="red",113                )114            if req.save(all_packages):115                click.secho("Changes saved to {}".format(req.file_path), fg="green")116            else:...release_changes.py
Source:release_changes.py  
...24        if arg in ["-c"]:25            ignore_next = True26        else:27            ignore_next = False28    def handle_install(lines):29        w.sub(handle_arg, lines.group(1))30    def handle_run(m):31        lines = m.group(0)32        y.sub(handle_install, m.group(1))33    x.sub(handle_run, lines)34    return pkgs35if __name__ == "__main__":36    inpfile = "Dockerfile"37    cfgfile = __file__.replace(".py", ".json")38    TAG = os.getenv("TAG")39    IMAGE_NAME = os.getenv("IMAGE")40    with open(cfgfile) as fp:41        cfg = json.load(fp)42    with open(inpfile) as f:...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!!
