Skip to content

Commit c549436

Browse files
committed
Fix pr issues
1 parent ed82cb3 commit c549436

9 files changed

+257
-251
lines changed

.travis.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@ addons:
1313
- ubuntu-toolchain-r-test
1414
- george-edison55-precise-backports
1515
packages:
16-
- gcc-5
1716
- g++-5
18-
- libsqlite3-dev
1917
- libsqlcipher-dev
20-
- cmake-data
2118
- cmake
2219

2320
before_install:
2421
- export CXX="g++-5" CC="gcc-5"
2522

26-
script: mkdir build && cd ./build && cmake .. && make && ./tests
23+
script: mkdir build && cd ./build && cmake .. && cmake --build . && ctest .
2724

2825
# TODO: fix sqlcipher test
2926
# script: mkdir build && cd ./build && cmake -DENABLE_SQLCIPHER_TESTS=ON .. && make && ./tests

.ycm_extra_conf.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,14 @@ def DirectoryOfThisScript():
1515
flags = [
1616
'-Wall',
1717
'-std=c++14',
18-
'-stdlib=libc++',
1918
'-x',
2019
'c++',
21-
'-I',
22-
'.',
2320
'-isystem', '/usr/local/include',
2421
'-isystem', '/usr/include',
2522
'-I.',
2623
]
2724

28-
if compilation_database_folder:
29-
database = ycm_core.CompilationDatabase(compilation_database_folder)
30-
else:
31-
database = None
25+
database = ycm_core.CompilationDatabase(compilation_database_folder)
3226

3327
SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
3428

CMakeLists.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ set (CMAKE_CXX_STANDARD 14)
77

88
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
99
include("cmake/HunterGate.cmake")
10-
include("cmake/ParseAndAddCatchTests.cmake")
11-
include("cmake/DownloadProject.CMake")
10+
include("cmake/Catch.cmake")
1211

