1# encoding=utf-8
2# Copyright © 2017 Intel Corporation
3
4# Permission is hereby granted, free of charge, to any person obtaining a copy
5# of this software and associated documentation files (the "Software"), to deal
6# in the Software without restriction, including without limitation the rights
7# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8# copies of the Software, and to permit persons to whom the Software is
9# furnished to do so, subject to the following conditions:
10
11# The above copyright notice and this permission notice shall be included in
12# all copies or substantial portions of the Software.
13
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20# SOFTWARE.
21
22import argparse
23import errno
24import os
25import subprocess
26import sys
27
28# The meson version handles windows paths better, but if it's not available
29# fall back to shlex
30try:
31    from meson.mesonlib import split_args
32except ImportError:
33    from shlex import split as split_args
34
35
36def arg_parser():
37    parser = argparse.ArgumentParser()
38    parser.add_argument(
39        '--glsl-compiler',
40        required=True,
41        help='Path to the standalone glsl compiler')
42    parser.add_argument(
43        '--test-directory',
44        required=True,
45        help='Directory containing tests to run.')
46    return parser.parse_args()
47
48
49def get_test_runner(runner):
50    """Wrap the test runner in the exe wrapper if necessary."""
51    wrapper = os.environ.get('MESON_EXE_WRAPPER', None)
52    if wrapper is None:
53        return [runner]
54    return split_args(wrapper) + [runner]
55
56
57def main():
58    args = arg_parser()
59    files = [f for f in os.listdir(args.test_directory) if f.endswith('.vert')]
60    passed = 0
61
62    if not files:
63        print('Could not find any tests')
64        exit(1)
65
66    runner = get_test_runner(args.glsl_compiler)
67
68    print('====== Testing compilation output ======')
69    for file in files:
70        print('Testing {} ...'.format(file), end='')
71        file = os.path.join(args.test_directory, file)
72
73        with open('{}.expected'.format(file), 'rb') as f:
74            expected = f.read().splitlines()
75
76        proc= subprocess.run(
77            runner + ['--just-log', '--version', '150', file],
78            stdout=subprocess.PIPE
79        )
80        if proc.returncode == 255:
81            print("Test returned general error, possibly missing linker")
82            sys.exit(77)
83        elif proc.returncode != 0:
84            print("Test returned error: {}, output:\n{}\n".format(proc.returncode, proc.stdout))
85
86        actual = proc.stdout.splitlines()
87
88        if actual == expected:
89            print('PASS')
90            passed += 1
91        else:
92            print('FAIL')
93
94    print('{}/{} tests returned correct results'.format(passed, len(files)))
95    exit(0 if passed == len(files) else 1)
96
97
98if __name__ == '__main__':
99    try:
100        main()
101    except OSError as e:
102        if e.errno == errno.ENOEXEC:
103            print('Skipping due to inability to run host binaries', file=sys.stderr)
104            sys.exit(77)
105        raise
106