Exemple #1
0
def test_project_existing_venv():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok'])
        runner.invoke(hatch, ['new', 'ko', '-ne'])
        venv_dir = os.path.join(d, 'venv')
        package_dir = os.path.join(d, 'ko')
        wait_until(is_venv, venv_dir)
        assert os.path.exists(venv_dir)

        with env_vars({'_IGNORE_VENV_': '1'}):
            runner.invoke(hatch, ['install', package_dir])
        wait_for_os()

        with venv(venv_dir):
            assert 'ko' in get_installed_packages(editable=False)

        with env_vars({'_IGNORE_VENV_': '1'}):
            result = runner.invoke(hatch, ['uninstall', 'ko', '-y'])
        wait_for_os()

        with venv(venv_dir):
            assert 'ko' not in get_installed_packages(editable=False)

        assert result.exit_code == 0
        assert 'A project has been detected!' not in result.output
        assert 'Uninstalling for this project...' in result.output
Exemple #2
0
def test_packages():
    with temp_chdir() as d:
        runner = CliRunner()
        venv_dir = os.path.join(d, 'venv')
        create_venv(venv_dir)

        with venv(venv_dir):
            assert 'six' not in get_installed_packages()
            result = runner.invoke(hatch, ['install', 'six'])
            assert 'six' in get_installed_packages()

        assert result.exit_code == 0
Exemple #3
0
def test_requirements():
    with temp_chdir() as d:
        runner = CliRunner()
        venv_dir = os.path.join(d, 'venv')
        create_venv(venv_dir)
        with open(os.path.join(d, 'requirements.txt'), 'w') as f:
            f.write('six\n')

        with venv(venv_dir):
            runner.invoke(hatch, ['install', '-nd', 'six'])
            assert 'six' in get_installed_packages()
            result = runner.invoke(hatch, ['uninstall', '-nd', '-y'])
            assert 'six' not in get_installed_packages()

        assert result.exit_code == 0
Exemple #4
0
def test_project_existing_venv_all_packages():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok'])
        venv_dir = os.path.join(d, 'venv')
        wait_until(is_venv, venv_dir)
        assert os.path.exists(venv_dir)

        with env_vars({'_IGNORE_VENV_': '1'}):
            runner.invoke(hatch, ['install', 'six==1.9.0'])
        wait_for_os()
        assert os.path.exists(venv_dir)

        with venv(venv_dir):
            assert 'ok' in get_installed_packages()
            initial_version = get_version_as_bytes('six')

        with env_vars({'_IGNORE_VENV_': '1'}):
            result = runner.invoke(hatch, ['update', '--all'])
        wait_for_os()

        with venv(venv_dir):
            final_version = get_version_as_bytes('six')

        assert result.exit_code == 0
        assert initial_version < final_version
        assert 'A project has been detected!' not in result.output
        assert 'Updating for this project...' in result.output
Exemple #5
0
def test_restore_success():
    with temp_chdir() as d:
        runner = CliRunner()

        env_name, fake_name = get_new_venv_name(count=2)
        venv_origin = os.path.join(d, env_name)
        venv_dir = os.path.join(get_venv_dir(), env_name)
        fake_venv = os.path.join(get_venv_dir(), fake_name)

        create_venv(venv_origin)
        copy_path(venv_origin, get_venv_dir())
        os.makedirs(fake_venv)

        try:
            runner.invoke(hatch, ['env', env_name])
            wait_until(is_venv, venv_dir)

            result = runner.invoke(hatch, ['env', '-r'])
            with venv(venv_dir):
                install_packages(['six'])
                installed_packages = get_installed_packages()
        finally:
            remove_path(venv_dir)
            remove_path(fake_venv)

        assert result.exit_code == 0
        assert 'Successfully restored all available virtual envs.' in result.output
        assert 'six' in installed_packages
