GeezerVison is showing github.com — Show equivalent pytest command (#6363) · jackdcasey/vscode-python@796169c
File treeExpand file treeCollapse file tree Open diff view settings tests/testing_tools/adapter
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pythonFiles/testing_tools/adapter/pytest/_discovery.py +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,11 +28,15 @@ def discover(pytestargs=None, hidestdio=False, | 28 | 28 | # No tests were discovered. | 29 | 29 | pass | 30 | 30 | elif ec != 0: | | 31 | + print(('equivalent command: {} -m pytest {}' | | 32 | + ).format(sys.executable, util.shlex_unsplit(pytestargs))) | 31 | 33 | if hidestdio: | 32 | 34 | print(stdio.getvalue(), file=sys.stderr) | 33 | 35 | sys.stdout.flush() | 34 | 36 | raise Exception('pytest discovery failed (exit code {})'.format(ec)) | 35 | 37 | if not _plugin._started: | | 38 | + print(('equivalent command: {} -m pytest {}' | | 39 | + ).format(sys.executable, util.shlex_unsplit(pytestargs))) | 36 | 40 | if hidestdio: | 37 | 41 | print(stdio.getvalue(), file=sys.stderr) | 38 | 42 | sys.stdout.flush() | |
Collapse file Copy file name to clipboardExpand all lines: pythonFiles/testing_tools/adapter/util.py +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,3 +59,34 @@ def group_attr_names(attrnames): | 59 | 59 | group = 'other' | 60 | 60 | grouped[group].append(name) | 61 | 61 | return grouped | | 62 | + | | 63 | + | | 64 | +def shlex_unsplit(argv): | | 65 | + """Return the shell-safe string for the given arguments. | | 66 | +
| | 67 | + This effectively the equivalent of reversing shlex.split(). | | 68 | + """ | | 69 | + argv = [_quote_arg(a) for a in argv] | | 70 | + return ' '.join(argv) | | 71 | + | | 72 | + | | 73 | +try: | | 74 | + from shlex import quote as _quote_arg | | 75 | +except ImportError: | | 76 | + def _quote_arg(arg): | | 77 | + parts = None | | 78 | + for i, c in enumerate(arg): | | 79 | + if c.isspace(): | | 80 | + pass | | 81 | + elif c == '"': | | 82 | + pass | | 83 | + elif c == "'": | | 84 | + c = "'\"'\"'" | | 85 | + else: | | 86 | + continue | | 87 | + if parts is None: | | 88 | + parts = list(arg) | | 89 | + parts[i] = c | | 90 | + if parts is not None: | | 91 | + arg = "'" + ''.join(parts) + "'" | | 92 | + return arg |
Collapse file Copy file name to clipboard +59Lines changed: 59 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. | | 2 | +# Licensed under the MIT License. | | 3 | + | | 4 | +import shlex | | 5 | +import unittest | | 6 | + | | 7 | +from testing_tools.adapter.util import shlex_unsplit | | 8 | + | | 9 | + | | 10 | +class ShlexUnsplitTests(unittest.TestCase): | | 11 | + | | 12 | + def test_no_args(self): | | 13 | + argv = [] | | 14 | + joined = shlex_unsplit(argv) | | 15 | + | | 16 | + self.assertEqual(joined, '') | | 17 | + self.assertEqual(shlex.split(joined), argv) | | 18 | + | | 19 | + def test_one_arg(self): | | 20 | + argv = ['spam'] | | 21 | + joined = shlex_unsplit(argv) | | 22 | + | | 23 | + self.assertEqual(joined, 'spam') | | 24 | + self.assertEqual(shlex.split(joined), argv) | | 25 | + | | 26 | + def test_multiple_args(self): | | 27 | + argv = [ | | 28 | + '-x', 'X', | | 29 | + '-xyz', | | 30 | + 'spam', | | 31 | + 'eggs', | | 32 | + ] | | 33 | + joined = shlex_unsplit(argv) | | 34 | + | | 35 | + self.assertEqual(joined, '-x X -xyz spam eggs') | | 36 | + self.assertEqual(shlex.split(joined), argv) | | 37 | + | | 38 | + def test_whitespace(self): | | 39 | + argv = [ | | 40 | + '-x', 'X Y Z', | | 41 | + 'spam spam\tspam', | | 42 | + 'eggs', | | 43 | + ] | | 44 | + joined = shlex_unsplit(argv) | | 45 | + | | 46 | + self.assertEqual(joined, "-x 'X Y Z' 'spam spam\tspam' eggs") | | 47 | + self.assertEqual(shlex.split(joined), argv) | | 48 | + | | 49 | + def test_quotation_marks(self): | | 50 | + argv = [ | | 51 | + '-x', "'<quoted>'", | | 52 | + 'spam"spam"spam', | | 53 | + "ham'ham'ham", | | 54 | + 'eggs', | | 55 | + ] | | 56 | + joined = shlex_unsplit(argv) | | 57 | + | | 58 | + self.assertEqual(joined, "-x ''\"'\"'<quoted>'\"'\"'' 'spam\"spam\"spam' 'ham'\"'\"'ham'\"'\"'ham' eggs") | | 59 | + self.assertEqual(shlex.split(joined), argv) |
|