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