Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Aug 3, 2021
1 parent 8ce2c70 commit d676346
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 56 deletions.
2 changes: 2 additions & 0 deletions pipenv/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json as simplejson
import logging
import os
from posixpath import expandvars
import sys
import time
import warnings
Expand Down Expand Up @@ -2387,6 +2388,7 @@ def _launch_windows_subprocess(script, path):
command = system_which(script.command, path=path)

options = {"universal_newlines": True}
script.cmd_args[1:] = [expandvars(arg) for arg in script.args]

# Command not found, maybe this is a shell built-in?
if not command:
Expand Down
2 changes: 1 addition & 1 deletion pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def pipfile_package_names(self):
def get_environment(self, allow_global=False):
# type: (bool) -> Environment
is_venv = is_in_virtualenv()
if allow_global or is_venv:
if allow_global and not is_venv:
prefix = sys.prefix
python = sys.executable
else:
Expand Down
12 changes: 3 additions & 9 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,7 @@ def pathlib_tmpdir(request, tmpdir):


def _create_tracked_dir():
tmp_location = os.environ.get("TEMP", os.environ.get("TMP"))
temp_args = {"prefix": "pipenv-", "suffix": "-test"}
if tmp_location is not None:
temp_args["dir"] = tmp_location
temp_path = create_tracked_tempdir(**temp_args)
return temp_path

Expand Down Expand Up @@ -505,7 +502,7 @@ def __init__(self, name="venv", base_dir=None):
base_dir = Path(_create_tracked_dir())
self.base_dir = base_dir
self.name = name
self.path = base_dir / name
self.path = (base_dir / name).resolve()

def __enter__(self):
self._old_environ = os.environ.copy()
Expand Down Expand Up @@ -536,17 +533,14 @@ def activate(self):
code = compile(f.read(), str(activate_this), "exec")
exec(code, dict(__file__=str(activate_this)))
os.environ["VIRTUAL_ENV"] = str(self.path)
try:
return self.path.absolute().resolve()
except OSError:
return self.path.absolute()
return self.path
else:
raise VirtualenvActivationException("Can't find the activate_this.py script.")


@pytest.fixture()
def virtualenv(vistir_tmpdir):
with temp_environ(), VirtualEnv(base_dir=vistir_tmpdir) as venv:
with VirtualEnv(base_dir=vistir_tmpdir) as venv:
yield venv


Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from flaky import flaky

from pipenv.utils import normalize_drive
from pipenv.utils import normalize_drive, subprocess_run


@pytest.mark.cli
Expand Down Expand Up @@ -217,8 +217,8 @@ def test_help(PipenvInstance):

@pytest.mark.cli
def test_man(PipenvInstance):
with PipenvInstance() as p:
c = p.pipenv('--man')
with PipenvInstance():
c = subprocess_run(["pipenv", "--man"])
assert c.returncode == 0, c.stderr


Expand Down
9 changes: 2 additions & 7 deletions tests/integration/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,16 +646,11 @@ def test_lock_with_incomplete_source(PipenvInstance):

@pytest.mark.lock
@pytest.mark.install
def test_lock_no_warnings(PipenvInstance):
def test_lock_no_warnings(PipenvInstance, recwarn):
with PipenvInstance(chdir=True) as p:
os.environ["PYTHONWARNINGS"] = "once"
c = p.pipenv("install six")
assert c.returncode == 0
c = p.pipenv('run python -c "import warnings; warnings.warn(\\"This is a warning\\", DeprecationWarning); print(\\"hello\\")"')
assert c.returncode == 0
assert "Warning" in c.stderr
assert "Warning" not in c.stdout
assert "hello" in c.stdout
assert len(recwarn) == 0


@pytest.mark.lock
Expand Down
37 changes: 14 additions & 23 deletions tests/integration/test_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import tarfile

from pathlib import Path

import pytest

