Files
clang-p2996/clang/test/bindings/python/tests/cindex/test_file.py
Rainer Orth 90c9cc2c98 [clang][python][test] Move python binding tests to lit framework (#145855)
As discussed in PR #142353, the current testsuite of the `clang` Python
bindings has several issues:

- If `libclang.so` cannot be loaded into `python` to run the testsuite,
the whole `ninja check-all` aborts.
- The result of running the testsuite isn't report like the `lit`-based
tests, rendering them almost invisible.
- The testsuite is disabled in a non-obvious way (`RUN_PYTHON_TESTS`) in
`tests/CMakeLists.txt`, which again doesn't show up in the test results.

All these issues can be avoided by integrating the Python bindings tests
with `lit`, which is what this patch does:

- The actual test lives in `clang/test/bindings/python/bindings.sh` and
is run by `lit`.
- The current `clang/bindings/python/tests` directory (minus the
now-superfluous `CMakeLists.txt`) is moved into the same directory.
- The check if `libclang` is loadable (originally from PR #142353) is
now handled via a new `lit` feature, `libclang-loadable`.
- The various ways to disable the tests have been turned into `XFAIL`s
as appropriate.
- AArch64 doesn't `FAIL` any longer, so no `XFAIL` is necessary.
- It keeps the `check-clang-python` target for use by the Clang Python
CI.

Tested on `sparc-sun-solaris2.11`, `sparcv9-sun-solaris2.11`,
`i386-pc-solaris2.11`, `amd64-pc-solaris2.11`, `i686-pc-linux-gnu`, and
`x86_64-pc-linux-gnu`.
2025-06-26 16:34:10 +02:00

72 lines
2.2 KiB
Python

import os
from clang.cindex import Config, File, Index, TranslationUnit
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
inputs_dir = os.path.join(os.path.dirname(__file__), "INPUTS")
class TestFile(unittest.TestCase):
def test_file(self):
index = Index.create()
tu = index.parse("t.c", unsaved_files=[("t.c", "")])
file = File.from_name(tu, "t.c")
self.assertEqual(str(file), "t.c")
self.assertEqual(file.name, "t.c")
self.assertEqual(repr(file), "<File: t.c>")
def test_file_eq(self):
path = os.path.join(inputs_dir, "testfile.c")
path_a = os.path.join(inputs_dir, "a.inc")
path_b = os.path.join(inputs_dir, "b.inc")
tu = TranslationUnit.from_source(path)
main_file = File.from_name(tu, path)
a_file = File.from_name(tu, path_a)
a_file2 = File.from_name(tu, path_a)
b_file = File.from_name(tu, path_b)
self.assertEqual(a_file, a_file2)
self.assertNotEqual(a_file, b_file)
self.assertNotEqual(main_file, a_file)
self.assertNotEqual(main_file, b_file)
self.assertNotEqual(main_file, "t.c")
def test_file_eq_in_memory(self):
tu = TranslationUnit.from_source(
"testfile.c",
unsaved_files=[
(
"testfile.c",
"""
int a[] = {
#include "a.inc"
};
int b[] = {
#include "b.inc"
};
""",
),
("a.inc", "1,2,3"),
("b.inc", "1,2,3"),
],
)
path = os.path.join(inputs_dir, "testfile.c")
path_a = os.path.join(inputs_dir, "a.inc")
path_b = os.path.join(inputs_dir, "b.inc")
tu = TranslationUnit.from_source(path)
main_file = File.from_name(tu, path)
a_file = File.from_name(tu, path_a)
a_file2 = File.from_name(tu, path_a)
b_file = File.from_name(tu, path_b)
self.assertEqual(a_file, a_file2)
self.assertNotEqual(a_file, b_file)
self.assertNotEqual(main_file, a_file)
self.assertNotEqual(main_file, b_file)
self.assertNotEqual(main_file, "a.inc")