Skip to content

Commit

Permalink
Enhance test project and fix C++ test expectations
Browse files Browse the repository at this point in the history
 - Added notification about setting `TEST_DB` CMake configuration
   variable at creation of project build folder. If missing, the user is
   given a warning on the command-line too.

C++-specific:
 - There was a test for the C++ references where the comment rightfully
   said 8 expected references, but only 7 was in the code. This has been
   fixed.
 - Better output from the `cpptest` binary and explained what the
   arguments given mean.
  • Loading branch information
whisperity committed Jul 19, 2018
1 parent 5981349 commit 81ebd3b
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 22 deletions.
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# IDEs and editors
# IDEs and editors.

nbproject
*.swp

# Generated files and directories
# Generated files and directories.

**/*odb\.cxx
**/*odb\.hxx
Expand All @@ -13,7 +13,3 @@ nbproject
**/gen-cpp/
**/gen-js/
**/gen-java/

# Other

/build/
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ include(Config.cmake)
# Utility functions
include(Functions.cmake)

enable_testing()
# Do some sanity check on the testing setup and enable testing if applicable.
include(Testing.cmake)

find_package(Threads REQUIRED)
find_package(LLVM REQUIRED CONFIG)
Expand Down
19 changes: 19 additions & 0 deletions Functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,22 @@ function(find_boost_libraries)

set(Boost_LINK_LIBRARIES ${LIBS} PARENT_SCOPE)
endfunction(find_boost_libraries)

# Prints a coloured, and optionally bold message to the console.
# _colour should be some ANSI colour name, like "blue" or "magenta".
function(fancy_message _str _colour _isBold)
set(BOLD_TAG "")
set(COLOUR_TAG "")

if (_isBold)
set(BOLD_TAG "--bold")
endif()

if (NOT (_colour STREQUAL ""))
set(COLOUR_TAG "--${_colour}")
endif()

execute_process(COMMAND
${CMAKE_COMMAND} -E env CLICOLOR_FORCE=1
${CMAKE_COMMAND} -E cmake_echo_color ${COLOUR_TAG} ${BOLD_TAG} ${_str})
endfunction(fancy_message)
36 changes: 36 additions & 0 deletions Testing.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Testing-specific configurations and setup calls.

if (NOT DEFINED TEST_DB)
set(FUNCTIONAL_TESTING_ENABLED
FALSE
CACHE INTERNAL
"Whether testing executable functionality was configured for the build.")

message(WARNING "TEST_DB variable not set. Skipping generation of functional \
tests... To enable, set a database connection string which \
can be used for the parsing of the test projects' files.")
else()
enable_testing()

# Initialize the variable in the cache and mark it as a string.
set(TEST_DB "" CACHE STRING
"Database connection string used in running functional tests.")

set(FUNCTIONAL_TESTING_ENABLED
TRUE
CACHE INTERNAL
"Whether testing executable functionality was configured for the build.")

# Ensure that if TEST_DB is set, then it uses the database backend CodeCompass
# will be built with.
string(FIND "${TEST_DB}" "${DATABASE}:" testDbUsesProperBackend)
if (NOT TEST_DB STREQUAL "" AND NOT testDbUsesProperBackend EQUAL 0)
string(FIND "${TEST_DB}" ":" findPos)
string(SUBSTRING "${TEST_DB}" 0 ${findPos} testDbBackend)
message("${testDbBackend}")
message(FATAL_ERROR "CodeCompass is being built with database backend \
'${DATABASE}', but TEST_DB was set to use \
'${testDbBackend}'. The same database system must be \
used, otherwise the tests won't run.")
endif()
endif()
1 change: 1 addition & 0 deletions doc/deps.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,4 @@ during compilation.
| `CMAKE_BUILD_TYPE` | Specifies the build type on single-configuration generators. Possible values are empty, **Debug**, **Release**. For more information see: https://cmake.org/cmake/help/v3.0/variable/CMAKE_BUILD_TYPE.html |
| `CMAKE_CXX_COMPILER` | If the official repository of your Linux distribution doesn't contain a C++ compiler which supports C++14 then you can install one manually and set to use it. For more information see: https://cmake.org/Wiki/CMake_Useful_Variables |
| `DATABASE` | Database type. Possible values are **sqlite**, **pgsql**. The default value is `sqlite`. |
| `TEST_DB` | The connection string for the database that will be used when executing tests with `make test`. |
43 changes: 32 additions & 11 deletions plugins/cpp/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,42 @@ find_boost_libraries(
log
program_options
system)