Exemple #6
0
def test_clone_success():
    with temp_chdir():
        runner = CliRunner()

        origin = os.urandom(10).hex()
        while os.path.exists(os.path.join(VENV_DIR, origin)):  # no cov
            origin = os.urandom(10).hex()

        origin_dir = os.path.join(VENV_DIR, origin)
        clone_dir = origin_dir

        try:
            runner.invoke(hatch, ['env', origin])
            with venv(origin_dir):
                install_packages(['requests'])

            clone = os.urandom(10).hex()
            while os.path.exists(os.path.join(VENV_DIR, clone)):  # no cov
                clone = os.urandom(10).hex()

            clone_dir = os.path.join(VENV_DIR, clone)

            result = runner.invoke(hatch, ['env', clone, '-c', origin])
            with venv(clone_dir):
                install_packages(['six'])
                installed_packages = get_installed_packages()
        finally:
            remove_path(origin_dir)
            remove_path(clone_dir)

        assert result.exit_code == 0
        assert 'Successfully cloned virtual env `{}` from `{}` to {}.'.format(
            clone, origin, clone_dir) in result.output
        assert 'requests' in installed_packages
        assert 'six' in installed_packages
Exemple #7
0
def test_project_no_venv_install_dev_requirements():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic', '-ne'])
        with open(os.path.join(d, 'dev-requirements.txt'), 'w') as f:
            f.write('six\n')

        create_test_passing(d)
        with env_vars({'_IGNORE_VENV_': '1'}):
            result = runner.invoke(hatch, ['test'])

        with venv(os.path.join(d, 'venv')):
            assert 'ok' in get_editable_packages()
            installed_packages = get_installed_packages(editable=False)
            assert 'pytest' in installed_packages
            assert 'coverage' in installed_packages
            assert 'six' in installed_packages

        assert result.exit_code == 0
        assert 'A project has been detected!' in result.output
        assert 'Creating a dedicated virtual env... complete!' in result.output
        assert 'Installing this project in the virtual env...' in result.output
        assert 'Ensuring pytest and coverage are available...' in result.output
        assert 'Installing test dependencies in the virtual env...' in result.output
        assert '1 passed' in result.output
Exemple #8
0
def test_clone_success():
    with temp_chdir():
        runner = CliRunner()

        origin, clone = get_new_venv_name(count=2)
        origin_dir = os.path.join(get_venv_dir(), origin)
        clone_dir = os.path.join(get_venv_dir(), clone)

        try:
            runner.invoke(hatch, ['env', origin])
            wait_until(is_venv, origin_dir)
            with venv(origin_dir):
                install_packages(['requests'])

            result = runner.invoke(hatch, ['env', '-c', origin, clone])
            wait_until(is_venv, clone_dir)
            with venv(clone_dir):
                install_packages(['six'])
                installed_packages = get_installed_packages()
        finally:
            remove_path(origin_dir)
            remove_path(clone_dir)

        assert result.exit_code == 0
        assert 'Successfully cloned virtual env `{}` from `{}` to `{}`.'.format(
            clone, origin, clone_dir) in result.output
        assert 'requests' in installed_packages
        assert 'six' in installed_packages
Exemple #9
0
def test_env():
    with temp_chdir():
        runner = CliRunner()

        env_name = get_new_venv_name()
        venv_dir = os.path.join(VENV_DIR, env_name)
        create_venv(venv_dir)

        try:
            with venv(venv_dir):
                assert 'six' not in get_installed_packages()
            result = runner.invoke(hatch, ['install', '-e', env_name, 'six'])
            with venv(venv_dir):
                assert 'six' in get_installed_packages()
        finally:
            remove_path(venv_dir)

        assert result.exit_code == 0
Exemple #10
0
def test_project_not_detected_when_venv_active():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '-ne'])
        runner.invoke(hatch, ['new', 'ko'])
        venv_dir = os.path.join(d, 'ko', 'venv')
        wait_until(is_venv, venv_dir)
        assert os.path.exists(venv_dir)

        with venv(venv_dir):
            runner.invoke(hatch, ['install'])
            wait_for_os()
            assert 'ok' in get_installed_packages(editable=False)
            result = runner.invoke(hatch, ['uninstall', 'ok', '-y'])
            wait_for_os()
            assert 'ok' not in get_installed_packages(editable=False)

        assert result.exit_code == 0
        assert 'A project has been detected!' not in result.output
        assert 'Uninstalling...' in result.output
Exemple #11
0
def test_get_installed_packages_no_editable():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic', '-ne'])

        venv_dir = os.path.join(d, 'venv')
        create_venv(venv_dir)

        with venv(venv_dir):
            install_packages(['six'])
            install_packages(['-e', '.'])
            packages = get_installed_packages(editable=False)
            assert 'six' in packages
            assert 'ok' not in packages
