discover_tests: Add tests

This commit is contained in:
Daniel Pfeifer authored and Brad King committed 2026-03-26 10:36:21 -04:00
1 parent 3947842733
commit 969999fa9f
20 files changed
+277

No files matched your search

+1
View File
@@ -413,6 +413,7 @@ add_RunCMake_test(CpsExportImportBuild)
add_RunCMake_test(CpsExportImportInstall)
add_RunCMake_test(CpsMapConfig)
add_RunCMake_test(DisallowedCommands)
add_RunCMake_test(discover_tests)
if("${CMAKE_GENERATOR}" MATCHES "Unix Makefiles|Ninja|FASTBuild")
add_RunCMake_test(ExportCompileCommands)
endif()
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.10)
project(${RunCMake_TEST} LANGUAGES NONE)
include(${RunCMake_TEST}.cmake)
@@ -0,0 +1,7 @@
Test #[0-9]*: ExpandLists.OFF.case_one;two
Test #[0-9]*: ExpandLists.OFF.case_three
Test #[0-9]*: ExpandLists.ON.case_one
Test #[0-9]*: ExpandLists.ON.case_two
Test #[0-9]*: ExpandLists.ON.case_three
Total Tests: 5
@@ -0,0 +1,7 @@
Start [0-9]*: Env.case_two
.*
100% tests passed, 0 tests failed out of 1
Label Time Summary:
FOOBAR
@@ -0,0 +1,4 @@
Test #[0-9]*: DT.case_one
Test #[0-9]*: DT.case_three
Total Tests: 2
@@ -0,0 +1,37 @@
include(RunCMake)
# Isolate our ctest runs from external environment.
unset(ENV{CTEST_PARALLEL_LEVEL})
unset(ENV{CTEST_OUTPUT_ON_FAILURE})
if(NOT RunCMake_GENERATOR_IS_MULTI_CONFIG)
set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
endif()
block()
set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/basic-build")
run_cmake(basic)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(basic-build ${CMAKE_COMMAND} --build . --config Debug)
run_cmake_command(PROPERTY ${CMAKE_CTEST_COMMAND} -C Debug -N -L LBL1)
set(ENV{TEST_LABEL} "FOO")
run_cmake_command(ENVIRONMENT ${CMAKE_CTEST_COMMAND} -C Debug -R "Env.")
run_cmake_command(COMMAND_EXPAND_LISTS
${CMAKE_CTEST_COMMAND} -C Debug -N -R "ExpandLists.")
endblock()
function(run_case CASE)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${CASE}-build)
run_cmake(${CASE})
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(${CASE}-build ${CMAKE_COMMAND} --build . --config Debug)
run_cmake_command(${CASE}-test ${CMAKE_CTEST_COMMAND} -C Debug)
endfunction()
run_case(bad-command)
run_case(bad-regex)
run_case(discovery-failure)
run_case(discovery-timeout)
@@ -0,0 +1 @@
8
@@ -0,0 +1,4 @@
CMake Error at .* \(discover_tests\):
discover_tests failed to run command:.*
no such file or directory
@@ -0,0 +1,11 @@
enable_language(C)
enable_testing()
add_executable(fake_discovery fake_discovery.c)
discover_tests(COMMAND fake_discovery_notfound
DISCOVERY_ARGS --list
DISCOVERY_MATCH "^([^,]+),([^,]+)$"
TEST_NAME "DT.\\1"
TEST_ARGS --run "\\1"
)
@@ -0,0 +1 @@
8
@@ -0,0 +1,2 @@
CMake Error at .* \(discover_tests\):
discover_tests DISCOVERY_MATCH failed to compile regex "\(".
@@ -0,0 +1,11 @@
enable_language(C)
enable_testing()
add_executable(fake_discovery fake_discovery.c)
discover_tests(COMMAND fake_discovery
DISCOVERY_ARGS --list
DISCOVERY_MATCH "("
TEST_NAME "DT.\\1"
TEST_ARGS --run "\\1"
)
+39
View File
@@ -0,0 +1,39 @@
enable_language(C)
enable_testing()
add_executable(fake_discovery fake_discovery.c)
discover_tests(COMMAND fake_discovery
DISCOVERY_ARGS --list_tests
DISCOVERY_MATCH "^([^,]+),([^,]+)$"
TEST_NAME "DT.\\1"
TEST_ARGS "\\1"
TEST_PROPERTIES
LABELS "\\2"
)
discover_tests(COMMAND fake_discovery
DISCOVERY_ARGS --list_env
DISCOVERY_MATCH "^([^,]+),([^,]+)$"
DISCOVERY_PROPERTIES
ENVIRONMENT "TEST_NAME=two"
ENVIRONMENT_MODIFICATION "TEST_LABEL=string_append:BAR"
TEST_NAME "Env.\\1"
TEST_ARGS "\\1"
TEST_PROPERTIES
LABELS "\\2"
)
discover_tests(COMMAND fake_discovery --list_args "one;two"
DISCOVERY_ARGS "three"
DISCOVERY_MATCH ".*"
TEST_NAME "ExpandLists.OFF.\\0"
TEST_ARGS ""
)
discover_tests(COMMAND fake_discovery --list_args "one;two" COMMAND_EXPAND_LISTS
DISCOVERY_ARGS "three"
DISCOVERY_MATCH ".*"
TEST_NAME "ExpandLists.ON.\\0"
TEST_ARGS ""
)
@@ -0,0 +1 @@
8
@@ -0,0 +1,4 @@
CMake Error at .* \(discover_tests\):
discover_tests failed to run command:.*
discovery failure
@@ -0,0 +1,13 @@
enable_language(C)
enable_testing()
add_executable(fake_discovery fake_discovery.c)
discover_tests(COMMAND fake_discovery
DISCOVERY_ARGS --list_fail
DISCOVERY_MATCH "^([^,]+),([^,]+)$"
TEST_NAME "DT.\\1"
TEST_ARGS "\\1"
TEST_PROPERTIES
LABELS "\\2"
)
@@ -0,0 +1 @@
8
@@ -0,0 +1,4 @@
CMake Error at .* \(discover_tests\):
discover_tests failed to run command:.*
Process terminated due to timeout
@@ -0,0 +1,15 @@
enable_language(C)
enable_testing()
add_executable(fake_discovery fake_discovery.c)
discover_tests(COMMAND fake_discovery
DISCOVERY_ARGS --list_timeout
DISCOVERY_MATCH "^([^,]+),([^,]+)$"
DISCOVERY_PROPERTIES
TIMEOUT "0.1"
TEST_NAME "DT.\\1"
TEST_ARGS "\\1"
TEST_PROPERTIES
LABELS "\\2"
)
@@ -0,0 +1,111 @@
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#if defined(_MSC_VER)
# define _CRT_SECURE_NO_DEPRECATE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
# include <windows.h>
#else
# include <unistd.h>
#endif
static int list_tests(int ac, char** av)
{
(void)ac, (void)av;
printf("this line should not match\n");
printf("case_one,LBL1\n");
printf("\n");
printf("case_two,LBL2\n");
printf("garbage,still,has,commas\n");
printf("case_three,LBL1\n");
return 0;
}
static int list_fail(int ac, char** av)
{
(void)ac, (void)av;
fprintf(stderr, "discovery failure\n");
return -1;
}
static int list_timeout(int ac, char** av)
{
(void)ac, (void)av;
printf("case_one,LBL1\n");
#if defined(_WIN32)
Sleep(5000);
#else
sleep(5);
#endif
return 0;
}
static int list_args(int ac, char** av)
{
int i;
for (i = 0; i < ac; i++) {
printf("case_%s\n", av[i]);
}
return 0;
}
static int list_env(int ac, char** av)
{
(void)ac, (void)av;
printf("case_%s,%s\n", getenv("TEST_NAME"), getenv("TEST_LABEL"));
return 0;
}
static int success(int ac, char** av)
{
(void)ac, (void)av;
return 0;
}
typedef int (*function_ptr)(int, char**);
struct function_entry
{
char const* name;
function_ptr function;
};
struct function_entry const function_map[] = {
{ "--list_tests", list_tests },
{ "--list_fail", list_fail },
{ "--list_timeout", list_timeout },
{ "--list_args", list_args },
{ "--list_env", list_env },
{ "case_one", success },
{ "case_two", success },
{ "case_three", success },
{ NULL, NULL },
};
static function_ptr lookup(char const* name)
{
struct function_entry const* it = function_map;
for (; it->name != NULL; ++it) {
if (strcmp(name, it->name) == 0) {
return it->function;
}
}
return NULL;
}
int main(int argc, char** argv)
{
function_ptr fn = argc > 1 ? lookup(argv[1]) : NULL;
if (fn == NULL) {
fprintf(stderr, "invalid argument\n");
return -1;
}
return fn(argc - 2, argv + 2);
}