File: run-tests.py

package info (click to toggle)
nova 2%3A32.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,500 kB
  • sloc: python: 418,899; pascal: 1,848; sh: 991; makefile: 163; xml: 83
file content (107 lines) | stat: -rwxr-xr-x 3,078 bytes parent folder | download | duplicates (3)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
#
# Copyright (c) 2019 SUSE
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.  See the License for the specific language governing
# permissions and limitations under the License.


# Filters list of files on STDIN for test files (can be more than one
# per line), and runs any which can be run in the current tox
# virtualenv.  Automatically detects which stestr options to use,
# e.g. --test-path=./nova/tests/functional if we're in a virtualenv
# for functional tests.

import os
import re
import subprocess
import sys


def get_tox_env():
    if os.getenv('VIRTUAL_ENV', '').find('/.tox/') == -1:
        sys.stderr.write(
            """%s should be run within a tox virtualenv.
Please first activate the tox virtualenv you want to use, e.g.

    source .tox/py36/bin/activate
""" %
            os.path.realpath(__file__))
        sys.exit(1)

    return os.getenv('VIRTUAL_ENV')


def tox_env_is_functional():
    return get_tox_env().find('.tox/functional') != -1


def get_stestr_opts():
    opts = sys.argv[1:]

    if tox_env_is_functional():
        opts = ['--test-path=./nova/tests/functional'] + opts

    return opts


def get_test_files():
    test_files = []
    functional = tox_env_is_functional()

    for line in sys.stdin:
        files = line.strip().split()
        for f in files:
            if not re.match(r'^nova/tests/.*\.py$', f):
                # In the future we could get really clever and
                # map source files to their corresponding tests,
                # as is typically done by Guardfile in projects
                # which use Guard: https://guardgem.org
                continue

            functional_re = r'^nova/tests/functional/'
            if functional:
                if not re.match(functional_re, f):
                    continue
            else:
                if re.match(functional_re, f):
                    continue

            test_files.append(f[:-3].replace('/', '.'))

    return test_files


def main():
    stestr_opts = get_stestr_opts()
    test_files = get_test_files()

    if not test_files:
        print("No test files found to run")
        sys.exit(0)

    if len(test_files) == 1:
        # If there's only one module to run (or test therein), we can
        # skip discovery which will chop quite a few seconds off the
        # runtime.
        stestr_opts = ['-n'] + stestr_opts

    cmd = ['stestr', 'run', *stestr_opts] + test_files
    print(' '.join(cmd))
    try:
        subprocess.check_call(cmd)
    except subprocess.CalledProcessError as e:
        print("\nstestr returned non-zero exit code %d\n" % e.returncode)


if __name__ == '__main__':
    main()