Skip to content

Commit

Permalink
Python code fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Mar 19, 2019
1 parent a994e3e commit 6ea447a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
25 changes: 12 additions & 13 deletions pythonFiles/testing_tools/adapter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,31 @@ def parse_args(
# Add "run" and "debug" subcommands when ready.
for cmdname in ['discover']:
sub = cmdsubs.add_parser(cmdname)
if cmdname == 'discover':
sub.add_argument('--simple', action='store_true')
sub.add_argument('--show-pytest', action='store_true')
subsubs = sub.add_subparsers(dest='tool')
for toolname in sorted(TOOLS):
try:
add_subparser = TOOLS[toolname]['_add_subparser']
except KeyError:
continue
add_subparser(cmdname, toolname, subsubs)
subsub = add_subparser(cmdname, toolname, subsubs)
if cmdname == 'discover':
subsub.add_argument('--simple', action='store_true')
subsub.add_argument('--show-' + toolname, action='store_true')
subsub.add_argument('--pretty', action='store_true')

# Parse the args!
args, toolargs = parser.parse_known_args(argv)
if '--' in argv:
seppos = argv.index('--')
toolargs = argv[seppos + 1:]
argv = argv[:seppos]
else:
toolargs = []
args = parser.parse_args(argv)
ns = vars(args)

cmd = ns.pop('cmd')
if not cmd:
parser.error('missing command')
if cmd == 'discover':
if '--simple' in toolargs:
toolargs.remove('--simple')
ns['simple'] = True
if '--show-pytest' in toolargs:
toolargs.remove('--show-pytest')
ns['show_pytest'] = True

tool = ns.pop('tool')
if not tool:
Expand All @@ -88,7 +88,6 @@ def main(toolname, cmdname, subargs, toolargs,

parents, result = run(toolargs, **subargs)
report_result(result, parents,
debug=('-v' in toolargs or '--verbose' in toolargs),
**subargs
)

Expand Down
10 changes: 6 additions & 4 deletions pythonFiles/testing_tools/adapter/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest

from . import util
from .errors import UnsupportedCommandError
from .info import TestInfo, TestPath, ParentInfo

Expand All @@ -23,13 +24,16 @@ def add_cli_subparser(cmd, name, parent):


def discover(pytestargs=None, show_pytest=False,
_pytest_main=pytest.main, _plugin=None, **kwargs):
_pytest_main=pytest.main, _plugin=None, **_ignored):
"""Return the results of test discovery."""
if _plugin is None:
_plugin = TestCollector()

pytestargs = _adjust_pytest_args(pytestargs, show_pytest=show_pytest)
ec = _pytest_main(pytestargs, [_plugin])
# We use this helper rather than "-pno:terminal" due to possible
# platform-dependent issues.
with util.hide_stdio() if not show_pytest else util.noop_cm():
ec = _pytest_main(pytestargs, [_plugin])
if ec != 0:
raise Exception('pytest discovery failed (exit code {})'.format(ec))
if not _plugin._started:
Expand All @@ -49,8 +53,6 @@ def _adjust_pytest_args(pytestargs, show_pytest):
pytestargs = list(pytestargs) if pytestargs else []
# Duplicate entries should be okay.
pytestargs.insert(0, '--collect-only')
if not show_pytest:
pytestargs.insert(0, '-pno:terminal')
# TODO: pull in code from:
# src/client/unittests/pytest/services/discoveryService.ts
# src/client/unittests/pytest/services/argsService.ts
Expand Down
6 changes: 3 additions & 3 deletions pythonFiles/testing_tools/adapter/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import json


def report_discovered(tests, parents, debug=False, simple=False,
_send=print, **kwargs):
def report_discovered(tests, parents, pretty=False, simple=False,
_send=print, **_ignored):
"""Serialize the discovered tests and write to stdout."""
if simple:
data = [{
Expand Down Expand Up @@ -62,7 +62,7 @@ def report_discovered(tests, parents, debug=False, simple=False,
} for root in sorted(byroot)]

kwargs = {}
if debug:
if pretty:
# human-formatted
kwargs = dict(
sort_keys=True,
Expand Down
27 changes: 27 additions & 0 deletions pythonFiles/testing_tools/adapter/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import contextlib
try:
from io import StringIO
except ImportError:
from StringIO import StringIO # 2.7
import sys


@contextlib.contextmanager
def noop_cm():
yield


@contextlib.contextmanager
def hide_stdio():
"""Swallow stdout and stderr."""
buf = StringIO()
sys.stdout = buf
sys.stderr = buf
try:
yield buf
finally:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__

0 comments on commit 6ea447a

Please sign in to comment.