target_link_libraries(cpptest
util
cppservice
${Boost_LINK_LIBRARIES}
${GTEST_BOTH_LIBRARIES}
pthread)

# Add test to the project to run by ctest
add_test(NAME allCppTest COMMAND cpptest
"mkdir -p ${CMAKE_CURRENT_SOURCE_DIR}/sources/build &&\
cd ${CMAKE_CURRENT_SOURCE_DIR}/sources/build &&\
cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=on"
"${CMAKE_INSTALL_PREFIX}/bin/keepalive ${CMAKE_INSTALL_PREFIX}/bin/CodeCompass_parser \
-d \"${TEST_DB}\"\
-n cpptest \
-i ${CMAKE_CURRENT_SOURCE_DIR}/sources/build/compile_commands.json \
-w ${CMAKE_CURRENT_SOURCE_DIR}/workdir/ -f"
"${TEST_DB}")
if (NOT FUNCTIONAL_TESTING_ENABLED)
fancy_message("Skipping generation of test project cpptest." "yellow" TRUE)
else()
# Clean up the build folder and the output of the test in a `make clean`.
set_property(DIRECTORY APPEND PROPERTY
ADDITIONAL_MAKE_CLEAN_FILES
"${CMAKE_CURRENT_BINARY_DIR}/build"
"${CMAKE_CURRENT_BINARY_DIR}/workdir")

# Add test to the project to run by ctest.
#
# There are three arguments that are passed to the `cpptest` binary here.
# The first sets up the build environment and builds the project.
# The second executes the parsing.
# Then we put the database string which will be used by the tests.
add_test(NAME cpp COMMAND cpptest
"echo \"Test database used: ${TEST_DB}\" && \
mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/build && \
cd ${CMAKE_CURRENT_BINARY_DIR}/build && \
cmake ${CMAKE_CURRENT_SOURCE_DIR}/sources \
-DCMAKE_EXPORT_COMPILE_COMMANDS=on"
"${CMAKE_INSTALL_PREFIX}/bin/keepalive \
${CMAKE_INSTALL_PREFIX}/bin/CodeCompass_parser \
--database \"${TEST_DB}\" \
--name cpptest \
--input ${CMAKE_CURRENT_BINARY_DIR}/build/compile_commands.json \
--workspace ${CMAKE_CURRENT_BINARY_DIR}/workdir/ \
--force"
"${TEST_DB}")
fancy_message("Generating test project for cpptest." "blue" TRUE)
endif()
10 changes: 10 additions & 0 deletions plugins/cpp/test/sources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 2.6)
project(CppTestProject)

# This is a dummy CMakeList that can be used to generate a build for the
# C++ test input files.

add_library(CppTestProject STATIC
inheritance.cpp
nestedclass.cpp
simpleclass.cpp)
4 changes: 2 additions & 2 deletions plugins/cpp/test/src/cppreferenceservicetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ TEST_F(CppReferenceServiceTest, DerivedClassTest)
{"Data member", {34}}, /*!< Z */
{"Definition", {31}},
{"Inherits from", {9, 21}}, /*!< BaseClass1, BaseClass2 */
{"Method", {-1, -1, -1, -1, 36, 38, 40}}, /*!< f, g, h, copy ctor, move ctor
cpy operator=, move operator=, dtor */
{"Method", {-1, -1, -1, -1, -1, 36, 38, 40}}, /*!< move assignment,
copy assignment, move ctor, copy ctor, dtor, f, g, h */
{"Usage", {31, 8, 40, 45, 50}}
};

Expand Down
14 changes: 12 additions & 2 deletions plugins/cpp/test/src/cppservicetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ const char* dbConnectionString;

int main(int argc, char** argv)
{
dbConnectionString = argv[3];
if (strcmp(dbConnectionString, "") == 0)
{
GTEST_LOG_(FATAL) << "No test database connection given.";
return 1;
}

GTEST_LOG_(INFO) << "Testing C++ started...";
GTEST_LOG_(INFO) << "Executing build command: " << argv[1];
system(argv[1]);
system(argv[2]);

dbConnectionString = argv[3];
GTEST_LOG_(INFO) << "Executing parser command: " << argv[2];
system(argv[2]);

GTEST_LOG_(INFO) << "Using database for tests: " << dbConnectionString;
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 81ebd3b

Please sign in to comment.