[clang] [libclang/python] Clean up tests (PR #114383)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 31 02:40:22 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Jannick Kremer (DeinAlptraum)
<details>
<summary>Changes</summary>
Clean up imports via isort
Remove unused imports
Remove unused variables
Remove Python <3.6 compatibility measures
---
Patch is 24.66 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/114383.diff
21 Files Affected:
- (modified) clang/bindings/python/tests/cindex/test_access_specifiers.py (+4-8)
- (modified) clang/bindings/python/tests/cindex/test_cdb.py (+10-19)
- (modified) clang/bindings/python/tests/cindex/test_code_completion.py (+6-9)
- (modified) clang/bindings/python/tests/cindex/test_comment.py (+4-4)
- (modified) clang/bindings/python/tests/cindex/test_cursor.py (+15-16)
- (modified) clang/bindings/python/tests/cindex/test_cursor_kind.py (+2-3)
- (modified) clang/bindings/python/tests/cindex/test_diagnostics.py (+3-4)
- (modified) clang/bindings/python/tests/cindex/test_enums.py (+8-8)
- (modified) clang/bindings/python/tests/cindex/test_exception_specification_kind.py (+5-6)
- (modified) clang/bindings/python/tests/cindex/test_file.py (+2-3)
- (modified) clang/bindings/python/tests/cindex/test_index.py (+3-5)
- (modified) clang/bindings/python/tests/cindex/test_linkage.py (+4-8)
- (modified) clang/bindings/python/tests/cindex/test_location.py (+10-9)
- (modified) clang/bindings/python/tests/cindex/test_rewrite.py (+2-8)
- (modified) clang/bindings/python/tests/cindex/test_source_range.py (+2-2)
- (modified) clang/bindings/python/tests/cindex/test_tls_kind.py (+4-8)
- (modified) clang/bindings/python/tests/cindex/test_token_kind.py (+2-3)
- (modified) clang/bindings/python/tests/cindex/test_tokens.py (+3-8)
- (modified) clang/bindings/python/tests/cindex/test_translation_unit.py (+25-33)
- (modified) clang/bindings/python/tests/cindex/test_type.py (+6-12)
- (modified) clang/bindings/python/tests/cindex/util.py (+1-19)
``````````diff
diff --git a/clang/bindings/python/tests/cindex/test_access_specifiers.py b/clang/bindings/python/tests/cindex/test_access_specifiers.py
index c1cc18ebe6e589..ca2bbd3cc86117 100644
--- a/clang/bindings/python/tests/cindex/test_access_specifiers.py
+++ b/clang/bindings/python/tests/cindex/test_access_specifiers.py
@@ -1,18 +1,14 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import AccessSpecifier, Config
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import AccessSpecifier
-from clang.cindex import Cursor
-from clang.cindex import TranslationUnit
-
-from .util import get_cursor
-from .util import get_tu
-
import unittest
+from .util import get_cursor, get_tu
+
class TestAccessSpecifiers(unittest.TestCase):
def test_access_specifiers(self):
diff --git a/clang/bindings/python/tests/cindex/test_cdb.py b/clang/bindings/python/tests/cindex/test_cdb.py
index a5cc22796aa2ad..d0adbb36d66f2a 100644
--- a/clang/bindings/python/tests/cindex/test_cdb.py
+++ b/clang/bindings/python/tests/cindex/test_cdb.py
@@ -1,20 +1,14 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import CompilationDatabase, CompilationDatabaseError, Config
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import CompilationDatabase
-from clang.cindex import CompilationDatabaseError
-from clang.cindex import CompileCommands
-from clang.cindex import CompileCommand
-import os
import gc
-import unittest
import sys
-from .util import skip_if_no_fspath
-from .util import str_to_path
-
+import unittest
+from pathlib import Path
kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
@@ -31,7 +25,7 @@ def test_create_fail(self):
with open(os.devnull, "wb") as null:
os.dup2(null.fileno(), 2)
with self.assertRaises(CompilationDatabaseError) as cm:
- cdb = CompilationDatabase.fromDirectory(path)
+ CompilationDatabase.fromDirectory(path)
os.dup2(stderr, 2)
os.close(stderr)
@@ -40,7 +34,7 @@ def test_create_fail(self):
def test_create(self):
"""Check we can load a compilation database"""
- cdb = CompilationDatabase.fromDirectory(kInputsDir)
+ CompilationDatabase.fromDirectory(kInputsDir)
def test_lookup_succeed(self):
"""Check we get some results if the file exists in the db"""
@@ -48,13 +42,10 @@ def test_lookup_succeed(self):
cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
self.assertNotEqual(len(cmds), 0)
- @skip_if_no_fspath
def test_lookup_succeed_pathlike(self):
"""Same as test_lookup_succeed, but with PathLikes"""
- cdb = CompilationDatabase.fromDirectory(str_to_path(kInputsDir))
- cmds = cdb.getCompileCommands(
- str_to_path("/home/john.doe/MyProject/project.cpp")
- )
+ cdb = CompilationDatabase.fromDirectory(Path(kInputsDir))
+ cmds = cdb.getCompileCommands(Path("/home/john.doe/MyProject/project.cpp"))
self.assertNotEqual(len(cmds), 0)
def test_all_compilecommand(self):
@@ -175,7 +166,7 @@ def test_compilationDB_references(self):
cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
del cdb
gc.collect()
- workingdir = cmds[0].directory
+ cmds[0].directory
def test_compilationCommands_references(self):
"""Ensure CompilationsCommand keeps a reference to CompilationCommands"""
@@ -185,4 +176,4 @@ def test_compilationCommands_references(self):
cmd0 = cmds[0]
del cmds
gc.collect()
- workingdir = cmd0.directory
+ cmd0.directory
diff --git a/clang/bindings/python/tests/cindex/test_code_completion.py b/clang/bindings/python/tests/cindex/test_code_completion.py
index 1d513dbca25364..c7a86aa82a8ebc 100644
--- a/clang/bindings/python/tests/cindex/test_code_completion.py
+++ b/clang/bindings/python/tests/cindex/test_code_completion.py
@@ -1,14 +1,12 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, TranslationUnit
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import TranslationUnit
-
import unittest
-from .util import skip_if_no_fspath
-from .util import str_to_path
+from pathlib import Path
class TestCodeCompletion(unittest.TestCase):
@@ -57,11 +55,10 @@ def test_code_complete(self):
]
self.check_completion_results(cr, expected)
- @skip_if_no_fspath
def test_code_complete_pathlike(self):
files = [
(
- str_to_path("fake.c"),
+ Path("fake.c"),
"""
/// Aaa.
int test1;
@@ -77,14 +74,14 @@ def test_code_complete_pathlike(self):
]
tu = TranslationUnit.from_source(
- str_to_path("fake.c"),
+ Path("fake.c"),
["-std=c99"],
unsaved_files=files,
options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION,
)
cr = tu.codeComplete(
- str_to_path("fake.c"),
+ Path("fake.c"),
9,
1,
unsaved_files=files,
diff --git a/clang/bindings/python/tests/cindex/test_comment.py b/clang/bindings/python/tests/cindex/test_comment.py
index 265c6d3d73de04..1ecbb42c18ffca 100644
--- a/clang/bindings/python/tests/cindex/test_comment.py
+++ b/clang/bindings/python/tests/cindex/test_comment.py
@@ -1,14 +1,14 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, TranslationUnit
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import TranslationUnit
-from tests.cindex.util import get_cursor
-
import unittest
+from .util import get_cursor
+
class TestComment(unittest.TestCase):
def test_comment(self):
diff --git a/clang/bindings/python/tests/cindex/test_cursor.py b/clang/bindings/python/tests/cindex/test_cursor.py
index 77d8ca415708f8..a36c6e803887fb 100644
--- a/clang/bindings/python/tests/cindex/test_cursor.py
+++ b/clang/bindings/python/tests/cindex/test_cursor.py
@@ -1,24 +1,23 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import (
+ AvailabilityKind,
+ BinaryOperator,
+ Config,
+ CursorKind,
+ StorageClass,
+ TemplateArgumentKind,
+ TranslationUnit,
+ TypeKind,
+)
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-import ctypes
import gc
import unittest
-from clang.cindex import AvailabilityKind
-from clang.cindex import CursorKind
-from clang.cindex import TemplateArgumentKind
-from clang.cindex import TranslationUnit
-from clang.cindex import TypeKind
-from clang.cindex import BinaryOperator
-from clang.cindex import StorageClass
-from .util import get_cursor
-from .util import get_cursors
-from .util import get_tu
-
+from .util import get_cursor, get_cursors, get_tu
kInput = """\
struct s0 {
@@ -170,7 +169,7 @@ def test_references(self):
self.assertIsInstance(cursor.translation_unit, TranslationUnit)
# If the TU was destroyed, this should cause a segfault.
- parent = cursor.semantic_parent
+ cursor.semantic_parent
def test_canonical(self):
source = "struct X; struct X; struct X { int member; };"
@@ -344,7 +343,7 @@ class Bar {
)
self.assertEqual(len(copy_assignment_operators_cursors), 10)
- self.assertTrue(len(non_copy_assignment_operators_cursors), 9)
+ self.assertEqual(len(non_copy_assignment_operators_cursors), 7)
self.assertTrue(
all(
@@ -923,7 +922,7 @@ def test_mangled_name(self):
# and force the target. To enable this test to pass on all platforms, accept
# all valid manglings.
# [c-index-test handles this by running the source through clang, emitting
- # an AST file and running libclang on that AST file]
+ # an AST file and running libclang on that AST file]
self.assertIn(
foo.mangled_name, ("_Z3fooii", "__Z3fooii", "?foo@@YAHHH", "?foo@@YAHHH at Z")
)
diff --git a/clang/bindings/python/tests/cindex/test_cursor_kind.py b/clang/bindings/python/tests/cindex/test_cursor_kind.py
index 87199dba06ed25..3b693ff45cfd44 100644
--- a/clang/bindings/python/tests/cindex/test_cursor_kind.py
+++ b/clang/bindings/python/tests/cindex/test_cursor_kind.py
@@ -1,11 +1,10 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, CursorKind
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import CursorKind
-
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_diagnostics.py b/clang/bindings/python/tests/cindex/test_diagnostics.py
index 041083d12c7f16..2ebd3414d692d8 100644
--- a/clang/bindings/python/tests/cindex/test_diagnostics.py
+++ b/clang/bindings/python/tests/cindex/test_diagnostics.py
@@ -1,14 +1,13 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, Diagnostic
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import *
-from .util import get_tu
-
import unittest
+from .util import get_tu
# FIXME: We need support for invalid translation units to test better.
diff --git a/clang/bindings/python/tests/cindex/test_enums.py b/clang/bindings/python/tests/cindex/test_enums.py
index 63b2292c5d9bdc..9e7f44fcf7867c 100644
--- a/clang/bindings/python/tests/cindex/test_enums.py
+++ b/clang/bindings/python/tests/cindex/test_enums.py
@@ -1,18 +1,18 @@
import unittest
from clang.cindex import (
- TokenKind,
+ AccessSpecifier,
+ AvailabilityKind,
+ BinaryOperator,
CursorKind,
- TemplateArgumentKind,
ExceptionSpecificationKind,
- AvailabilityKind,
- AccessSpecifier,
- TypeKind,
- RefQualifierKind,
LinkageKind,
- TLSKind,
+ RefQualifierKind,
StorageClass,
- BinaryOperator,
+ TemplateArgumentKind,
+ TLSKind,
+ TokenKind,
+ TypeKind,
)
diff --git a/clang/bindings/python/tests/cindex/test_exception_specification_kind.py b/clang/bindings/python/tests/cindex/test_exception_specification_kind.py
index e4742db31adbe2..f7806ffad80124 100644
--- a/clang/bindings/python/tests/cindex/test_exception_specification_kind.py
+++ b/clang/bindings/python/tests/cindex/test_exception_specification_kind.py
@@ -1,18 +1,17 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, CursorKind, ExceptionSpecificationKind
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-import clang.cindex
-from clang.cindex import ExceptionSpecificationKind
-from .util import get_tu
-
import unittest
+from .util import get_tu
+
def find_function_declarations(node, declarations=[]):
- if node.kind == clang.cindex.CursorKind.FUNCTION_DECL:
+ if node.kind == CursorKind.FUNCTION_DECL:
declarations.append(node)
for child in node.get_children():
declarations = find_function_declarations(child, declarations)
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 7024b0cdf11d97..14a3084ee2b47a 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -1,11 +1,10 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, File, Index
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import Index, File
-
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_index.py b/clang/bindings/python/tests/cindex/test_index.py
index bf29628f5e4e79..756f4bd9c7dfb5 100644
--- a/clang/bindings/python/tests/cindex/test_index.py
+++ b/clang/bindings/python/tests/cindex/test_index.py
@@ -1,20 +1,18 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, Index, TranslationUnit
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import *
-import os
import unittest
-
kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
class TestIndex(unittest.TestCase):
def test_create(self):
- index = Index.create()
+ Index.create()
# FIXME: test Index.read
diff --git a/clang/bindings/python/tests/cindex/test_linkage.py b/clang/bindings/python/tests/cindex/test_linkage.py
index 4a8838276fae57..93bf43a0420476 100644
--- a/clang/bindings/python/tests/cindex/test_linkage.py
+++ b/clang/bindings/python/tests/cindex/test_linkage.py
@@ -1,18 +1,14 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, LinkageKind
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import LinkageKind
-from clang.cindex import Cursor
-from clang.cindex import TranslationUnit
-
-from .util import get_cursor
-from .util import get_tu
-
import unittest
+from .util import get_cursor, get_tu
+
class TestLinkage(unittest.TestCase):
def test_linkage(self):
diff --git a/clang/bindings/python/tests/cindex/test_location.py b/clang/bindings/python/tests/cindex/test_location.py
index 27854a312e6721..bbf79126ab1f8d 100644
--- a/clang/bindings/python/tests/cindex/test_location.py
+++ b/clang/bindings/python/tests/cindex/test_location.py
@@ -1,19 +1,20 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import (
+ Config,
+ Cursor,
+ File,
+ SourceLocation,
+ SourceRange,
+ TranslationUnit,
+)
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import Cursor
-from clang.cindex import File
-from clang.cindex import SourceLocation
-from clang.cindex import SourceRange
-from clang.cindex import TranslationUnit
-from .util import get_cursor
-from .util import get_tu
-
import unittest
+from .util import get_cursor, get_tu
baseInput = "int one;\nint two;\n"
diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py
index 42006f57554e28..6f7e5d906172c1 100644
--- a/clang/bindings/python/tests/cindex/test_rewrite.py
+++ b/clang/bindings/python/tests/cindex/test_rewrite.py
@@ -1,13 +1,7 @@
-import unittest
import tempfile
+import unittest
-from clang.cindex import (
- Rewriter,
- TranslationUnit,
- File,
- SourceLocation,
- SourceRange,
-)
+from clang.cindex import File, Rewriter, SourceLocation, SourceRange, TranslationUnit
class TestRewrite(unittest.TestCase):
diff --git a/clang/bindings/python/tests/cindex/test_source_range.py b/clang/bindings/python/tests/cindex/test_source_range.py
index 47d8960fcafb35..81c0a9b05cff82 100644
--- a/clang/bindings/python/tests/cindex/test_source_range.py
+++ b/clang/bindings/python/tests/cindex/test_source_range.py
@@ -1,11 +1,11 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, SourceLocation, SourceRange, TranslationUnit
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
-from clang.cindex import SourceLocation, SourceRange, TranslationUnit
from .util import get_tu
diff --git a/clang/bindings/python/tests/cindex/test_tls_kind.py b/clang/bindings/python/tests/cindex/test_tls_kind.py
index b8ef74614ab038..f80a46f4d56806 100644
--- a/clang/bindings/python/tests/cindex/test_tls_kind.py
+++ b/clang/bindings/python/tests/cindex/test_tls_kind.py
@@ -1,18 +1,14 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, TLSKind
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import TLSKind
-from clang.cindex import Cursor
-from clang.cindex import TranslationUnit
-
-from .util import get_cursor
-from .util import get_tu
-
import unittest
+from .util import get_cursor, get_tu
+
class TestTLSKind(unittest.TestCase):
def test_tls_kind(self):
diff --git a/clang/bindings/python/tests/cindex/test_token_kind.py b/clang/bindings/python/tests/cindex/test_token_kind.py
index 747d328a577dc4..594f30a448d84f 100644
--- a/clang/bindings/python/tests/cindex/test_token_kind.py
+++ b/clang/bindings/python/tests/cindex/test_token_kind.py
@@ -1,11 +1,10 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, TokenKind
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import TokenKind
-
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_tokens.py b/clang/bindings/python/tests/cindex/test_tokens.py
index 2cbf42c4c6cb93..b6c1fc8b836009 100644
--- a/clang/bindings/python/tests/cindex/test_tokens.py
+++ b/clang/bindings/python/tests/cindex/test_tokens.py
@@ -1,19 +1,14 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import Config, CursorKind, SourceLocation, SourceRange, TokenKind
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from clang.cindex import CursorKind
-from clang.cindex import Index
-from clang.cindex import SourceLocation
-from clang.cindex import SourceRange
-from clang.cindex import TokenKind
+import unittest
from .util import get_tu
-import unittest
-
class TestTokens(unittest.TestCase):
def test_token_to_cursor(self):
diff --git a/clang/bindings/python/tests/cindex/test_translation_unit.py b/clang/bindings/python/tests/cindex/test_translation_unit.py
index ff7213c69dd0f1..56bf3742417554 100644
--- a/clang/bindings/python/tests/cindex/test_translation_unit.py
+++ b/clang/bindings/python/tests/cindex/test_translation_unit.py
@@ -1,30 +1,28 @@
import os
-from clang.cindex import Config
+
+from clang.cindex import (
+ Config,
+ Cursor,
+ CursorKind,
+ File,
+ Index,
+ SourceLocation,
+ SourceRange,
+ TranslationUnit,
+ TranslationUnitLoadError,
+ TranslationUnitSaveError,
+)
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
-from contextlib import contextmanager
import gc
-import os
-import sys
import tempfile
import unittest
+from contextlib import contextmanager
+from pathlib import Path
-from clang.cindex import CursorKind
-from clang.cindex import Cursor
-from clang.cindex import File
-from clang.cindex import Index
-from clang.cindex import SourceLocation
-from clang.cindex import SourceRange
-from clang.cindex import TranslationUnitSaveError
-from clang.cindex import TranslationUnitLoadError
-from clang.cindex import TranslationUnit
-from .util import get_cursor
-from .util import get_tu
-from .util import skip_if_no_fspath
-from .util import str_to_path
-
+from .util import get_cursor, get_tu
kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
@@ -47,7 +45,7 @@ def save_tu_pathlike(tu):
Returns the filename it was saved to.
"""
with tempfile.NamedTemporaryFile() as t:
- tu.save(str_to_path(t.name))
+ tu.save(Path(t.name))
yield t.name
@@ -105,24 +103,21 @@ def test_unsaved_files(self):
self.assertEqual(spellings[-1], "y")
def test_unsaved_files_2(self):
- if sys.version_info.major >= 3:
- from io import StringIO
- else:
- from io import BytesIO as StringIO
+ from io import StringIO
+
tu = TranslationUnit.from_source(
"fake.c", unsaved_files=[("fake.c", StringIO("int x;"))]
)
spellings = [c.spelling for c in tu.cursor.get_children()]
self.assertEqual(spellings[-1], "x")
- @skip_if_no_fspath
def test_from_source_accepts_pathlike(self):
tu = TranslationUnit.from_source(
- str_to_path("fake.c"),
+ Path("fake.c"),
["-Iincludes"],
unsaved_files=[
(
- str_to_path...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/114383
More information about the cfe-commits
mailing list