Skip to content

Commit 221016f

Browse files
committed
subdir tests
1 parent cb9fdba commit 221016f

File tree

6 files changed

+164
-79
lines changed

6 files changed

+164
-79
lines changed

test/CMakeLists.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ target_link_libraries(boost_redis_src PRIVATE boost_redis_project_options)
1515
add_library(boost_redis_tests_common STATIC common.cpp)
1616
target_compile_features(boost_redis_tests_common PRIVATE cxx_std_17)
1717
target_link_libraries(boost_redis_tests_common PRIVATE boost_redis_project_options)
18-
if (NOT BOOST_REDIS_MAIN_PROJECT)
19-
target_link_libraries(boost_redis_tests_common PUBLIC Boost::unit_test_framework)
20-
endif()
2118

2219
macro(make_test TEST_NAME STANDARD)
23-
add_executable(${TEST_NAME} ${TEST_NAME}.cpp)
24-
target_link_libraries(${TEST_NAME} PRIVATE boost_redis_src boost_redis_tests_common)
25-
target_link_libraries(${TEST_NAME} PRIVATE boost_redis_project_options)
26-
target_compile_features(${TEST_NAME} PRIVATE cxx_std_${STANDARD})
27-
add_test(${TEST_NAME} ${TEST_NAME})
20+
set(EXE_NAME "boost_redis_${TEST_NAME}")
21+
add_executable(${EXE_NAME} ${TEST_NAME}.cpp)
22+
target_link_libraries(${EXE_NAME} PRIVATE
23+
boost_redis_src
24+
boost_redis_tests_common
25+
boost_redis_project_options
26+
)
27+
target_compile_features(${EXE_NAME} PRIVATE cxx_std_${STANDARD})
28+
add_test(${EXE_NAME} ${EXE_NAME})
2829
endmacro()
2930

3031
make_test(test_conn_quit 17)

test/cmake_b2_test/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.5...3.22)
2+
3+
project(boost_redis_b2_test LANGUAGES CXX)
4+
5+
find_package(Boost REQUIRED COMPONENTS headers)
6+
find_package(Threads REQUIRED)
7+
find_package(OpenSSL REQUIRED)
8+
9+
add_executable(main main.cpp)
10+
target_link_libraries(main PRIVATE Boost::headers Threads::Threads OpenSSL::Crypto OpenSSL::SSL)
11+
12+
include(CTest)
13+
add_test(NAME main COMMAND main)

test/cmake_b2_test/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva ([email protected])
2+
*
3+
* Distributed under the Boost Software License, Version 1.0. (See
4+
* accompanying file LICENSE.txt)
5+
*/
6+
7+
8+
#include <boost/redis/connection.hpp>
9+
#include <boost/redis/src.hpp>
10+
11+
int main()
12+
{
13+
boost::redis::connection conn(boost::asio::system_executor{});
14+
return static_cast<int>(!conn.will_reconnect());
15+
}

test/cmake_test/CMakeLists.txt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
cmake_minimum_required(VERSION 3.5...3.22)
2+
3+
project(cmake_subdir_test LANGUAGES CXX)
4+
5+
option(BOOST_CI_INSTALL_TEST "Whether to build install or add_subdirectory tests" OFF)
6+
7+
if(BOOST_CI_INSTALL_TEST)
8+
find_package(boost_redis REQUIRED)
9+
else()
10+
# Generated by boostdep --brief redis
11+
set(_DEPENDENCIES
12+
# Primary dependencies
13+
asio
14+
assert
15+
core
16+
mp11
17+
system
18+
throw_exception
19+
20+
# Secondary dependencies
21+
align
22+
array
23+
bind
24+
chrono
25+
config
26+
context
27+
coroutine
28+
date_time
29+
exception
30+
"function"
31+
regex
32+
smart_ptr
33+
type_traits
34+
utility
35+
static_assert
36+
variant2
37+
winapi
38+
integer
39+
move
40+
mpl
41+
predef
42+
ratio
43+
typeof
44+
pool
45+
algorithm
46+
io
47+
lexical_cast
48+
numeric/conversion
49+
range
50+
tokenizer
51+
tuple
52+
preprocessor
53+
concept_check
54+
container_hash
55+
iterator
56+
unordered
57+
describe
58+
container
59+
conversion
60+
detail
61+
optional
62+
rational
63+
intrusive
64+
function_types
65+
fusion
66+
functional
67+
)
68+
69+
# Build our dependencies, so the targets Boost::xxx are defined
70+
set(_BOOST_ROOT ../../../..)
71+
foreach(_DEPENDENCY IN LISTS _DEPENDENCIES)
72+
add_subdirectory(${_BOOST_ROOT}/libs/${_DEPENDENCY} boostorg/${_DEPENDENCY})
73+
endforeach()
74+
75+
# Build our project
76+
add_subdirectory(${_BOOST_ROOT}/libs/redis boostorg/redis)
77+
endif()
78+
79+
# Copied from Alexander Grund's Boost.CI
80+
add_executable(main main.cpp)
81+
target_link_libraries(main PRIVATE Boost::redis)
82+
83+
include(CTest)
84+
add_test(NAME main COMMAND main)
85+
86+
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C $<CONFIG>)

