Skip to content

Commit

Permalink
Add ability to show a list of tags
Browse files Browse the repository at this point in the history
Usage:

```sh
./test-run.py --tags
./test-run.py app-tap/ --tags
```

Follows up #22
  • Loading branch information
Totktonada committed Oct 5, 2021
1 parent 58a2590 commit e030ba2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -365,6 +365,13 @@ Usage:
test-run will run only those tests, which have at least one of the
provided tags.

Show a list of tags:

```sh
./test-run.py --tags
./test-run.py app-tap/ --tags
```

The tags metainfo should be placed within a first comment of a test
file.

Expand Down
21 changes: 21 additions & 0 deletions lib/options.py
Expand Up @@ -33,6 +33,9 @@ class Options(object):
_instance = None
_initialized = False

# Just some unique marker.
_show_tags = {}

def __new__(cls, *args, **kwargs):
"""Make the class singleton."""
if cls._instance:
Expand Down Expand Up @@ -278,19 +281,37 @@ def __init__(self):
parser.add_argument(
'--tags',
dest='tags',
const=self._show_tags,
nargs='?',
default=None,
type=split_list,
help="""Comma separated list of tags.
If tags are provided, test-run will run only those
tests, which are marked with ANY of the provided
tags.
If the option is given without a parameter (at the
last position), test-run will show a list of tags
and stop.
""")

# XXX: We can use parser.parse_intermixed_args() on
# Python 3.7 to understand commands like
# ./test-run.py foo --exclude bar baz
self.args = parser.parse_args()

# If `--tags foo,bar` is passed, just keep the list in
# `args.tags`.
#
# If `--tags` is passed without a parameter, clean up
# `args.tags` and toggle `args.show_tags`.
if self.args.tags == self._show_tags:
self.args.tags = None
self.args.show_tags = True
else:
self.args.show_tags = False

self.check()

Options._initialized = True
Expand Down
3 changes: 2 additions & 1 deletion lib/test_suite.py
Expand Up @@ -164,7 +164,8 @@ def collect_tests(self):
else:
raise ValueError('Cannot collect tests of unknown type')

if not Options().args.reproduce:
# In given cases, this large output looks redundant.
if not Options().args.reproduce and not Options().args.show_tags:
color_stdout("Collecting tests in ", schema='ts_text')
color_stdout(
'%s (Found %s tests)' % (
Expand Down
18 changes: 18 additions & 0 deletions test-run.py
Expand Up @@ -55,6 +55,7 @@

from lib import Options
from lib.colorer import color_stdout
from lib.utils import find_tags
from lib.utils import print_tail_n
from lib.utils import PY3
from lib.worker import get_task_groups
Expand Down Expand Up @@ -221,6 +222,19 @@ def main_consistent():
return (-1 if failed_test_ids else 0)


def show_tags():
# Collect tests in the same way as when we run them.
collected_tags = set()
for name, task_group in get_task_groups().items():
for task_id in task_group['task_ids']:
test_name, _ = task_id
for tag in find_tags(test_name):
collected_tags.add(tag)

for tag in sorted(collected_tags):
color_stdout(tag + '\n')


if __name__ == "__main__":
# In Python 3 start method 'spawn' in multiprocessing module becomes
# default on Mac OS.
Expand Down Expand Up @@ -275,6 +289,10 @@ def open_as_utf8(*args, **kwargs):

status = 0

if Options().args.show_tags:
show_tags()
exit(status)

force_parallel = bool(Options().args.reproduce)
if not force_parallel and Options().args.jobs == -1:
status = main_consistent()
Expand Down

0 comments on commit e030ba2

Please sign in to comment.