Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/extras/Catch3ShardTestsImpl.cmake
blob: 99fd900afca2f6d608afe7c3c95ff1cd4ac4418f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#              Copyright Catch2 Authors
# Distributed under the Boost Software License, Version 1.0.
#   (See accompanying file LICENSE.txt or copy at
#        https://www.boost.org/LICENSE_1_0.txt)

# SPDX-License-Identifier: BSL-1.0

# Indirection for CatchShardTests that allows us to delay the script
# file generation until build time.

# Expected args:
#  * TEST_BINARY - full path to the test binary to run sharded
#  * CTEST_FILE  - full path to ctest script file to write to
#  * TARGET_NAME - name of the target to shard (used for test names)
#  * SHARD_COUNT - number of shards to split the binary into
# Optional args:
#  * REPORTER_SPEC - report options passed to the binary as they are, such
#                    as --junit=FILE
#  * TEST_SPEC     - test spec to pass down to the test binary

if(NOT EXISTS "${TEST_BINARY}")
  message(FATAL_ERROR
    "Specified test binary '${TEST_BINARY}' does not exist"
  )
endif()

set(other_args "")
if(TEST_SPEC)
  set(other_args "${other_args} ${TEST_SPEC}")
endif()
if(REPORTER_SPEC)
  set(other_args "${other_args} ${REPORTER_SPEC}")
endif()

# foreach RANGE in cmake is inclusive of the end, so we have to adjust it
math(EXPR adjusted_shard_count "${SHARD_COUNT} - 1")

# The runner assigns tests to shards by a stable hash of their names, so the
# shards cover every test once whatever the seed; the shared seed keeps their
# random numbers the same.
file(WRITE "${CTEST_FILE}"
  "string(RANDOM LENGTH 9 ALPHABET \"0123456789\" rng_seed)\n"
  "\n"
  "foreach(shard_idx RANGE ${adjusted_shard_count})\n"
  "  add_test(${TARGET_NAME}-shard-" [[${shard_idx}]] "/${adjusted_shard_count}\n"
  "    ${TEST_BINARY}"
  " --shard=" [[${shard_idx}]] "/${SHARD_COUNT}"
  " --seed=" [[${rng_seed}]]
  " --order=random"
  "${other_args}"
  "\n"
  "  )\n"
  "endforeach()\n"
)