Exemple #12
0
def test_env():
    with temp_chdir():
        runner = CliRunner()

        env_name = os.urandom(10).hex()
        while os.path.exists(os.path.join(VENV_DIR, env_name)):  # no cov
            env_name = os.urandom(10).hex()

        venv_dir = os.path.join(VENV_DIR, env_name)
        create_venv(venv_dir)

        try:
            runner.invoke(hatch, ['install', '-e', env_name, 'six'])
            with venv(venv_dir):
                assert 'six' in get_installed_packages()
            result = runner.invoke(hatch,
                                   ['uninstall', '-y', '-e', env_name, 'six'])
            with venv(venv_dir):
                assert 'six' not in get_installed_packages()
        finally:
            remove_path(venv_dir)

        assert result.exit_code == 0
Exemple #13
0
def test_project_existing_venv():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '--basic'])
        venv_dir = os.path.join(d, 'venv')
        wait_until(is_venv, venv_dir)
        with venv(venv_dir):
            install_packages(['pytest', 'coverage'])
            installed_packages = get_installed_packages(editable=False)
            assert 'pytest' in installed_packages
            assert 'coverage' in installed_packages

        create_test_passing(d)
        with env_vars({'_IGNORE_VENV_': '1'}):
            result = runner.invoke(hatch, ['test'])

        assert result.exit_code == 0
        assert '1 passed' in result.output
Exemple #14
0
def test_requirements_includes_hatch():
    with temp_chdir() as d:
        runner = CliRunner()
        with open(os.path.join(d, 'requirements.txt'), 'w') as f:
            f.write('requests==2.18.1\nhatch>=0.0.1\n')

        venv_dir = os.path.join(d, 'venv')
        create_venv(venv_dir)

        with venv(venv_dir):
            install_packages(['requests==2.17.3'])
            initial_version = get_version_as_bytes('requests')
            result = runner.invoke(hatch, ['update', '-nd'])
            final_version = get_version_as_bytes('requests')
            installed_packages = get_installed_packages()

        assert result.exit_code == 0
        assert initial_version < final_version
        assert 'hatch' not in installed_packages
Exemple #15
0
def test_project_no_venv():
    with temp_chdir() as d:
        runner = CliRunner()
        runner.invoke(hatch, ['init', 'ok', '-ne'])
        venv_dir = os.path.join(d, 'venv')

        assert not os.path.exists(venv_dir)
        with env_vars({'_IGNORE_VENV_': '1'}):
            result = runner.invoke(hatch, ['update', 'six'])
        wait_until(is_venv, venv_dir)
        assert os.path.exists(venv_dir)

        with venv(venv_dir):
            assert 'ok' in get_installed_packages()

        assert result.exit_code == 0
        assert 'A project has been detected!' in result.output
        assert 'Creating a dedicated virtual env... complete!' in result.output
        assert 'Installing this project in the virtual env... complete!' in result.output
        assert 'Updating for this project...' in result.output
Exemple #16
0
def test_restore_success():
    with temp_chdir() as d:
        runner = CliRunner()

        env_name = os.urandom(10).hex()
        while os.path.exists(os.path.join(VENV_DIR, env_name)):  # no cov
            env_name = os.urandom(10).hex()

        venv_origin = os.path.join(d, env_name)
        create_venv(venv_origin)

        venv_dir = os.path.join(VENV_DIR, env_name)
        copy_path(venv_origin, VENV_DIR)

        fake_venv = ''

        try:
            runner.invoke(hatch, ['env', env_name])

            env_name = os.urandom(10).hex()
            while os.path.exists(os.path.join(VENV_DIR, env_name)):  # no cov
                env_name = os.urandom(10).hex()
            fake_venv = os.path.join(VENV_DIR, env_name)
            os.makedirs(fake_venv)

            result = runner.invoke(hatch, ['env', '-r'])
            with venv(venv_dir):
                install_packages(['six'])
                installed_packages = get_installed_packages()
        finally:
            remove_path(venv_dir)
            remove_path(fake_venv)

        assert result.exit_code == 0
        assert 'Successfully restored all available virtual envs.' in result.output
        assert 'six' in installed_packages
