Skip to content

Commit

Permalink
more test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbernat committed Jan 21, 2019
1 parent f9391fc commit bbc2c11
Show file tree
Hide file tree
Showing 18 changed files with 307 additions and 230 deletions.
45 changes: 30 additions & 15 deletions src/tox/_pytestplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def cmd(request, monkeypatch):
request.addfinalizer(py.path.local().chdir)

def run(*argv):
reset_report()
key = str("PYTHONPATH")
python_paths = (i for i in (os.getcwd(), os.getenv(key)) if i)
monkeypatch.setenv(key, os.pathsep.join(python_paths))
Expand Down Expand Up @@ -107,15 +108,17 @@ def __init__(self, args):

def __enter__(self):
self._start = time.time()
self._out_tell = sys.stdout.tell()
self._err_tell = sys.stderr.tell()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.duration = time.time() - self._start
self.out = self._read(sys.stdout)
self.err = self._read(sys.stderr)
self.out = self._read(sys.stdout, self._out_tell)
self.err = self._read(sys.stderr, self._err_tell)

def _read(self, out):
out.buffer.seek(0)
def _read(self, out, pos):
out.buffer.seek(pos)
return out.buffer.read().decode(out.encoding, errors=out.errors)

@property
Expand All @@ -138,7 +141,10 @@ def __init__(self):

def clear(self):
self._index = -1
self.instance.reported_lines.clear()
if six.PY3:
self.instance.reported_lines.clear()
else:
del self.instance.reported_lines[:]

def getnext(self, cat):
__tracebackhide__ = True
Expand Down Expand Up @@ -208,13 +214,16 @@ def create_mocksession(request):

class MockSession(Session):
def __init__(self, config):
update_default_reporter(config.option.quiet_level, config.option.verbose_level)
self.logging_levels(config.option.quiet_level, config.option.verbose_level)
super(MockSession, self).__init__(config, popen=self.popen)
self._pcalls = []
self.report = ReportExpectMock()

def _clearmocks(self):
self._pcalls.clear()
if six.PY3:
self._pcalls.clear()
else:
del self._pcalls[:]
self.report.clear()

def popen(self, args, cwd, shell=None, stdout=None, stderr=None, env=None, **_):
Expand All @@ -223,11 +232,17 @@ def popen(self, args, cwd, shell=None, stdout=None, stderr=None, env=None, **_):
return process_call_mock

def new_config(self, config):
update_default_reporter(config.option.quiet_level, config.option.verbose_level)
self.logging_levels(config.option.quiet_level, config.option.verbose_level)
self.config = config
self.venv_dict.clear()
self.existing_venvs.clear()

def logging_levels(self, quiet, verbose):
update_default_reporter(quiet, verbose)
if hasattr(self, "config"):
self.config.option.quiet_level = quiet
self.config.option.verbose_level = verbose

return MockSession(config)


Expand Down Expand Up @@ -494,18 +509,18 @@ def current_tox_py():


def pytest_runtest_setup(item):
from tox.reporter import _INSTANCE

_INSTANCE._reset()
reset_report()


def pytest_runtest_teardown(item):
from tox.reporter import _INSTANCE

_INSTANCE._reset()
reset_report()


def pytest_pyfunc_call(pyfuncitem):
reset_report()


def reset_report(quiet=0, verbose=0):
from tox.reporter import _INSTANCE

