Skip to content

[libclang/python] Clean up tests #114383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions clang/bindings/python/tests/cindex/test_access_specifiers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
29 changes: 10 additions & 19 deletions clang/bindings/python/tests/cindex/test_cdb.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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)

Expand All @@ -40,21 +34,18 @@ 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"""
cdb = CompilationDatabase.fromDirectory(kInputsDir)
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):
Expand Down Expand Up @@ -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"""
Expand All @@ -185,4 +176,4 @@ def test_compilationCommands_references(self):
cmd0 = cmds[0]
del cmds
gc.collect()
workingdir = cmd0.directory
cmd0.directory
15 changes: 6 additions & 9 deletions clang/bindings/python/tests/cindex/test_code_completion.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions clang/bindings/python/tests/cindex/test_comment.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
31 changes: 15 additions & 16 deletions clang/bindings/python/tests/cindex/test_cursor.py
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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; };"
Expand Down Expand Up @@ -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)
Comment on lines -347 to +346
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a mistake here in #109846


self.assertTrue(
all(
Expand Down Expand Up @@ -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@Z")
)
Expand Down
5 changes: 2 additions & 3 deletions clang/bindings/python/tests/cindex/test_cursor_kind.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
7 changes: 3 additions & 4 deletions clang/bindings/python/tests/cindex/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
16 changes: 8 additions & 8 deletions clang/bindings/python/tests/cindex/test_enums.py
Original file line number Diff line number Diff line change
@@ -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,
)


Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 2 additions & 3 deletions clang/bindings/python/tests/cindex/test_file.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
8 changes: 3 additions & 5 deletions clang/bindings/python/tests/cindex/test_index.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 4 additions & 8 deletions clang/bindings/python/tests/cindex/test_linkage.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
Loading
Loading