def test_multiple_versions_use_package_dependency(
        delete_pip3_multiple_version_site_packages):
    """
    Case: use project which uses package-dependency.
    Expect: package-dependency is accessible from `pip3-multiple-versions-site-packages` particular version folder.
    """
    runner = CliRunner()
    runner.invoke(cli, [
        'package', 'install', '--name', 'scikit-learn', '--version', '0.22.1'
    ])

    import multiple_versions
    multiple_versions.use(name='scikit-learn',
                          version='0.22.1',
                          import_name='sklearn')

    from sklearn.datasets.setup import configuration
    assert configuration is not None
def test_multiple_versions_use_different_name(
        delete_pip3_multiple_version_site_packages):
    """
    Case: use project where project name is different from package folder name and don't use import name.
    Expect: while using multiple versions' use, the only one, specified, is used in runtime.
    """
    package_version = '3.11.0'

    runner = CliRunner()
    runner.invoke(cli, [
        'package', 'install', '--name', 'djangorestframework', '--version',
        package_version
    ])

    import multiple_versions

    with pytest.raises(expected_exception=ModuleNotFoundError) as error:
        multiple_versions.use(name='djangorestframework',
                              version=package_version)

    assert 'Unknown module folder, package name is different from package folder name.' == error.value.msg
def test_multiple_versions_use(delete_pip3_multiple_version_site_packages,
                               target_version, another_version):
    """
    Case: install two version of the same package.
    Expect: while using multiple versions' use, the only one, specified, is used in runtime.
    """
    runner = CliRunner()
    runner.invoke(cli, [
        'package', 'install', '--name', 'requests', '--version', target_version
    ])
    runner.invoke(cli, [
        'package', 'install', '--name', 'requests', '--version',
        another_version
    ])

    import multiple_versions
    multiple_versions.use(name='requests', version=target_version)

    import requests

    assert target_version == requests.__version__
    assert target_version == pkg_resources.get_distribution('requests').version
def test_multiple_versions_use_same_name(
        delete_pip3_multiple_version_site_packages):
    """
    Case: use project where project name is different from package folder name and use import name.
    Expect: while using multiple versions' use, the only one, specified, is used in runtime.
    """
    package_version = '3.11.0'

    runner = CliRunner()
    runner.invoke(cli, [
        'package', 'install', '--name', 'djangorestframework', '--version',
        package_version
    ])

    import multiple_versions
    multiple_versions.use(name='djangorestframework',
                          version=package_version,
                          import_name='rest_framework')

    import rest_framework

    assert package_version == rest_framework.__version__
    assert package_version == pkg_resources.get_distribution(
        'djangorestframework').version
def test_multiple_versions_use_package_dependency_by_different_versions(
        delete_pip3_multiple_version_site_packages):
    """
    Case: use two versions of project which uses package-dependency.
    Expect: package-dependency is accessible from `pip3-multiple-versions-site-packages` corresponding version folder.
    """
    package_name = 'scikit-learn'

    site_packages = SitePackages()
    sys_path = SysPath()

    runner = CliRunner()
    runner.invoke(
        cli,
        ['package', 'install', '--name', package_name, '--version', '0.22.0'])
    runner.invoke(
        cli,
        ['package', 'install', '--name', package_name, '--version', '0.22.1'])

    import multiple_versions
    multiple_versions.use(name='scikit-learn',
                          version='0.22.0',
                          import_name='sklearn')

    assert f'{site_packages.path}/{package_name}-0.22.0' in sys_path.get()

    import multiple_versions
    multiple_versions.use(name='scikit-learn',
                          version='0.22.1',
                          import_name='sklearn')

    assert f'{site_packages.path}/{package_name}-0.22.0' not in sys_path.get()
    assert f'{site_packages.path}/{package_name}-0.22.1' in sys_path.get()

    from sklearn.datasets.setup import configuration
    assert configuration is not None