Skip to content

Commit

Permalink
tests: cleanup cache and http usage
Browse files Browse the repository at this point in the history
- ensure tests rely on temporary cache directory
- remove external http call requirement for lock --no-update

Relates-to: python-poetry#1645
  • Loading branch information
abn committed Oct 20, 2020
1 parent 68d6939 commit bcc9de3
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 193 deletions.
4 changes: 2 additions & 2 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def test_config_get_default_value(config):
assert config.get("virtualenvs.create") is True


def test_config_get_processes_depended_on_values(config):
assert os.path.join("/foo", "virtualenvs") == config.get("virtualenvs.path")
def test_config_get_processes_depended_on_values(config, config_cache_dir):
assert str(config_cache_dir / "virtualenvs") == config.get("virtualenvs.path")


def test_config_get_from_environment_variable(config, environ):
Expand Down
21 changes: 19 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,21 @@ def all(self): # type: () -> Dict[str, Any]


@pytest.fixture
def config_source():
def config_cache_dir(tmp_dir):
path = Path(tmp_dir) / ".cache" / "pypoetry"
path.mkdir(parents=True)
return path


@pytest.fixture
def config_virtualenvs_path(config_cache_dir):
return config_cache_dir / "virtualenvs"


@pytest.fixture
def config_source(config_cache_dir):
source = DictConfigSource()
source.add_property("cache-dir", "/foo")
source.add_property("cache-dir", str(config_cache_dir))

return source

Expand Down Expand Up @@ -226,6 +238,7 @@ def _factory(
dependencies=None,
dev_dependencies=None,
pyproject_content=None,
poetry_lock_content=None,
install_deps=True,
):
project_dir = workspace / "poetry-fixture-{}".format(name)
Expand All @@ -249,6 +262,10 @@ def _factory(
dev_dependencies=dev_dependencies,
).create(project_dir, with_tests=False)

if poetry_lock_content:
lock_file = project_dir / "poetry.lock"
lock_file.write_text(data=poetry_lock_content, encoding="utf-8")

poetry = Factory().create_poetry(project_dir)

locker = TestLocker(
Expand Down
30 changes: 18 additions & 12 deletions tests/console/commands/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,39 @@ def test_show_config_with_local_config_file_empty(tester, mocker):
assert "" == tester.io.fetch_output()


def test_list_displays_default_value_if_not_set(tester, config):
def test_list_displays_default_value_if_not_set(tester, config, config_cache_dir):
tester.execute("--list")

expected = """cache-dir = "/foo"
expected = """cache-dir = "{cache}"
experimental.new-installer = true
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.path = {path} # /foo{sep}virtualenvs
virtualenvs.path = {path} # {virtualenvs}
""".format(
path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), sep=os.path.sep
cache=str(config_cache_dir),
path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")),
virtualenvs=str(config_cache_dir / "virtualenvs"),
)

assert expected == tester.io.fetch_output()


def test_list_displays_set_get_setting(tester, config):
def test_list_displays_set_get_setting(tester, config, config_cache_dir):
tester.execute("virtualenvs.create false")

tester.execute("--list")

expected = """cache-dir = "/foo"
expected = """cache-dir = "{cache}"
experimental.new-installer = true
virtualenvs.create = false
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.path = {path} # /foo{sep}virtualenvs
virtualenvs.path = {path} # {virtualenvs}
""".format(
path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), sep=os.path.sep
cache=str(config_cache_dir),
path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")),
virtualenvs=str(config_cache_dir / "virtualenvs"),
)

assert 0 == config.set_config_source.call_count
Expand Down Expand Up @@ -79,19 +83,21 @@ def test_display_single_local_setting(command_tester_factory, fixture_dir):
assert expected == tester.io.fetch_output()


def test_list_displays_set_get_local_setting(tester, config):
def test_list_displays_set_get_local_setting(tester, config, config_cache_dir):
tester.execute("virtualenvs.create false --local")

tester.execute("--list")

