Skip to content

Commit

Permalink
Speed up tests (#2257)
Browse files Browse the repository at this point in the history
When testing the behavior for git packages with a setup.py file, the Provider class would create a temporary virtual environment and execute python setup.py egg_info. Both actions are costly and take time. By mocking them, we can reduce the tests time by a factor of 4 or 5.
  • Loading branch information
sdispater authored Apr 2, 2020
1 parent 655f582 commit bf331a5
Show file tree
Hide file tree
Showing 26 changed files with 125 additions and 9 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*.egg
!/tests/**/*.egg
/*.egg-info
/tests/fixtures/**/*.egg-info
/dist/*
build
_build
Expand Down
11 changes: 7 additions & 4 deletions poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,7 @@ def get_package_from_directory(
os.chdir(str(directory))

try:
with temporary_directory() as tmp_dir:
EnvManager.build_venv(tmp_dir)
venv = VirtualEnv(Path(tmp_dir), Path(tmp_dir))
venv.run("python", "setup.py", "egg_info")
cls._execute_setup()
except EnvCommandError:
result = SetupReader.read_from_directory(directory)
if not result["name"]:
Expand Down Expand Up @@ -817,3 +814,9 @@ def progress(self):
yield

self._in_progress = False

def _execute_setup(self):
with temporary_directory() as tmp_dir:
EnvManager.build_venv(tmp_dir)
venv = VirtualEnv(Path(tmp_dir), Path(tmp_dir))
venv.run("python", "setup.py", "egg_info")
20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,23 @@ known_third_party = [
"shellingham",
"tomlkit",
]


[tool.black]
line-length = 88
include = '\.pyi?$'
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
| tests/.*/setup.py
)/
'''
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def download_mock(mocker):
mocker.patch("poetry.utils.inspector.Inspector.download", new=mock_download)


@pytest.fixture(autouse=True)
def execute_setup_mock(mocker):
mocker.patch("poetry.puzzle.provider.Provider._execute_setup")


@pytest.fixture
def environ():
original_environ = dict(os.environ)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Metadata-Version: 2.1
Name: demo
Version: 0.1.2
Summary: Demo project.
Home-page: https://github.com/demo/demo
Author: Sébastien Eustace
Author-email: [email protected]
License: MIT
Description: UNKNOWN
Platform: UNKNOWN
Provides-Extra: bar
Provides-Extra: foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
setup.py
demo/__init__.py
demo.egg-info/PKG-INFO
demo.egg-info/SOURCES.txt
demo.egg-info/dependency_links.txt
demo.egg-info/requires.txt
demo.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pendulum>=1.4.4

[bar]
tomlkit

[foo]
cleo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Metadata-Version: 1.0
Name: demo
Version: 0.1.2
Summary: Demo project.
Home-page: https://github.com/demo/demo
Author: Sébastien Eustace
Author-email: [email protected]
License: MIT
Description: UNKNOWN
Platform: UNKNOWN
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
setup.py
demo/__init__.py
demo.egg-info/PKG-INFO
demo.egg-info/SOURCES.txt
demo.egg-info/dependency_links.txt
demo.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Metadata-Version: 2.1
Name: Demo
Version: 0.1.2
Summary: Demo project.
Home-page: https://github.com/demo/demo
Author: Sébastien Eustace
Author-email: [email protected]
License: MIT
Description: UNKNOWN
Platform: UNKNOWN
Provides-Extra: foo
Provides-Extra: bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
setup.py
Demo.egg-info/PKG-INFO
Demo.egg-info/SOURCES.txt
Demo.egg-info/dependency_links.txt
Demo.egg-info/requires.txt
Demo.egg-info/top_level.txt
demo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pendulum>=1.4.4

[bar]
tomlkit

[foo]
cleo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
demo
10 changes: 10 additions & 0 deletions tests/fixtures/project_with_setup/my_package.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Metadata-Version: 1.0
Name: my-package
Version: 0.1.2
Summary: Demo project.
Home-page: https://github.com/demo/demo
Author: Sébastien Eustace
Author-email: [email protected]
License: MIT
Description: UNKNOWN
Platform: UNKNOWN
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
setup.py
my_package/__init__.py
my_package.egg-info/PKG-INFO
my_package.egg-info/SOURCES.txt
my_package.egg-info/dependency_links.txt
my_package.egg-info/requires.txt
my_package.egg-info/top_level.txt
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pendulum>=1.4.4
cachy[msgpack]>=0.2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
my_package
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
My Package
==========
==========
2 changes: 1 addition & 1 deletion tests/puzzle/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_search_for_vcs_read_setup_with_extras(provider, mocker):

def test_search_for_vcs_read_setup_raises_error_if_no_version(provider, mocker):
mocker.patch(
"poetry.utils.env.VirtualEnv.run",
"poetry.puzzle.provider.Provider._execute_setup",
side_effect=EnvCommandError(CalledProcessError(1, "python", output="")),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,3 @@ A full list of contributors can be found in `GitHub's overview <https://github.c

It’s the spiritual successor of `characteristic <https://characteristic.readthedocs.io/>`_ and aspires to fix some of it clunkiness and unfortunate decisions.
Both were inspired by Twisted’s `FancyEqMixin <https://twistedmatrix.com/documents/current/api/twisted.python.util.FancyEqMixin.html>`_ but both are implemented using class decorators because `subclassing is bad for you <https://www.youtube.com/watch?v=3MNVP9-hglc>`_, m’kay?


0 comments on commit bf331a5

Please sign in to comment.