from pipenv.patched import pipfile
Expand Down Expand Up @@ -170,38 +172,26 @@ def test_include_editable_packages(PipenvInstance, testsroot, pathlib_tmpdir):
@pytest.mark.virtualenv
def test_run_in_virtualenv_with_global_context(PipenvInstance, virtualenv):
with PipenvInstance(chdir=True, venv_root=virtualenv.as_posix(), ignore_virtualenvs=False, venv_in_project=False) as p:
c = subprocess_run(
["pipenv", "run", "pip", "freeze"], cwd=os.path.abspath(p.path),
env=os.environ.copy()
)
c = p.pipenv("run pip freeze")
assert c.returncode == 0, (c.stdout, c.stderr)
assert 'Creating a virtualenv' not in c.stderr, c.stderr
project = Project()
assert project.virtualenv_location == str(virtualenv), (
assert Path(project.virtualenv_location).resolve() == Path(virtualenv), (
project.virtualenv_location, str(virtualenv)
)
c = subprocess_run(
["pipenv", "run", "pip", "install", "-i", p.index_url, "click"],
cwd=os.path.abspath(p.path),
env=os.environ.copy()
)

c = p.pipenv(f"run pip install -i {p.index_url} click")
assert c.returncode == 0, (c.stdout, c.stderr)
assert "Courtesy Notice" in c.stderr, (c.stdout, c.stderr)
c = subprocess_run(
["pipenv", "install", "-i", p.index_url, "six"],
cwd=os.path.abspath(p.path), env=os.environ.copy()
)

c = p.pipenv("install six")
assert c.returncode == 0, (c.stdout, c.stderr)
c = subprocess_run(
['pipenv', 'run', 'python', '-c', 'import click;print(click.__file__)'],
cwd=os.path.abspath(p.path), env=os.environ.copy()
)

c = p.pipenv("run python -c 'import click;print(click.__file__)'")
assert c.returncode == 0, (c.stdout, c.stderr)
assert is_in_path(c.stdout.strip(), str(virtualenv)), (c.stdout.strip(), str(virtualenv))
c = subprocess_run(
["pipenv", "clean", "--dry-run"], cwd=os.path.abspath(p.path),
env=os.environ.copy()
)

c = p.pipenv("clean --dry-run")
assert c.returncode == 0, (c.stdout, c.stderr)
assert "click" in c.stdout, c.stdout

Expand All @@ -227,6 +217,7 @@ def test_run_in_virtualenv(PipenvInstance):
assert c.returncode == 0
assert "click" in c.stdout


@pytest.mark.project
@pytest.mark.sources
def test_no_sources_in_pipfile(PipenvInstance):
Expand All @@ -235,7 +226,7 @@ def test_no_sources_in_pipfile(PipenvInstance):
contents = """
[packages]
pytest = "*"
""".format(os.environ['PIPENV_TEST_INDEX']).strip()
""".strip()
f.write(contents)
c = p.pipenv('install --skip-lock')
assert c.returncode == 0
13 changes: 6 additions & 7 deletions tests/integration/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import pytest

from pipenv.project import Project
from pipenv.utils import temp_environ
from pipenv.utils import subprocess_run, temp_environ


@pytest.mark.run
@pytest.mark.dotenv
def test_env(PipenvInstance):
with PipenvInstance(pipfile=False, chdir=True) as p:
with open('.env', 'w') as f:
f.write('HELLO=WORLD')
c = p.pipenv('run python -c "import os; print(os.environ[\'HELLO\'])"')
with open(os.path.join(p.path, ".env"), "w") as f:
f.write("HELLO=WORLD")
c = subprocess_run(['pipenv', 'run', 'python', '-c', "import os; print(os.environ['HELLO'])"], env=p.env)
assert c.returncode == 0
assert 'WORLD' in c.stdout

Expand Down Expand Up @@ -40,11 +40,10 @@ def test_scripts(PipenvInstance):
assert not c.stderr.strip()

c = p.pipenv('run notfoundscript')
assert c.returncode == 1
assert c.returncode != 0
assert c.stdout == ''
if os.name != 'nt': # TODO: Implement this message for Windows.
assert 'Error' in c.stderr
assert 'randomthingtotally (from notfoundscript)' in c.stderr
assert 'not found' in c.stderr

project = Project()

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_sync_error_without_lockfile(PipenvInstance):

c = p.pipenv('sync')
assert c.returncode != 0
assert 'Pipfile.lock not found!' in str(c.exception)
assert 'Pipfile.lock not found!' in c.stderr


@pytest.mark.sync
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,4 @@ def test_uninstall_missing_parameters(PipenvInstance):

c = p.pipenv("uninstall")
assert c.returncode != 0
assert "No package provided!" in str(c.exception)
assert "No package provided!" in c.stderr
8 changes: 4 additions & 4 deletions tests/unit/test_help.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pipenv.utils import subprocess_run
import subprocess
import sys

Expand All @@ -20,11 +21,10 @@ def test_help():
def test_count_of_description_pre_option():
test_command = 'pipenv install --help'
test_line = '--pre Allow pre-releases.'
out = subprocess.Popen(test_command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, _ = out.communicate()
lines = stdout.decode().split('\n')
c = subprocess_run(test_command, shell=True)
lines = c.stdout.splitlines()
count = 0
for line in lines:
if line.strip().split() == test_line.split():
count += 1
assert count == 1
assert count == 1, c.stdout

0 comments on commit d676346

Please sign in to comment.