File: run-unit-tests.py

package info (click to toggle)
systemd-udeb 259-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 104,120 kB
  • sloc: ansic: 726,480; xml: 121,118; python: 35,852; sh: 33,447; cpp: 946; awk: 102; makefile: 89; lisp: 13; sed: 1
file content (89 lines) | stat: -rwxr-xr-x 2,734 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later

import argparse
import os
import pathlib
import subprocess
import sys
try:
    import colorama as c
    GREEN = c.Fore.GREEN
    YELLOW = c.Fore.YELLOW
    RED = c.Fore.RED
    RESET_ALL = c.Style.RESET_ALL
    BRIGHT = c.Style.BRIGHT
except ImportError:
    GREEN = YELLOW = RED = RESET_ALL = BRIGHT = ''

class total:
    total = None
    good = 0
    skip = 0
    fail = 0

def argument_parser():
    p = argparse.ArgumentParser()
    p.add_argument('-u', '--unsafe', action='store_true',
                   help='run "unsafe" tests too')
    p.add_argument('-A', '--artifact_directory',
                   help='store output from failed tests in this dir')
    p.add_argument('-s', '--skip', action='append', default=[],
                   help='skip the named test')

    return p

opts = argument_parser().parse_args()

unittestdir = pathlib.Path(__file__).parent.absolute() / 'unit-tests'

tests = list(unittestdir.glob('test-*'))
if opts.unsafe:
    tests += unittestdir.glob('unsafe/test-*')

if not opts.artifact_directory and os.getenv('ARTIFACT_DIRECTORY'):
    opts.artifact_directory = os.getenv('ARTIFACT_DIRECTORY')

total.total = len(tests)
for test in sorted(tests):
    name = os.path.basename(test)

    if name in opts.skip:
        print(f'{YELLOW}SKIP: {name} (by user) {RESET_ALL}')
        total.skip += 1
        continue

    ex = subprocess.run(test, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    if ex.returncode == 0:
        print(f'{GREEN}PASS: {name}{RESET_ALL}')
        total.good += 1
    elif ex.returncode == 77:
        print(f'{YELLOW}SKIP: {name}{RESET_ALL}')
        total.skip += 1
    elif ex.returncode == 127:
        print(f'{YELLOW}SKIP: {name} (no interpreter) {RESET_ALL}')
        total.skip += 1
    else:
        print(f'{RED}FAIL: {name}{RESET_ALL}')
        total.fail += 1

        output_file = None
        if opts.artifact_directory:
            output_dir = pathlib.Path(opts.artifact_directory) / 'unit-tests'
            output_dir.mkdir(parents=True, exist_ok=True)
            output_file = output_dir / name
            output_file.write_bytes(ex.stdout)

        try:
            print(ex.stdout.decode('utf-8'))
        except UnicodeDecodeError:
            print(f'{BRIGHT}Note, some test output shown here is not UTF-8')
            if output_file:
                print(f'For actual test output see artifact file {output_file}')
            print(f'{RESET_ALL}')
            print(ex.stdout.decode('utf-8', errors='replace'))
    sys.stdout.flush()


print(f'{BRIGHT}OK: {total.good} SKIP: {total.skip} FAIL: {total.fail}{RESET_ALL}')
sys.exit(total.fail > 0)