import os
import shlex
import sys
import lit.formats
import lit.Test
import lit.util
kIsWindows = sys.platform in ["win32", "cygwin"]
class LibcTest(lit.formats.ExecutableTest):
def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig):
source_path = testSuite.getSourcePath(path_in_suite)
if not os.path.isdir(source_path):
return
for filename in sorted(os.listdir(source_path)):
filepath = os.path.join(source_path, filename)
if self._isTestExecutable(filename, filepath, localConfig):
yield lit.Test.Test(testSuite, path_in_suite + (filename,), localConfig)
def _isTestExecutable(self, filename, filepath, localConfig):
test_name = filename
if kIsWindows and filename.endswith(".exe"):
test_name = filename[: -len(".exe")]
if not test_name.endswith(".__build__"):
return False
if test_name.startswith("libc.test.src."):
pass elif test_name.startswith("libc.test.include."):
if ".__unit__." not in test_name and ".__hermetic__." not in test_name:
return False
elif test_name.startswith("libc.test.integration."):
pass elif test_name.startswith("libc.test.shared."):
pass elif test_name.startswith("libc.test.utils."):
pass else:
return False
if not os.path.isfile(filepath):
return False
if (
not kIsWindows
and not os.access(filepath, os.X_OK)
and not getattr(localConfig, "libc_crosscompiling_emulator", None)
):
return False
return True
def _getParamsPath(self, test_path):
params_path = test_path + ".params"
if os.path.isfile(params_path):
return params_path
root, ext = os.path.splitext(test_path)
if ext.lower() == ".exe":
params_path = root + ".params"
if os.path.isfile(params_path):
return params_path
return None
def execute(self, test, litConfig):
test_path = test.getSourcePath()
exec_dir = os.path.dirname(test_path)
loader_args = []
test_args = []
extra_env = {}
params_path = self._getParamsPath(test_path)
if params_path:
with open(params_path) as f:
content = f.read()
sections = content.split("---\n")
if len(sections) >= 3:
loader_args = [l for l in sections[0].splitlines() if l]
test_args = [l for l in sections[1].splitlines() if l]
env_section = sections[2]
else:
loader_args = []
test_args = [l for l in sections[0].splitlines() if l]
env_section = sections[1] if len(sections) > 1 else ""
for line in env_section.splitlines():
if "=" in line:
k, _, v = line.partition("=")
extra_env[k] = v
env = dict(os.environ)
env["PWD"] = exec_dir
env.update(extra_env)
timeout = litConfig.maxIndividualTestTime
test_cmd_template = getattr(test.config, "libc_test_cmd", "")
if test_cmd_template:
if "@BINARY@" in test_cmd_template:
prefix, _, suffix = test_cmd_template.partition("@BINARY@")
cmd_args = (
shlex.split(prefix)
+ loader_args
+ [test_path]
+ shlex.split(suffix)
+ test_args
)
else:
cmd_args = (
shlex.split(test_cmd_template)
+ loader_args
+ [test_path]
+ test_args
)
if not cmd_args:
cmd_args = [test_path]
else:
cmd_args = [test_path] + test_args
try:
out, err, exit_code = lit.util.executeCommand(
cmd_args, cwd=exec_dir, env=env, timeout=timeout
)
except lit.util.ExecuteCommandTimeoutException as e:
return (
lit.Test.TIMEOUT,
f"{e.out}\n--\n" f"Reached timeout of {timeout} seconds",
)
if not exit_code:
return lit.Test.PASS, ""
return lit.Test.FAIL, out + err