test_cdb.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from clang.cindex import CompilationDatabase
  2. from clang.cindex import CompilationDatabaseError
  3. from clang.cindex import CompileCommands
  4. from clang.cindex import CompileCommand
  5. import os
  6. import gc
  7. kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS')
  8. def test_create_fail():
  9. """Check we fail loading a database with an assertion"""
  10. path = os.path.dirname(__file__)
  11. try:
  12. cdb = CompilationDatabase.fromDirectory(path)
  13. except CompilationDatabaseError as e:
  14. assert e.cdb_error == CompilationDatabaseError.ERROR_CANNOTLOADDATABASE
  15. else:
  16. assert False
  17. def test_create():
  18. """Check we can load a compilation database"""
  19. cdb = CompilationDatabase.fromDirectory(kInputsDir)
  20. def test_lookup_fail():
  21. """Check file lookup failure"""
  22. cdb = CompilationDatabase.fromDirectory(kInputsDir)
  23. assert cdb.getCompileCommands('file_do_not_exist.cpp') == None
  24. def test_lookup_succeed():
  25. """Check we get some results if the file exists in the db"""
  26. cdb = CompilationDatabase.fromDirectory(kInputsDir)
  27. cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
  28. assert len(cmds) != 0
  29. def test_all_compilecommand():
  30. """Check we get all results from the db"""
  31. cdb = CompilationDatabase.fromDirectory(kInputsDir)
  32. cmds = cdb.getAllCompileCommands()
  33. assert len(cmds) == 3
  34. expected = [
  35. { 'wd': '/home/john.doe/MyProjectA',
  36. 'line': ['clang++', '-o', 'project2.o', '-c',
  37. '/home/john.doe/MyProject/project2.cpp']},
  38. { 'wd': '/home/john.doe/MyProjectB',
  39. 'line': ['clang++', '-DFEATURE=1', '-o', 'project2-feature.o', '-c',
  40. '/home/john.doe/MyProject/project2.cpp']},
  41. { 'wd': '/home/john.doe/MyProject',
  42. 'line': ['clang++', '-o', 'project.o', '-c',
  43. '/home/john.doe/MyProject/project.cpp']}
  44. ]
  45. for i in range(len(cmds)):
  46. assert cmds[i].directory == expected[i]['wd']
  47. for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
  48. assert arg == exp
  49. def test_1_compilecommand():
  50. """Check file with single compile command"""
  51. cdb = CompilationDatabase.fromDirectory(kInputsDir)
  52. cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
  53. assert len(cmds) == 1
  54. assert cmds[0].directory == '/home/john.doe/MyProject'
  55. expected = [ 'clang++', '-o', 'project.o', '-c',
  56. '/home/john.doe/MyProject/project.cpp']
  57. for arg, exp in zip(cmds[0].arguments, expected):
  58. assert arg == exp
  59. def test_2_compilecommand():
  60. """Check file with 2 compile commands"""
  61. cdb = CompilationDatabase.fromDirectory(kInputsDir)
  62. cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project2.cpp')
  63. assert len(cmds) == 2
  64. expected = [
  65. { 'wd': '/home/john.doe/MyProjectA',
  66. 'line': ['clang++', '-o', 'project2.o', '-c',
  67. '/home/john.doe/MyProject/project2.cpp']},
  68. { 'wd': '/home/john.doe/MyProjectB',
  69. 'line': ['clang++', '-DFEATURE=1', '-o', 'project2-feature.o', '-c',
  70. '/home/john.doe/MyProject/project2.cpp']}
  71. ]
  72. for i in range(len(cmds)):
  73. assert cmds[i].directory == expected[i]['wd']
  74. for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
  75. assert arg == exp
  76. def test_compilecommand_iterator_stops():
  77. """Check that iterator stops after the correct number of elements"""
  78. cdb = CompilationDatabase.fromDirectory(kInputsDir)
  79. count = 0
  80. for cmd in cdb.getCompileCommands('/home/john.doe/MyProject/project2.cpp'):
  81. count += 1
  82. assert count <= 2
  83. def test_compilationDB_references():
  84. """Ensure CompilationsCommands are independent of the database"""
  85. cdb = CompilationDatabase.fromDirectory(kInputsDir)
  86. cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
  87. del cdb
  88. gc.collect()
  89. workingdir = cmds[0].directory
  90. def test_compilationCommands_references():
  91. """Ensure CompilationsCommand keeps a reference to CompilationCommands"""
  92. cdb = CompilationDatabase.fromDirectory(kInputsDir)
  93. cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
  94. del cdb
  95. cmd0 = cmds[0]
  96. del cmds
  97. gc.collect()
  98. workingdir = cmd0.directory