Skip to content

Commit

Permalink
Merge pull request #36 from masenf/ignore-install-package-deps
Browse files Browse the repository at this point in the history
plugin4: skip install_package_deps
  • Loading branch information
masenf committed Feb 9, 2023
2 parents f5e4e74 + 81fea31 commit a968f82
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/tox_pin_deps/plugin4.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from tox.execute.request import StdinSource
from tox.plugin import impl
from tox.tox_env.api import ToxEnvCreateArgs
from tox.tox_env.python.api import Python
from tox.tox_env.python.pip.pip_install import Pip
from tox.tox_env.python.pip.req_file import PythonDeps
from tox.tox_env.python.virtual_env.runner import VirtualEnvRunner
Expand All @@ -19,6 +20,10 @@
class PipCompileInstaller(PipCompile, Pip):
"""tox4 Installer that uses `pip-compile` or env-specific lock files."""

def __init__(self, tox_env: Python, with_list_deps: bool = True):
self._installed_from_lock_file = False
super().__init__(tox_env, with_list_deps)

@property
def toxinidir(self) -> Path:
return Path(self.venv.core["toxinidir"])
Expand Down Expand Up @@ -97,6 +102,14 @@ def install(self, arguments: t.Any, section: str, of_type: str) -> None:
raw=pinned_deps_spec,
root=self.env_requirements.parent,
)
self._installed_from_lock_file = True
if self._installed_from_lock_file and of_type == "package":
# do not override pinned deps with package requirements
try:
for item in arguments:
item.deps[:] = []
except TypeError:
pass # maybe given something other than a list of packages?
super().install(
arguments=pinned_deps or arguments,
section=section,
Expand Down
3 changes: 0 additions & 3 deletions tests/integration/examples/pyproj/exp_lock_4.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
~renodeps: install_deps> python -I -m pip install -r /.*/pyproj/requirements/nodeps\.txt
nodeps: install_package_deps> python -I -m pip install mock_pkg_quuc
nodeps: install_package>
nodeps: commands[0]> pip freeze
mock-pkg-bar==1.5
Expand All @@ -8,7 +7,6 @@ mock-pkg-quuc==2.2
pyproj @
nodeps: OK
~reprefoo: install_deps> python -I -m pip install --pre -r /.*/pyproj/requirements/prefoo\.txt --pre
prefoo: install_package_deps> python -I -m pip install --pre mock_pkg_quuc --pre
prefoo: install_package>
prefoo: commands[0]> pip freeze
mock-pkg-bar==1.5
Expand All @@ -17,7 +15,6 @@ mock-pkg-quuc==2.2
pyproj @
prefoo: OK
~reoldfoo: install_deps> python -I -m pip install -r /.*/pyproj/requirements/oldfoo\.txt
oldfoo: install_package_deps> python -I -m pip install mock_pkg_quuc
oldfoo: install_package>
oldfoo: commands[0]> pip freeze
mock-pkg-bar==1.5
Expand Down
3 changes: 0 additions & 3 deletions tests/integration/examples/pyproj/exp_pip_compile_4.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
nodeps: tox-pin-deps> pip install pip-tools
~renodeps: tox-pin-deps> pip-compile /.*/pyproj/pyproject\.toml --output-file /.*/pyproj/requirements/nodeps\.txt --generate-hashes -v --resolver=backtracking --extra-index-url
~renodeps: install_deps> python -I -m pip install -r /.*/pyproj/requirements/nodeps\.txt
nodeps: install_package_deps> python -I -m pip install mock_pkg_quuc
nodeps: install_package>
nodeps: commands[0]> pip freeze
mock-pkg-bar==1.5
Expand All @@ -13,7 +12,6 @@ nodeps: OK
prefoo: tox-pin-deps> pip install pip-tools
~reprefoo: tox-pin-deps> pip-compile --pre /.*/pyproj/pyproject\.toml --output-file /.*/pyproj/requirements/prefoo\.txt --resolver=backtracking --extra-index-url
~reprefoo: install_deps> python -I -m pip install --pre -r /.*/pyproj/requirements/prefoo\.txt --pre
prefoo: install_package_deps> python -I -m pip install --pre mock_pkg_quuc --pre
prefoo: install_package>
prefoo: commands[0]> pip freeze
mock-pkg-bar==1.5
Expand All @@ -25,7 +23,6 @@ prefoo: OK
oldfoo: tox-pin-deps> pip install pip-tools
~reoldfoo: tox-pin-deps> pip-compile /.*/pyproj/.tox-pin-deps-oldfoo-requirements\..*\.in /.*/pyproj/pyproject\.toml --output-file /.*/pyproj/requirements/oldfoo\.txt --generate-hashes -v --extra-index-url
~reoldfoo: install_deps> python -I -m pip install -r /.*/pyproj/requirements/oldfoo\.txt
oldfoo: install_package_deps> python -I -m pip install mock_pkg_quuc
oldfoo: install_package>
oldfoo: commands[0]> pip freeze
mock-pkg-bar==1.5
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_plugin4.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,33 @@ def test_register_config(venv):
assert isinstance(orig_installer, tox_pin_deps.plugin4.PipCompileInstaller)
# subsequent access to the installer should return the same instance
assert inst.installer is orig_installer


@pytest.mark.parametrize(
"installed_from_lock_file", [True, False], ids=["installed_from_lock_file", ""]
)
@pytest.mark.parametrize("weird_type", [True, False], ids=["weird_type", "list"])
def test_install_package_deps(venv, installed_from_lock_file, weird_type):
mockdep = mock.Mock()
exp_package_deps = ["foo", "bar"]
mockdep.deps = exp_package_deps
pip_compile_installer = tox_pin_deps.plugin4.PipCompileInstaller(venv)
if installed_from_lock_file:
pip_compile_installer._installed_from_lock_file = installed_from_lock_file
if weird_type:
# typically arguments would be iterable, but Mock is not
arguments = mockdep
else:
arguments = [mockdep]
assert pip_compile_installer.install(arguments, None, "package") is None
pip_mock = ShimBaseMock._get_last_instance_and_reset(assert_n_instances=1)
pip_mock._install_mock.assert_called_once_with(
arguments=arguments,
section=None,
of_type="package",
)
assert len(venv.execute.mock_calls) == 0
if installed_from_lock_file and not weird_type:
assert mockdep.deps == []
else:
assert mockdep.deps == exp_package_deps

0 comments on commit a968f82

Please sign in to comment.