forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_test.py
107 lines (88 loc) · 3.63 KB
/
install_test.py
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
import argparse
import functools
import os
import re
import subprocess
import sys
import unittest
from python import runfiles
import install_test_helper
class InstallTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Install into bazel read-only temporary directory. We expect this
# method to be called exactly once, so we assert that the install
# directory must not exist beforehand, but must exist afterward.
cls._installation_folder = install_test_helper.get_install_dir()
assert not os.path.exists(cls._installation_folder)
install_test_helper.install()
assert os.path.isdir(cls._installation_folder)
def test_basic_paths(self):
# Verify install directory content.
content = set(os.listdir(self._installation_folder))
self.assertSetEqual(set(['bin', 'include', 'lib', 'share']), content)
def _run_one_command(self, test_command):
# On macOS, we must run the install tests against venv's interpreter,
# so that necessary libraries are available. On Ubuntu, the libraries
# are installed system-wide (without a venv).
if sys.platform == "darwin":
manifest = runfiles.Create()
python = manifest.Rlocation("python/bin/python3")
else:
python = install_test_helper.get_python_executable()
# Our launched processes should be independent, not inherit their
# runfiles from the install_test.py runner.
env = dict(os.environ)
for key in ["RUNFILES_MANIFEST_FILE", "RUNFILES_DIR", "TEST_SRCDIR"]:
if key in env:
del env[key]
# Execute the test_command.
print("+ {}".format(test_command), file=sys.stderr)
assert test_command.endswith(".py")
subprocess.check_call(
[python, os.path.join(os.getcwd(), test_command)],
env=env)
def _convert_test_command_to_test_case_name(test_command):
program = test_command.split()[0]
basename = os.path.basename(program)
bare_name = os.path.splitext(basename)[0]
identifier = re.sub("[^0-9a-zA-Z]+", "_", bare_name)
if identifier.startswith("test_"):
test_case_name = identifier
else:
test_case_name = "test_" + identifier
return test_case_name
def main():
# Locate the command-line argument that provides the list of test commands.
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--install_tests_filename', required=True)
args, unparsed = parser.parse_known_args()
new_argv = ["install_test"] + unparsed
# Read the list of tests.
with open(args.install_tests_filename, 'r') as f:
lines = f.readlines()
# Add them as individual tests.
test_case_names = []
for one_line in lines:
test_command = one_line.strip()
test_case_name = _convert_test_command_to_test_case_name(test_command)
setattr(InstallTest, test_case_name, functools.partialmethod(
InstallTest._run_one_command, test_command=test_command))
test_case_names.append(test_case_name)
# Give some Drake-specific help, if requested.
if "--help" in new_argv:
try:
unittest.main(argv=new_argv)
except: # noqa
print("To run just one test case, use:")
print(" bazel test //:py/install_test "
"--test_arg=InstallTest.test_foo")
print()
print("Tests:")
for name in test_case_names:
print(f" InstallTest.{name}")
return 1
# Delegate to unittest.
unittest.main(argv=new_argv)
if __name__ == '__main__':
main()