Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for --ignore-pipfile flag #69

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ Is `Pipfile.lock` expected to be under source control?

According to `pipenv` documentation, `Pipfile.lock` is not recommended under source control if it is going to be used under multiple Python versions.

What if I still want to use dependencies from `Pipfile.lock`?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can add `ignore_pipfile = True` to tox.ini or run tox with `--ignore-pipfile` flag.

What is the role of `requirements.txt` file?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MockEnvironmentConfig(object):
sitepackages = False
envdir = None
pip_pre = False
ignore_pipfile = False


class MockSession(object):
Expand Down
30 changes: 28 additions & 2 deletions test/unit/test_testenv_install_deps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
from tox_pipenv.plugin import tox_testenv_install_deps
import subprocess
import os
Expand Down Expand Up @@ -58,7 +57,6 @@ def test_install_special_deps(venv, mocker, actioncls):
)



def test_install_pip_pre_deps(venv, mocker, actioncls):
"""
Test that nothing is called when there are no deps
Expand Down Expand Up @@ -87,3 +85,31 @@ def test_install_pip_pre_deps(venv, mocker, actioncls):
cwd=venv.path.dirpath(),
venv=False,
)


def test_install_pip_ignore_pipfile(venv, mocker, actioncls):
"""
Test that --ignore-pipfile flag is passed to pipenv executable
"""
action = actioncls(venv)

mocker.patch.object(os, "environ", autospec=True)
mocker.patch.object(action.venv.envconfig, 'ignore_pipfile', True)
mocker.patch("subprocess.Popen")
result = tox_testenv_install_deps(venv, action)
assert result == True
assert subprocess.Popen.call_count == 1
subprocess.Popen.assert_called_once_with(
[
sys.executable,
"-m",
"pipenv",
"install",
"--dev",
"--ignore-pipfile",
"--keep-outdated",
],
action=action,
cwd=venv.path.dirpath(),
venv=False,
)
52 changes: 52 additions & 0 deletions tox_pipenv/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ def _init_pipenv_environ():


def _clone_pipfile(venv):
"""
Copy Pipfile and Pipfile.lock into virtual environment
"""
if hasattr(venv, 'session'):
root_pipfile_path = venv.session.config.toxinidir.join("Pipfile")
root_pipfile_lock_path = venv.session.config.toxinidir.join("Pipfile.lock")
else:
root_pipfile_path = venv.envconfig.config.toxinidir.join("Pipfile")
root_pipfile_lock_path = venv.envconfig.config.toxinidir.join("Pipfile.lock")

# venv path may not have been created yet
venv.path.ensure(dir=1)
Expand All @@ -35,6 +40,12 @@ def _clone_pipfile(venv):
os.utime(str(root_pipfile_path), None)
if not venv_pipfile_path.check():
root_pipfile_path.copy(venv_pipfile_path)

if _ignore_pipfile(venv):
venv_pipfile_lock_path = venv.path.join("Pipfile.lock")
if root_pipfile_lock_path.exists() and not venv_pipfile_lock_path.check():
root_pipfile_lock_path.copy(venv_pipfile_lock_path)

return venv_pipfile_path


Expand All @@ -46,7 +57,9 @@ def wrap_pipenv_environment(venv, pipfile_path):
os.environ["PIPENV_VIRTUALENV"] = os.path.join(str(venv.path))
old_venv = os.environ.get("VIRTUAL_ENV", None)
os.environ["VIRTUAL_ENV"] = os.path.join(str(venv.path))

yield

if old_pipfile:
os.environ["PIPENV_PIPFILE"] = old_pipfile
if old_pipvenv:
Expand Down Expand Up @@ -98,12 +111,20 @@ def tox_testenv_install_deps(venv, action):
args = [sys.executable, "-m", "pipenv", "install", "--dev"]
if venv.envconfig.pip_pre:
args.append('--pre')

# Use Pipfile.lock instead of Pipfile
if _ignore_pipfile(venv):
action.setactivity("pipenv", "using Pipfile.lock")
args.append("--ignore-pipfile")
args.append('--keep-outdated') # so additional dependencies don't run locking process

with wrap_pipenv_environment(venv, pipfile_path):
if deps:
action.setactivity("installdeps", "%s" % ",".join(list(map(str, deps))))
args += list(map(str, deps))
else:
action.setactivity("installdeps", "[]")

venv._pcall(args, venv=False, action=action, cwd=basepath)

# Return non-None to indicate the plugin has completed
Expand Down Expand Up @@ -183,3 +204,34 @@ def tox_runenvreport(venv, action):

output = output.split("\n")
return output


@hookimpl
def tox_addoption(parser):
help_text = "Ignore Pipfile when installing, using the Pipfile.lock"

parser.add_argument(
"--ignore-pipfile",
action="store_true",
)
parser.add_testenv_attribute(
name="ignore_pipfile",
type="bool",
default=False,
help=help_text,
)


def _ignore_pipfile(venv):
""" In case we want to use Pipfile.lock instead of Pipfile """
# Get the flag from tox.ini
if venv.envconfig.ignore_pipfile is True:
return True
# Get the flag from command line options
try:
if venv.envconfig.config.option.ignore_pipfile is True:
return True
except AttributeError:
pass

return False
Loading