perf/pixel-rate: new pixel throughput microbenchmark
[piglit.git] / self-tests / test-installed-piglit-script-imports-correct-framework-module
blobf0a19a40056fa82c5d31937db76d29f7f209e56b
1 #!/usr/bin/env python2
3 import argparse
4 import os
5 import shutil
6 import sys
7 import tempfile
9 from subprocess import check_call
10 from textwrap import dedent
12 CMAKE_INSTALL_LIBDIR_CHOICES = [None, 'lib', 'lib32', 'lib64']
13 PIGLIT_INSTALL_VERSION_CHOICES = [None, '1234']
15 DESCRIPTION = dedent("""\
16     Test that the main piglit script, when installed, successfully imports
17     the correct framework module.
19     Failure modes
20         1. The script fails to find any framework module and dies.
21         2. The script finds and imports a framework module that belongs to
22             a different Piglit.
24     This test installs and verifies the piglit script with all combinations
25     of the following CMake configuration variables:
27         CMAKE_INSTALL_LIBDIR in {CMAKE_INSTALL_LIBDIR_CHOICES}
28         PIGLIT_INSTALL_VERSION in {PIGLIT_INSTALL_VERSION_CHOICES}
30     This test must be run from a git work tree. Due to the warned behavior below,
31     the test will run only if given the '--force' option.
33     WARNING
34         - This test may remove any changes not committed to git, including ignored
35             and untracked files.
36     
37         - This test may make changes to files in the git work tree (but it won't
38             make any changes to the git directory itself).
39     
40         - This test will build and install Piglit multiple times to multiple
41             temporary directories named 'tmp/piglit.XXXXXX'.  This test will not
42             clean up its temporary directories. That's your responsibility.
43     """.format(
44         CMAKE_INSTALL_LIBDIR_CHOICES=CMAKE_INSTALL_LIBDIR_CHOICES,
45         PIGLIT_INSTALL_VERSION_CHOICES=PIGLIT_INSTALL_VERSION_CHOICES))
47 def parse_args():
48     parser = argparse.ArgumentParser()
49     parser.formatter_class = argparse.RawDescriptionHelpFormatter
50     parser.description = DESCRIPTION
51     parser.add_argument('-f', '--force',
52                         action='store_true',
53                         help='really run the test')
54     parser.add_argument('srcdir',
55                         help='top of piglit source tree')
56     return (parser.prog, parser.parse_args())
58 def main():
59     prog, args = parse_args()
60     if not args.force:
61         parser.error("skipping test because '--force' option is missing")
63     for piglit_install_version in PIGLIT_INSTALL_VERSION_CHOICES:
64         for libdir in CMAKE_INSTALL_LIBDIR_CHOICES:
65             test_once(args.srcdir, piglit_install_version, libdir)
67     print('{0}: all tests passed'.format(prog))
69 def shell(command, cwd=None):
70     check_call(command, shell=True, cwd=cwd)
72 def test_once(srcdir, piglit_install_version, libdir):
73     test_result = False
75     srcdir = os.path.abspath(srcdir)
76     build_dir = tempfile.mkdtemp(prefix='piglit-build.')
77     prefix = tempfile.mkdtemp(prefix='piglit-prefix.')
79     def print_test_info():
80         print('    srcdir: {0!r}'.format(srcdir))
81         print('    libdir: {0!r}'.format(libdir))
82         print('    piglit_install_version: {0!r}'.format(piglit_install_version))
83         print('    build_dir: {0!r}'.format(build_dir))
84         print('    prefix: {0!r}'.format(prefix))
86     def print_test_result():
87         if test_result:
88             print('test passed:')
89         else:
90             print('test failed:')
92         print_test_info()
94     print('test start:')
95     print_test_info()
97     try:
98         # Scrub the source tree.
99         shell('git reset --hard', cwd=srcdir)
100         shell('git clean -xfd', cwd=srcdir)
102         # Make it build fast. We want to test the Python, not the C.
103         shell("sed -i '/add_subdirectory/d' CMakeLists.txt", cwd=srcdir)
105         cmake_args = [
106             'cmake',
107             '-GNinja',
108             '-DCMAKE_INSTALL_PREFIX=' + prefix,
109         ]
111         if libdir is not None:
112             cmake_args.append('-DCMAKE_INSTALL_LIBDIR=' + libdir)
113         if piglit_install_version is not None:
114             cmake_args.append('-DPIGLIT_INSTALL_VERSION=' + piglit_install_version)
116         cmake_args.append(srcdir)
118         check_call(cmake_args, cwd=build_dir)
119         shell('ninja install', cwd=build_dir)
121         # To prevent the false positive case when the installed script
122         # accidentally imports the framework module located in git work tree,
123         # execute the installed script with cwd=srcdir and fill the framework
124         # module in srcdir with garbage. If the installed piglit script tries
125         # to import it, then script will raise a Python exception and fail.
126         shell("find framework -type f -exec echo GARBAGE '>' '{}' ';'", cwd=srcdir)
128         # Append install version, if any, onto script name.
129         script_name = 'piglit'
130         if piglit_install_version is not None:
131             script_name += '-' + piglit_install_version
133         # Check script works when called with explicit filepath.
134         bindir = os.path.join(prefix, 'bin')
135         script_path = os.path.join(bindir, script_name)
136         check_call([script_path, '--help'], cwd=srcdir)
138         # Check script works when called through a PATH search.
139         new_env = os.environ.copy()
140         new_env['PATH'] = '{0}:{1}'.format(bindir, new_env['PATH'])
141         check_call([script_name, '--help'], env=new_env, cwd=srcdir)
143         test_result = True
145     finally:
146         print_test_result()
147         shell('git reset --hard', cwd=srcdir)
148         shutil.rmtree(build_dir)
149         shutil.rmtree(prefix)
151 main()