Skip to content

Commit

Permalink
Move setup code to fixtures in decide_user_install unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
McSinyx committed Jun 6, 2020
1 parent 384b1b2 commit 004b199
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions tests/unit/test_decide_user_install.py
Expand Up @@ -4,7 +4,7 @@

from itertools import product

from pytest import mark, raises
from pytest import fixture, mark, raises

from pip._internal.commands.install import decide_user_install
from pip._internal.exceptions import CommandError, InstallationError
Expand All @@ -26,6 +26,36 @@ def true(*args, **kwargs):
return True


@fixture
def user_site_enabled(monkeypatch):
"""site.ENABLE_USER_SITE mocked to be True."""
monkeypatch.setattr(ENABLE_USER_SITE, True)


@fixture
def nonexists(monkeypatch):
"""os.path.exists mocked to always return False."""
monkeypatch.setattr(EXISTS, false)


@fixture
def nonwritable(monkeypatch):
"""test_writable_dir mocked to always return False."""
monkeypatch.setattr(WRITABLE, false)


@fixture
def writable(monkeypatch):
"""test_writable_dir mocked to always return True."""
monkeypatch.setattr(WRITABLE, true)


@fixture
def virtualenv_global(monkeypatch):
"""virtualenv_no_global mocked to always return False."""
monkeypatch.setattr(VIRTUALENV_NO_GLOBAL, false)


@mark.parametrize(('use_user_site', 'prefix_path', 'target_dir', 'root_path'),
filter(lambda args: sum(map(bool, args)) > 1,
product((False, True), (None, 'foo'),
Expand All @@ -38,9 +68,8 @@ def test_conflicts(use_user_site, prefix_path, target_dir, root_path):
target_dir=target_dir, root_path=root_path)


def test_target_exists(monkeypatch):
def test_target_exists(writable, monkeypatch):
"""Test exist target path."""
monkeypatch.setattr(WRITABLE, true)
monkeypatch.setattr(EXISTS, true)
monkeypatch.setattr(ISDIR, false)
with raises(InstallationError):
Expand All @@ -51,9 +80,8 @@ def test_target_exists(monkeypatch):
assert decide_user_install(target_dir='bar') is False


def test_target_writable(monkeypatch):
def test_target_writable(nonexists, monkeypatch):
"""Test writable path check with target specified."""
monkeypatch.setattr(EXISTS, false)
monkeypatch.setattr(WRITABLE, false)
with raises(InstallationError):
decide_user_install(target_dir='bar')
Expand All @@ -70,12 +98,11 @@ def test_prefix_writable(monkeypatch):
assert decide_user_install(prefix_path='foo') is False


def test_global_site_writable(monkeypatch):
def test_global_site_writable(virtualenv_global, user_site_enabled,
monkeypatch):
"""Test error handling and user site-packages decision
with writable and non-writable global site-packages.
"""
monkeypatch.setattr(VIRTUALENV_NO_GLOBAL, false)
monkeypatch.setattr(ENABLE_USER_SITE, True)
monkeypatch.setattr(WRITABLE, false)
with raises(InstallationError):
decide_user_install(use_user_site=False)
Expand All @@ -89,11 +116,9 @@ def test_global_site_writable(monkeypatch):
assert decide_user_install() is False


def test_enable_user_site(monkeypatch):
def test_enable_user_site(virtualenv_global, writable, monkeypatch):
"""Test with different values of site.ENABLE_USER_SITE."""
monkeypatch.setattr(VIRTUALENV_NO_GLOBAL, false)
monkeypatch.setattr(ENABLE_USER_SITE, None)
monkeypatch.setattr(WRITABLE, true)
assert decide_user_install() is False
assert decide_user_install(use_user_site=False) is False
with raises(InstallationError):
Expand All @@ -107,18 +132,14 @@ def test_enable_user_site(monkeypatch):
monkeypatch.setattr(ENABLE_USER_SITE, True)
assert decide_user_install(use_user_site=False) is False
assert decide_user_install(use_user_site=True) is True
monkeypatch.setattr(WRITABLE, false)
assert decide_user_install() is True


def test_virtualenv_no_global(monkeypatch):
def test_virtualenv_no_global(user_site_enabled, nonwritable, monkeypatch):
"""Test for final assertion of virtualenv_no_global
when user site-packages is decided to be used.
"""
monkeypatch.setattr(VIRTUALENV_NO_GLOBAL, true)
with raises(InstallationError):
decide_user_install(use_user_site=True)
monkeypatch.setattr(ENABLE_USER_SITE, True)
monkeypatch.setattr(WRITABLE, false)
with raises(InstallationError):
decide_user_install()

0 comments on commit 004b199

Please sign in to comment.