1312
HunterGate(
1413
URL "https://github.com/ruslo/hunter/archive/v0.19.227.tar.gz"
@@ -43,7 +42,7 @@ else()
4342
target_link_libraries(tests Catch::Catch sqlite_modern_cpp sqlite3::sqlite3)
4443
endif()
4544

46-
ParseAndAddCatchTests(tests)
45+
catch_discover_tests(tests)
4746

4847
# Place the file in the source directory, permitting us to place a single configuration file for YCM there.
4948
# YCM is the code-completion engine for (neo)vim https://github.com/Valloric/YouCompleteMe

cmake/Catch.cmake

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2+
# file Copyright.txt or https://cmake.org/licensing for details.
3+
4+
#[=======================================================================[.rst:
5+
Catch
6+
-----
7+
8+
This module defines a function to help use the Catch test framework.
9+
10+
The :command:`catch_discover_tests` discovers tests by asking the compiled test
11+
executable to enumerate its tests. This does not require CMake to be re-run
12+
when tests change. However, it may not work in a cross-compiling environment,
13+
and setting test properties is less convenient.
14+
15+
This command is intended to replace use of :command:`add_test` to register
16+
tests, and will create a separate CTest test for each Catch test case. Note
17+
that this is in some cases less efficient, as common set-up and tear-down logic
18+
cannot be shared by multiple test cases executing in the same instance.
19+
However, it provides more fine-grained pass/fail information to CTest, which is
20+
usually considered as more beneficial. By default, the CTest test name is the
21+
same as the Catch name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``.
22+
23+
.. command:: catch_discover_tests
24+
25+
Automatically add tests with CTest by querying the compiled test executable
26+
for available tests::
27+
28+
catch_discover_tests(target
29+
[TEST_SPEC arg1...]
30+
[EXTRA_ARGS arg1...]
31+
[WORKING_DIRECTORY dir]
32+
[TEST_PREFIX prefix]
33+
[TEST_SUFFIX suffix]
34+
[PROPERTIES name1 value1...]
35+
[TEST_LIST var]
36+
)
37+
38+
``catch_discover_tests`` sets up a post-build command on the test executable
39+
that generates the list of tests by parsing the output from running the test
40+
with the ``--list-test-names-only`` argument. This ensures that the full
41+
list of tests is obtained. Since test discovery occurs at build time, it is
42+
not necessary to re-run CMake when the list of tests changes.
43+
However, it requires that :prop_tgt:`CROSSCOMPILING_EMULATOR` is properly set
44+
in order to function in a cross-compiling environment.
45+
46+
Additionally, setting properties on tests is somewhat less convenient, since
47+
the tests are not available at CMake time. Additional test properties may be
48+
assigned to the set of tests as a whole using the ``PROPERTIES`` option. If
49+
more fine-grained test control is needed, custom content may be provided
50+
through an external CTest script using the :prop_dir:`TEST_INCLUDE_FILES`
51+
directory property. The set of discovered tests is made accessible to such a
52+
script via the ``<target>_TESTS`` variable.
53+
54+
The options are:
55+
56+
``target``
57+
Specifies the Catch executable, which must be a known CMake executable
58+
target. CMake will substitute the location of the built executable when
59+
running the test.
60+
61+
``TEST_SPEC arg1...``
62+
Specifies test cases, wildcarded test cases, tags and tag expressions to
63+
pass to the Catch executable with the ``--list-test-names-only`` argument.
64+
65+
``EXTRA_ARGS arg1...``
66+
Any extra arguments to pass on the command line to each test case.
67+
68+
``WORKING_DIRECTORY dir``
69+
Specifies the directory in which to run the discovered test cases. If this
70+
option is not provided, the current binary directory is used.
71+
72+
``TEST_PREFIX prefix``
73+
Specifies a ``prefix`` to be prepended to the name of each discovered test
74+
case. This can be useful when the same test executable is being used in
75+
multiple calls to ``catch_discover_tests()`` but with different
76+
``TEST_SPEC`` or ``EXTRA_ARGS``.
77+
78+
``TEST_SUFFIX suffix``
79+
Similar to ``TEST_PREFIX`` except the ``suffix`` is appended to the name of
80+
every discovered test case. Both ``TEST_PREFIX`` and ``TEST_SUFFIX`` may
81+
be specified.
82+
83+
``PROPERTIES name1 value1...``
84+
Specifies additional properties to be set on all tests discovered by this
85+
invocation of ``catch_discover_tests``.
86+
87+
``TEST_LIST var``
88+
Make the list of tests available in the variable ``var``, rather than the
89+
default ``<target>_TESTS``. This can be useful when the same test
90+
executable is being used in multiple calls to ``catch_discover_tests()``.
91+
Note that this variable is only available in CTest.
92+
93+
#]=======================================================================]
94+
95+
#------------------------------------------------------------------------------
96+
function(catch_discover_tests TARGET)
97+
cmake_parse_arguments(
98+
""
99+
""
100+
"TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST"
101+
"TEST_SPEC;EXTRA_ARGS;PROPERTIES"
102+
${ARGN}
103+
)
104+
105+
if(NOT _WORKING_DIRECTORY)
106+
set(_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
107+
endif()
108+
if(NOT _TEST_LIST)
109+
set(_TEST_LIST ${TARGET}_TESTS)
110+
endif()
111+
112+
## Generate a unique name based on the extra arguments
113+
string(SHA1 args_hash "${_TEST_SPEC} ${_EXTRA_ARGS}")
114+
string(SUBSTRING ${args_hash} 0 7 args_hash)
115+
116+
# Define rule to generate test list for aforementioned test executable
117+
set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_include-${args_hash}.cmake")
118+
set(ctest_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_tests-${args_hash}.cmake")
119+
get_property(crosscompiling_emulator
120+
TARGET ${TARGET}
121+
PROPERTY CROSSCOMPILING_EMULATOR
122+
)
123+
add_custom_command(
124+
TARGET ${TARGET} POST_BUILD
125+
BYPRODUCTS "${ctest_tests_file}"
126+
COMMAND "${CMAKE_COMMAND}"
127+
-D "TEST_TARGET=${TARGET}"
128+
-D "TEST_EXECUTABLE=$<TARGET_FILE:${TARGET}>"
129+
-D "TEST_EXECUTOR=${crosscompiling_emulator}"
130+
-D "TEST_WORKING_DIR=${_WORKING_DIRECTORY}"
131+
-D "TEST_SPEC=${_TEST_SPEC}"
132+
-D "TEST_EXTRA_ARGS=${_EXTRA_ARGS}"
133+
-D "TEST_PROPERTIES=${_PROPERTIES}"
134+
-D "TEST_PREFIX=${_TEST_PREFIX}"
135+
-D "TEST_SUFFIX=${_TEST_SUFFIX}"
136+
-D "TEST_LIST=${_TEST_LIST}"
137+
-D "CTEST_FILE=${ctest_tests_file}"
138+
-P "${_CATCH_DISCOVER_TESTS_SCRIPT}"
139+
VERBATIM
140+
)
141+
142+
file(WRITE "${ctest_include_file}"
143+
"if(EXISTS \"${ctest_tests_file}\")\n"
144+
" include(\"${ctest_tests_file}\")\n"
145+
"else()\n"
146+
" add_test(${TARGET}_NOT_BUILT-${args_hash} ${TARGET}_NOT_BUILT-${args_hash})\n"
147+
"endif()\n"
148+
)
149+
150+
if(NOT ${CMAKE_VERSION} VERSION_LESS "3.10.0")
151+
# Add discovered tests to directory TEST_INCLUDE_FILES
152+
set_property(DIRECTORY
153+
APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}"
154+
)
155+
else()
156+
# Add discovered tests as directory TEST_INCLUDE_FILE if possible
157+
get_property(test_include_file_set DIRECTORY PROPERTY TEST_INCLUDE_FILE SET)
158+
if (NOT ${test_include_file_set})
159+
set_property(DIRECTORY
160+
PROPERTY TEST_INCLUDE_FILE "${ctest_include_file}"
161+
)
162+
else()
163+
message(FATAL_ERROR
164+
"Cannot set more than one TEST_INCLUDE_FILE"
165+
)
166+
endif()
167+
endif()
168+
169+
endfunction()
170+
171+
###############################################################################
172+
173+
set(_CATCH_DISCOVER_TESTS_SCRIPT
174+
${CMAKE_CURRENT_LIST_DIR}/CatchAddTests.cmake
175+
)

cmake/CatchAddTests.cmake

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2+
# file Copyright.txt or https://cmake.org/licensing for details.
3+
4+
set(prefix "${TEST_PREFIX}")
5+
set(suffix "${TEST_SUFFIX}")
6+
set(spec ${TEST_SPEC})
7+
set(extra_args ${TEST_EXTRA_ARGS})
8+
set(properties ${TEST_PROPERTIES})
9+
set(script)
10+
set(suite)
11+
set(tests)
12+
13+
function(add_command NAME)
14+
set(_args "")
15+
foreach(_arg ${ARGN})
16+
if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
17+
set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
18+
else()
19+
set(_args "${_args} ${_arg}")
20+
endif()
21+
endforeach()
22+
set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
23+
endfunction()
24+
25+
# Run test executable to get list of available tests
26+
if(NOT EXISTS "${TEST_EXECUTABLE}")
27+
message(FATAL_ERROR
28+
"Specified test executable '${TEST_EXECUTABLE}' does not exist"
29+
)
30+
endif()
31+
execute_process(
32+
COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-names-only
33+
OUTPUT_VARIABLE output
34+
RESULT_VARIABLE result
35+
)
36+
# Catch --list-test-names-only reports the number of tests, so 0 is... surprising
37+
if(${result} EQUAL 0)
38+
message(WARNING
39+
"Test executable '${TEST_EXECUTABLE}' contains no tests!\n"
40+
)
41+
elseif(${result} LESS 0)
42+
message(FATAL_ERROR
43+
"Error running test executable '${TEST_EXECUTABLE}':\n"
44+
" Result: ${result}\n"
45+
" Output: ${output}\n"
46+
)
47+
endif()
48+
49+
string(REPLACE "\n" ";" output "${output}")
50+
51+
# Parse output
52+
foreach(line ${output})
53+
# Test name; strip spaces to get just the name...
54+
string(REGEX REPLACE " +" "" test "${line}")
55+
# ...and add to script
56+
add_command(add_test
57+
"${prefix}${test}${suffix}"
58+
${TEST_EXECUTOR}
59+
"${TEST_EXECUTABLE}"
60+
"${test}"
61+
${extra_args}
62+
)
63+
add_command(set_tests_properties
64+
"${prefix}${test}${suffix}"
65+
PROPERTIES
66+
WORKING_DIRECTORY "${TEST_WORKING_DIR}"
67+
${properties}
68+
)
69+
list(APPEND tests "${prefix}${test}${suffix}")
70+
endforeach()
71+
72+
# Create a list of all discovered tests, which users may use to e.g. set
73+
# properties on the tests
74+
add_command(set ${TEST_LIST} ${tests})
75+
76+
# Write CTest script
77+
file(WRITE "${CTEST_FILE}" "${script}")

0 commit comments

Comments
 (0)