_INSTANCE._reset()
_INSTANCE._reset(quiet_level=quiet, verbose_level=verbose)
16 changes: 9 additions & 7 deletions src/tox/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ def popen(
stream_getter = self._get_standard_streams(
capture_err, cmd_args_shell, redirect, returnout
)
cwd = os.getcwd() if cwd is None else cwd
with stream_getter as (fin, out_path, stderr, stdout):
try:
args = self._rewrite_args(cwd, args)
process = self.via_popen(
self._rewrite_args(cwd, args),
args,
stdout=stdout,
stderr=stderr,
cwd=cwd,
cwd=str(cwd),
env=os.environ.copy() if env is None else env,
universal_newlines=True,
shell=False,
Expand All @@ -88,20 +90,20 @@ def popen(
output = self.feed_stdin(fin, process, redirect)
exit_code = process.wait()
if exit_code and not ignore_ret:
invoked = " ".join(map(str, process.args))
invoked = " ".join(map(str, args))
if out_path:
reporter.error(
"invocation failed (exit code {:d}), logfile: {}".format(exit_code, out_path)
)
output = out_path.read()
reporter.error(output)
self.command_log.add_command(process.args, output, exit_code)
self.command_log.add_command(args, output, exit_code)
raise InvocationError(invoked, exit_code, out_path)
else:
raise InvocationError(invoked, exit_code)
if not output and out_path:
output = out_path.read()
self.command_log.add_command(process.args, output, exit_code)
self.command_log.add_command(args, output, exit_code)
return output

def feed_stdin(self, fin, process, redirect):
Expand Down Expand Up @@ -149,14 +151,14 @@ def _get_standard_streams(self, capture_err, cmd_args_shell, redirect, returnout
stderr = subprocess.STDOUT if capture_err else None
stdout_file = None
if self.generate_tox_log or redirect:
stdout_file = open(self.get_log_path(self.name), "wt")
out_path = self.get_log_path(self.name)
stdout_file = out_path.open("wt")
stdout_file.write(
"actionid: {}\nmsg: {}\ncmdargs: {!r}\n\n".format(
self.name, self.msg, cmd_args_shell
)
)
stdout_file.flush()
out_path = py.path.local(stdout_file.name)
fin = out_path.open("rb")
fin.read() # read the header, so it won't be written to stdout
stdout = stdout_file
Expand Down
10 changes: 10 additions & 0 deletions src/tox/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
They live in the tox namespace and can be accessed as tox.[NAMESPACE.]NAME
"""
import os
import re
import sys

_THIS_FILE = os.path.realpath(os.path.abspath(__file__))


def _construct_default_factors(cpython_versions, pypy_versions, other_interpreters):
default_factors = {"py": sys.executable, "py2": "python2", "py3": "python3"}
Expand Down Expand Up @@ -75,3 +78,10 @@ class PIP:
]
INSTALL_SHORT_OPTIONS_ARGUMENT = ["-{}".format(option) for option in SHORT_OPTIONS]
INSTALL_LONG_OPTIONS_ARGUMENT = ["--{}".format(option) for option in LONG_OPTIONS]


_HELP_DIR = os.path.join(os.path.dirname(_THIS_FILE), "helper")
VERSION_QUERY_SCRIPT = os.path.join(_HELP_DIR, "get_version.py")
SITE_PACKAGE_QUERY_SCRIPT = os.path.join(_HELP_DIR, "get_site_package_dir.py")
BUILD_REQUIRE_SCRIPT = os.path.join(_HELP_DIR, "build_requires.py")
BUILD_ISOLATED = os.path.join(_HELP_DIR, "build_isolated.py")
Empty file added src/tox/helper/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions src/tox/helper/build_isolated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import sys

backend_spec = sys.argv[1]
backend_obj = sys.argv[2]


backend = __import__(backend_spec, fromlist=[None])
if backend_obj:
backend = getattr(backend, backend_obj)

dist_folder = sys.argv[3]

basename = backend.build_sdist(dist_folder, {"--global-option": ["--formats=gztar"]})
print(basename)
13 changes: 13 additions & 0 deletions src/tox/helper/build_requires.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import json
import sys

backend_spec = sys.argv[1]
backend_obj = sys.argv[2]
backend = __import__(backend_spec, fromlist=[None])
if backend_obj:
backend = getattr(backend, backend_obj)


for_build_requires = backend.get_requires_for_build_sdist(None)
output = json.dumps(for_build_requires)
print(output)
8 changes: 8 additions & 0 deletions src/tox/helper/get_site_package_dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import unicode_literals

import distutils.sysconfig
import json
import sys

data = json.dumps({"dir": distutils.sysconfig.get_python_lib(prefix=sys.argv[1])})
print(data)
13 changes: 13 additions & 0 deletions src/tox/helper/get_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from __future__ import unicode_literals

import json
import sys

info = {
"executable": sys.executable,
"version_info": list(sys.version_info),
"version": sys.version,
"sysplatform": sys.platform,
}
info_as_dump = json.dumps(info)
print(info_as_dump)
28 changes: 10 additions & 18 deletions src/tox/interpreters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

import distutils.util
import json
import re
Expand All @@ -7,6 +9,7 @@
import py

import tox
from tox.constants import SITE_PACKAGE_QUERY_SCRIPT, VERSION_QUERY_SCRIPT


class Interpreters:
Expand Down Expand Up @@ -45,13 +48,7 @@ def get_sitepackagesdir(self, info, envdir):
return ""
envdir = str(envdir)
try:
code = (
"import distutils.sysconfig; import json;"
"print(json.dumps("
"{{ 'dir': distutils.sysconfig.get_python_lib(prefix={!r})}}"
"))"
)
res = exec_on_interpreter(str(info.executable), "-c", code.format(envdir))
res = exec_on_interpreter(str(info.executable), SITE_PACKAGE_QUERY_SCRIPT, str(envdir))
except ExecFailed as e:
print("execution failed: {} -- {}".format(e.out, e.err))
return ""
Expand All @@ -62,18 +59,13 @@ def get_sitepackagesdir(self, info, envdir):
def run_and_get_interpreter_info(name, executable):
assert executable
try:
result = exec_on_interpreter(
str(executable),
"-c",
"import sys; import json;"
'print(json.dumps({"version_info": tuple(sys.version_info),'
' "sysplatform": sys.platform}))',
)
result = exec_on_interpreter(str(executable), VERSION_QUERY_SCRIPT)
result["version_info"] = tuple(result["version_info"]) # fix json dump transformation
del result["version"]
except ExecFailed as e:
return NoInterpreterInfo(name, executable=e.executable, out=e.out, err=e.err)
else:
return InterpreterInfo(name, executable, **result)
return InterpreterInfo(name, **result)


def exec_on_interpreter(*args):
Expand Down Expand Up @@ -168,12 +160,12 @@ def tox_get_python_executable(envconfig):

def locate_via_py(*parts):
ver = "-{}".format(".".join(parts))
script = "import sys; print(sys.executable)"
py_exe = distutils.spawn.find_executable("py")
if py_exe:
proc = subprocess.Popen(
(py_exe, ver, "-c", script), stdout=subprocess.PIPE, stderr=subprocess.PIPE
(py_exe, ver, VERSION_QUERY_SCRIPT), stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
out, _ = proc.communicate()
result = json.loads(out)
if not proc.returncode:
return out.decode("UTF-8").strip()
return result["executable"]
15 changes: 5 additions & 10 deletions src/tox/logs/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import json
import subprocess

from tox.constants import VERSION_QUERY_SCRIPT

from .command import CommandLog


Expand All @@ -15,17 +17,10 @@ def __init__(self, result_log, name, dict):
self.dict = dict

def set_python_info(self, python_executable):
cmd = [
str(python_executable),
"-c",
"import sys; import json;"
"print(json.dumps({"
"'executable': sys.executable,"
"'version_info': list(sys.version_info),"
"'version': sys.version}))",
]
cmd = [str(python_executable), VERSION_QUERY_SCRIPT]
result = subprocess.check_output(cmd, universal_newlines=True)
self.dict["python"] = json.loads(result)
answer = json.loads(result)
self.dict["python"] = answer

def get_commandlog(self, name):
"""get the command log for a given group name"""
Expand Down
Loading

0 comments on commit bbc2c11

Please sign in to comment.