test/cmake_test/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva ([email protected])
2+
*
3+
* Distributed under the Boost Software License, Version 1.0. (See
4+
* accompanying file LICENSE.txt)
5+
*/
6+
7+
8+
#include <boost/redis/connection.hpp>
9+
#include <boost/redis/src.hpp>
10+
11+
int main()
12+
{
13+
boost::redis::connection conn(boost::asio::system_executor{});
14+
return static_cast<int>(!conn.will_reconnect());
15+
}

tools/ci.py

100644100755
Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111

1212
_is_windows = os.name == 'nt'
13-
_boost_root = Path(os.path.expanduser('~')).joinpath('boost-root')
13+
_home = Path(os.path.expanduser('~'))
14+
_boost_root = _home.joinpath('boost-root')
15+
_b2_distro = _home.joinpath('boost-b2-distro')
16+
_cmake_distro = _home.joinpath('boost-cmake-distro')
1417
_b2_command = str(_boost_root.joinpath('b2'))
1518

1619

@@ -68,7 +71,10 @@ def _deduce_boost_branch() -> str:
6871
return res
6972

7073

71-
def _install_boost(
74+
# Gets Boost (develop or master, depending on the CI branch we're operating on),
75+
# with the required dependencies, and leaves it at _boost_root. Places our library,
76+
# located under source_dir, under $BOOST_ROOT/libs. Also runs the bootstrap script so b2 is usable.
77+
def _setup_boost(
7278
source_dir: Path
7379
) -> None:
7480
assert source_dir.is_absolute()
@@ -103,21 +109,22 @@ def _install_boost(
103109
_run([_b2_command, 'headers'])
104110

105111

106-
def _build_b2_distro(
107-
install_prefix: Path
108-
):
112+
# Builds a Boost distribution using ./b2 install, and places it into _b2_distro.
113+
# This emulates a regular Boost distribution, like the ones in releases
114+
def _build_b2_distro():
109115
os.chdir(str(_boost_root))
110116
_run([
111117
_b2_command,
112-
'--prefix={}'.format(install_prefix),
118+
'--prefix={}'.format(_b2_distro),
113119
'--with-system',
114120
'-d0',
115121
'install'
116122
])
117123

118124

125+
# Builds a Boost distribution using cmake, and places it into _cmake_distro.
126+
# It includes only our library and any dependency.
119127
def _build_cmake_distro(
120-
install_prefix: Path,
121128
generator: str,
122129
build_type: str,
123130
cxxstd: str,
@@ -133,7 +140,7 @@ def _build_cmake_distro(
133140
'-DCMAKE_CXX_STANDARD={}'.format(cxxstd),
134141
'-DBOOST_INCLUDE_LIBRARIES=redis',
135142
'-DBUILD_SHARED_LIBS={}'.format(_cmake_bool(build_shared_libs)),
136-
'-DCMAKE_INSTALL_PREFIX={}'.format(install_prefix),
143+
'-DCMAKE_INSTALL_PREFIX={}'.format(_cmake_distro),
137144
'-DBUILD_TESTING=ON',
138145
'-DBoost_VERBOSE=ON',
139146
'-DCMAKE_INSTALL_MESSAGE=NEVER',
@@ -144,8 +151,10 @@ def _build_cmake_distro(
144151
_run(['cmake', '--build', '.', '--target', 'install', '--config', build_type])
145152

146153

154+
# Builds and runs our CMake tests as a standalone project
155+
# (BOOST_REDIS_MAIN_PROJECT is ON) and we find_package Boost.
156+
# This ensures that all our test suite is run.
147157
def _run_cmake_standalone_tests(
148-
b2_distro: Path,
149158
generator: str,
150159
build_type: str,
151160
cxxstd: str,
@@ -155,7 +164,7 @@ def _run_cmake_standalone_tests(
155164
_run([
156165
'cmake',
157166
'-DBUILD_TESTING=ON',
158-
'-DCMAKE_PREFIX_PATH={}'.format(_build_prefix_path(b2_distro)),
167+
'-DCMAKE_PREFIX_PATH={}'.format(_build_prefix_path(_b2_distro)),
159168
'-DCMAKE_BUILD_TYPE={}'.format(build_type),
160169
'-DBUILD_SHARED_LIBS={}'.format(_cmake_bool(build_shared_libs)),
161170
'-DCMAKE_CXX_STANDARD={}'.format(cxxstd),
@@ -167,6 +176,7 @@ def _run_cmake_standalone_tests(
167176
_run(['ctest', '--output-on-failure', '--build-config', build_type, '--no-tests=error'])
168177

169178

179+
# Tests that the library can be consumed using add_subdirectory()
170180
def _run_cmake_add_subdirectory_tests(
171181
generator: str,
172182
build_type: str,
@@ -188,8 +198,8 @@ def _run_cmake_add_subdirectory_tests(
188198
_run(['ctest', '--output-on-failure', '--build-config', build_type, '--no-tests=error'])
189199

190200

201+
# Tests that the library can be consumed using find_package on a distro built by cmake
191202
def _run_cmake_find_package_tests(
192-
cmake_distro: Path,
193203
generator: str,
194204
build_type: str,
195205
build_shared_libs: bool = False
@@ -203,15 +213,15 @@ def _run_cmake_find_package_tests(
203213
'-DBOOST_CI_INSTALL_TEST=ON',
204214
'-DCMAKE_BUILD_TYPE={}'.format(build_type),
205215
'-DBUILD_SHARED_LIBS={}'.format(_cmake_bool(build_shared_libs)),
206-
'-DCMAKE_PREFIX_PATH={}'.format(_build_prefix_path(cmake_distro)),
216+
'-DCMAKE_PREFIX_PATH={}'.format(_build_prefix_path(_cmake_distro)),
207217
'..'
208218
])
209219
_run(['cmake', '--build', '.', '--config', build_type])
210220
_run(['ctest', '--output-on-failure', '--build-config', build_type, '--no-tests=error'])
211221

212222

223+
# Tests that the library can be consumed using find_package on a distro built by b2
213224
def _run_cmake_b2_find_package_tests(
214-
b2_distro: Path,
215225
generator: str,
216226
build_type: str,
217227
build_shared_libs: bool = False
@@ -222,7 +232,7 @@ def _run_cmake_b2_find_package_tests(
222232
'-G',
223233
generator,
224234
'-DBUILD_TESTING=ON',
225-
'-DCMAKE_PREFIX_PATH={}'.format(_build_prefix_path(b2_distro)),
235+
'-DCMAKE_PREFIX_PATH={}'.format(_build_prefix_path(_b2_distro)),
226236
'-DCMAKE_BUILD_TYPE={}'.format(build_type),
227237
'-DBUILD_SHARED_LIBS={}'.format(_cmake_bool(build_shared_libs)),
228238
'-DBUILD_TESTING=ON',
@@ -232,68 +242,25 @@ def _run_cmake_b2_find_package_tests(
232242
_run(['ctest', '--output-on-failure', '--build-config', build_type, '--no-tests=error'])
233243

234244

235-
def _run_b2_tests(
236-
toolset: str,
237-
cxxstd: str,
238-
variant: str,
239-
stdlib: str = 'native',
240-
address_model: str = '64',
241-
address_sanitizer: bool = False,
242-
undefined_sanitizer: bool = False,
243-
):
244-
os.chdir(str(_boost_root))
245-
_run([
246-
_b2_command,
247-
'--abbreviate-paths',
248-
'toolset={}'.format(toolset),
249-
'cxxstd={}'.format(cxxstd),
250-
'address-model={}'.format(address_model),
251-
'variant={}'.format(variant),
252-
'stdlib={}'.format(stdlib),
253-
] + (['address-sanitizer=norecover'] if address_sanitizer else []) # can only be disabled by omitting the arg
254-
+ (['undefined-sanitizer=norecover'] if undefined_sanitizer else []) # can only be disabled by omitting the arg
255-
+ [
256-
'warnings-as-errors=on',
257-
'-j4',
258-
'libs/redis/test',
259-
'libs/redis/example'
260-
])
261-
262-
# Get Boost
263-
# Generate "pre-built" b2 distro
264-
# Build the library, run the tests, and install, from the superproject
265-
# Library tests, using the b2 Boost distribution generated before (this tests our normal dev workflow)
266-
# Subdir tests, using add_subdirectory() (lib can be consumed using add_subdirectory)
267-
# Subdir tests, using find_package with the library installed in the previous step
268-
# (library can be consumed using find_package on a distro built by cmake)
269-
270-
# Subdir tests, using find_package with the b2 distribution
271-
# (library can be consumed using find_package on a distro built by b2)
272-
273-
274-
275245
def main():
276246
parser = argparse.ArgumentParser()
277247
subparsers = parser.add_subparsers()
278248

279-
subp = subparsers.add_parser('install-boost')
249+
subp = subparsers.add_parser('setup-boost')
280250
subp.add_argument('--source-dir', type=Path, required=True)
281-
subp.set_defaults(func=_install_boost)
251+
subp.set_defaults(func=_setup_boost)
282252

283253
subp = subparsers.add_parser('build-b2-distro')
284-
subp.add_argument('--install-prefix', type=Path, required=True)
285254
subp.set_defaults(func=_build_b2_distro)
286255

287256
subp = subparsers.add_parser('build-cmake-distro')
288-
subp.add_argument('--install-prefix', type=Path, required=True)
289257
subp.add_argument('--generator', default='Unix Makefiles')
290258
subp.add_argument('--build-type', default='Debug')
291259
subp.add_argument('--cxxstd', default='20')
292260
subp.add_argument('--build-shared-libs', type=_str2bool, default=False)
293261
subp.set_defaults(func=_build_cmake_distro)
294262

295263
subp = subparsers.add_parser('run-cmake-standalone-tests')
296-
subp.add_argument('--b2-distro', type=Path, required=True)
297264
subp.add_argument('--generator', default='Unix Makefiles')
298265
subp.add_argument('--build-type', default='Debug')
299266
subp.add_argument('--cxxstd', default='20')
@@ -307,29 +274,17 @@ def main():
307274
subp.set_defaults(func=_run_cmake_add_subdirectory_tests)
308275

309276
subp = subparsers.add_parser('run-cmake-find-package-tests')
310-
subp.add_argument('--cmake-distro', type=Path, required=True)
311277
subp.add_argument('--generator', default='Unix Makefiles')
312278
subp.add_argument('--build-type', default='Debug')
313279
subp.add_argument('--build-shared-libs', type=_str2bool, default=False)
314280
subp.set_defaults(func=_run_cmake_find_package_tests)
315281

316282
subp = subparsers.add_parser('run-cmake-b2-find-package-tests')
317-
subp.add_argument('--cmake-distro', type=Path, required=True)
318283
subp.add_argument('--generator', default='Unix Makefiles')
319284
subp.add_argument('--build-type', default='Debug')
320285
subp.add_argument('--build-shared-libs', type=_str2bool, default=False)
321286
subp.set_defaults(func=_run_cmake_b2_find_package_tests)
322287

323-
subp = subparsers.add_parser('run-b2-tests')
324-
subp.add_argument('--toolset', required=True)
325-
subp.add_argument('--cxxstd', default='20')
326-
subp.add_argument('--variant', default='debug,release')
327-
subp.add_argument('--stdlib', default='native')
328-
subp.add_argument('--address-model', default='64')
329-
subp.add_argument('--address-sanitizer', type=_str2bool, default=False)
330-
subp.add_argument('--undefined-sanitizer', type=_str2bool, default=False)
331-
subp.set_defaults(func=_run_b2_tests)
332-
333288
args = parser.parse_args()
334289

335290
os.environ['CMAKE_BUILD_PARALLEL_LEVEL'] = '4'

0 commit comments

Comments
 (0)