Bug 1611515 - Add test cases for CompileDB command output. r=rstewart

Also this patch adds tests for `ClangdBackend` and `StaticAnalysisBackend` since
both of them are derived from `CompileDB`.

Differential Revision: https://phabricator.services.mozilla.com/D91768
This commit is contained in:
Andi-Bogdan Postelnicu 2020-09-30 14:52:54 +00:00
Родитель 0ec28a45b6
Коммит ed9f7b17b0
9 изменённых файлов: 107 добавлений и 0 удалений

Просмотреть файл

@ -42,6 +42,15 @@ CONFIGS = defaultdict(lambda: {
'COMPILE_ENVIRONMENT': '1',
},
},
'database': {
'defines': {},
'substs': {
'CC': 'clang',
'CXX': 'clang++',
'LIB_PREFIX': 'lib',
'LIB_SUFFIX': 'a',
},
},
'rust-library': {
'defines': {},
'substs': {

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -0,0 +1,12 @@
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/
@template
def Library(name):
'''Template for libraries.'''
LIBRARY_NAME = name
Library('dummy')
SOURCES = ['bar.c', 'bar.cpp', 'foo.c', 'foo.cpp']

Просмотреть файл

@ -0,0 +1,84 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import, print_function, unicode_literals
import json
import os
import six
from mozbuild.compilation.database import CompileDBBackend
from mozbuild.backend.clangd import ClangdBackend
from mozbuild.backend.static_analysis import StaticAnalysisBackend
from mozbuild.test.backend.common import BackendTester
from mozunit import main
class TestCompileDBBackends(BackendTester):
def perform_check(self, compile_commands_path, topsrcdir, topobjdir):
self.assertTrue(os.path.exists(compile_commands_path))
compile_db = json.loads(open(compile_commands_path, "r").read())
# Verify that we have the same number of items
self.assertEqual(len(compile_db), 4)
expected_db = [
{
"directory": topobjdir,
"command": "clang -o /dev/null -c -ferror-limit=0 {}/bar.c".format(topsrcdir),
"file": "{}/bar.c".format(topsrcdir),
},
{
"directory": topobjdir,
"command": "clang -o /dev/null -c -ferror-limit=0 {}/foo.c".format(topsrcdir),
"file": "{}/foo.c".format(topsrcdir),
},
{
"directory": topobjdir,
"command": "clang++ -o /dev/null -c -ferror-limit=0 {}/bar.cpp".format(topsrcdir),
"file": "{}/bar.cpp".format(topsrcdir),
},
{
"directory": topobjdir,
"command": "clang++ -o /dev/null -c -ferror-limit=0 {}/foo.cpp".format(topsrcdir),
"file": "{}/foo.cpp".format(topsrcdir),
},
]
# Verify item consistency against `expected_db`
six.assertCountEqual(self, compile_db, expected_db)
def test_database(self):
"""Ensure we can generate a `compile_commands.json` and that is correct."""
env = self._consume("database", CompileDBBackend)
compile_commands_path = os.path.join(env.topobjdir, "compile_commands.json")
self.perform_check(compile_commands_path, env.topsrcdir, env.topobjdir)
def test_clangd(self):
"""Ensure we can generate a `compile_commands.json` and that is correct.
in order to be used by ClandBackend"""
env = self._consume("database", ClangdBackend)
compile_commands_path = os.path.join(env.topobjdir, "clangd", "compile_commands.json")
self.perform_check(compile_commands_path, env.topsrcdir, env.topobjdir)
def test_static_analysis(self):
"""Ensure we can generate a `compile_commands.json` and that is correct.
in order to be used by StaticAnalysisBackend"""
env = self._consume("database", StaticAnalysisBackend)
compile_commands_path = os.path.join(
env.topobjdir, "static-analysis", "compile_commands.json"
)
self.perform_check(compile_commands_path, env.topsrcdir, env.topobjdir)
if __name__ == "__main__":
main()

Просмотреть файл

@ -8,6 +8,8 @@ subsuite = mozbuild
[backend/test_fastermake.py]
[backend/test_recursivemake.py]
[backend/test_build.py]
[backend/test_database.py]
skip-if = python == 2
[backend/test_configenvironment.py]
[backend/test_gn_processor.py]
[backend/test_partialconfigenvironment.py]