llvm/lldb/packages/Python/lldbsuite/__init__.py
Jordan Rupprecht fcb0d8163a [lldb/test] Use realpath consistently for test root file paths.
LLDB tests assume that tests are in the test tree (the `LLDB_TEST_SRC` env variable, configured by `dotest.py`).
If this assertion doesn't hold, tests fail in strange ways. An early place this goes wrong is in `compute_mydir` which does a simple length-based substring to get the relative path. Later, we use that path to chdir to. If the test file and test tree don't agree in realpath-ness (and therefore length), this will be a cryptic error of chdir-ing to a directory that does not exist.

The actual discrepency is that the places we look for `use_lldb_suite.py` don't use a realpath, but `dotest.py` does (see initialization of `configuration.testdirs`).

It doesn't particularly matter whether we use realpath or abspath to canonicalize things, but many places end up with implicit dependencies on the canonicalized pwd being a realpath, so make them realpath consistently. Also, in the `compute_mydir` method mentioned, raise an error if the path types don't agree.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D85258
2020-08-05 11:35:37 -07:00

27 lines
832 B
Python

# Module level initialization for the `lldbsuite` module.
import inspect
import os
import sys
def find_lldb_root():
lldb_root = os.path.realpath(
os.path.dirname(inspect.getfile(inspect.currentframe())))
while True:
parent = os.path.dirname(lldb_root)
if parent == lldb_root: # dirname('/') == '/'
raise Exception("use_lldb_suite_root.py not found")
lldb_root = parent
test_path = os.path.join(lldb_root, "use_lldb_suite_root.py")
if os.path.isfile(test_path):
return lldb_root
# lldbsuite.lldb_root refers to the root of the git/svn source checkout
lldb_root = find_lldb_root()
# lldbsuite.lldb_test_src_root refers to the root of the python test case tree
# (i.e. the actual unit tests).
lldb_test_root = os.path.join(lldb_root, "test", "API")