expected = """cache-dir = "/foo"
expected = """cache-dir = "{cache}"
experimental.new-installer = true
virtualenvs.create = false
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.path = {path} # /foo{sep}virtualenvs
virtualenvs.path = {path} # {virtualenvs}
""".format(
path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), sep=os.path.sep
cache=str(config_cache_dir),
path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")),
virtualenvs=str(config_cache_dir / "virtualenvs"),
)

assert 1 == config.set_config_source.call_count
Expand Down
29 changes: 19 additions & 10 deletions tests/console/commands/test_lock.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import shutil

import pytest

from poetry.factory import Factory
from poetry.packages import Locker
from poetry.utils._compat import Path
from tests.helpers import get_package


@pytest.fixture
Expand All @@ -18,15 +16,26 @@ def tester(command_tester_factory):


@pytest.fixture
def poetry_with_old_lockfile(fixture_dir, source_dir):
project_dir = source_dir / "project"
shutil.copytree(str(fixture_dir("old_lock")), str(project_dir))
poetry = Factory().create_poetry(cwd=project_dir)
return poetry
def poetry_with_old_lockfile(project_factory, fixture_dir, source_dir):
source = fixture_dir("old_lock")
pyproject_content = (source / "pyproject.toml").read_text(encoding="utf-8")
poetry_lock_content = (source / "poetry.lock").read_text(encoding="utf-8")
return project_factory(
name="foobar",
pyproject_content=pyproject_content,
poetry_lock_content=poetry_lock_content,
)


def test_lock_no_update(command_tester_factory, poetry_with_old_lockfile, repo):
repo.add_package(get_package("sampleproject", "1.3.1"))
repo.add_package(get_package("sampleproject", "2.0.0"))

def test_lock_no_update(command_tester_factory, poetry_with_old_lockfile, http):
http.disable()
locker = Locker(
lock=poetry_with_old_lockfile.pyproject.file.path.parent / "poetry.lock",
local_config=poetry_with_old_lockfile.locker._local_config,
)
poetry_with_old_lockfile.set_locker(locker)

locked_repository = poetry_with_old_lockfile.locker.locked_repository(
with_dev_reqs=True
Expand Down
150 changes: 8 additions & 142 deletions tests/fixtures/old_lock/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/fixtures/old_lock/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Poetry Developer <developer@python-poetry.org>"]

[tool.poetry.dependencies]
python = "^3.8"
docker = "^4.3.1"
sampleproject = ">=1.3.1"

[tool.poetry.dev-dependencies]

Expand Down
7 changes: 5 additions & 2 deletions tests/installation/test_chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_get_cached_archives_for_link(config, mocker):
}


def test_get_cache_directory_for_link(config):
def test_get_cache_directory_for_link(config, config_cache_dir):
chef = Chef(
config,
MockEnv(
Expand All @@ -71,8 +71,11 @@ def test_get_cache_directory_for_link(config):
directory = chef.get_cache_directory_for_link(
Link("https://files.python-poetry.org/poetry-1.1.0.tar.gz")
)

expected = Path(
"/foo/artifacts/ba/63/13/283a3b3b7f95f05e9e6f84182d276f7bb0951d5b0cc24422b33f7a4648"
"{}/artifacts/ba/63/13/283a3b3b7f95f05e9e6f84182d276f7bb0951d5b0cc24422b33f7a4648".format(
config_cache_dir.as_posix()
)
)

assert expected == directory
6 changes: 4 additions & 2 deletions tests/installation/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ def test_execute_executes_a_batch_of_operations(
assert 5 == len(env.executed)


def test_execute_shows_skipped_operations_if_verbose(config, pool, io):
def test_execute_shows_skipped_operations_if_verbose(
config, pool, io, config_cache_dir
):
config = Config()
config.merge({"cache-dir": "/foo"})
config.merge({"cache-dir": config_cache_dir.as_posix()})

env = MockEnv()
executor = Executor(env, pool, config, io)
Expand Down
Loading

0 comments on commit bcc9de3

Please sign in to comment.