Exemple #17
0
def update(packages, env_name, eager, all_packages, infra, global_install,
           force, dev, as_module, self, quiet):
    """If the option --env is supplied, the update will be applied using
    that named virtual env. Unless the option --global is selected, the
    update will only affect the current user. Of course, this will have
    no effect if a virtual env is in use. The desired name of the admin
    user can be set with the `_DEFAULT_ADMIN_` environment variable.

    When performing a global update, your system may use an older version
    of pip that is incompatible with some features such as --eager. To
    force the use of these features, use --force.

    With no packages nor options selected, this will update packages by
    looking for a `requirements.txt` or a dev version of that in the current
    directory.

    To update this tool, use the --self flag. After the update, you may want
    to press Enter. All other methods of updating will ignore `hatch`. See:
    https://github.com/pypa/pip/issues/1299
    """
    command = ['install', '--upgrade'] + (['-q'] if quiet else [])
    if not global_install or force:  # no cov
        command.extend(['--upgrade-strategy', 'eager' if eager else 'only-if-needed'])

    infra_packages = ['pip', 'setuptools', 'wheel']
    temp_dir = None

    # Windows' `runas` allows only a single argument for the
    # command so we catch this case and turn our command into
    # a string later.
    windows_admin_command = None

    if self:  # no cov
        as_module = True

    if env_name:
        venv_dir = os.path.join(VENV_DIR, env_name)
        if not os.path.exists(venv_dir):
            click.echo('Virtual env named `{}` does not exist.'.format(env_name))
            sys.exit(1)

        with venv(venv_dir):
            executable = (
                [sys.executable if self else get_proper_python(), '-m', 'pip']
                if as_module or (infra and ON_WINDOWS)
                else [get_proper_pip()]
            )
            command = executable + command
            if all_packages:
                installed_packages = infra_packages if infra else get_installed_packages()
            else:
                installed_packages = None
    else:
        venv_dir = None
        executable = (
            [sys.executable if self else get_proper_python(), '-m', 'pip']
            if as_module or (infra and ON_WINDOWS)
            else [get_proper_pip()]
        )
        command = executable + command
        if all_packages:
            installed_packages = infra_packages if infra else get_installed_packages()
        else:
            installed_packages = None

        if not venv_active():  # no cov
            if global_install:
                if ON_WINDOWS:
                    windows_admin_command = get_admin_command()
                else:
                    command = get_admin_command() + command
            else:
                command.append('--user')

    if self:  # no cov
        command.append('hatch')
        if venv_dir:
            with venv(venv_dir):
                subprocess.Popen(command, shell=NEED_SUBPROCESS_SHELL)
        else:
            subprocess.Popen(command, shell=NEED_SUBPROCESS_SHELL)
        sys.exit()
    elif infra:
        command.extend(infra_packages)
    elif all_packages:
        installed_packages = [
            package for package in installed_packages
            if package not in infra_packages and package != 'hatch'
        ]
        if not installed_packages:
            click.echo('No packages installed.')
            sys.exit(1)
        command.extend(installed_packages)
    elif packages:
        packages = [package for package in packages if package != 'hatch']
        if not packages:
            click.echo('No packages to install.')
            sys.exit(1)
        command.extend(packages)

    # When https://github.com/pypa/pipfile is finalized, we'll use it.
    else:
        reqs = get_requirements_file(os.getcwd(), dev=dev)
        if not reqs:
            click.echo('Unable to locate a requirements file.')
            sys.exit(1)

        with open(reqs, 'r') as f:
            lines = f.readlines()

        matches = []
        for line in lines:
            match = re.match(r'^[^=<>]+', line.lstrip())
            if match and match.group(0) == 'hatch':
                matches.append(line)

        if matches:
            for line in matches:
                lines.remove(line)

            temp_dir = TemporaryDirectory()
            reqs = os.path.join(temp_dir.name, basepath(reqs))

            with open(reqs, 'w') as f:
                f.writelines(lines)

        command.extend(['-r', reqs])

    if windows_admin_command:  # no cov
        command = windows_admin_command + [' '.join(command)]

    if venv_dir:
        with venv(venv_dir):
            result = subprocess.run(command, shell=NEED_SUBPROCESS_SHELL)
    else:
        result = subprocess.run(command, shell=NEED_SUBPROCESS_SHELL)

    if temp_dir is not None:
        temp_dir.cleanup()

    sys.exit(result.returncode)