[Bug]: Death tests don't work with emscripten
- Dominant language
- C++
- Stars
- 39.5k
- Forks
- 10.9k
- Avg merge
- 6d 13h
- Merged PRs (30d)
- 1
Description
### Describe the issue
`GTEST_DEATH_TEST` et. al. are not defined under Emscripten. If you configure with `emcmake cmake` and run the tests with ctest, there are a number of failures (I tried to run the tests with bazel under emscripten, but couldn't figure out how to do that). The one I care about is the first one:
```
Start 1: googletest-death-test-test
Process not started
/Users/dave/src/googletest/.build/googletest/googletest-death-test-test.js
[permission denied]
1/45 Test #1: googletest-death-test-test .................***Not Run 0.00 sec
```
I realize that the wasm environment might not support the `clone()` or `fork()` calls needed to catch death tests in the generated test executable. If that is the obstacle, there's another way: when building with emscripten, create individual `ctest` death tests that check for the death of commands that launch a single death test from the test executable. Presumably bazel has its own test driver that could be leveraged in the same way.
For example, I use this CMake function when targeting emscripten:
```cmake
# Turns all tests discovered in by
# gtest_discover_tests that are named with the "DeathTest" convention
# into CMake WILL_FAIL tests.
#
# It is expected that under emscripten, these tests are actually
# compiled as non-death tests; i.e. they simply execute the code that
# is expected to die. There is no support for checking that the exit
# code or output meets expectations.
function(handle_emscripten_death_tests test_executable)
set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${test_executable}_death.cmake")
file(WRITE "${ctest_include_file}"
"set(death_tests \"\${${test_executable}_TESTS}}\")\n"
"list(FILTER death_tests INCLUDE REGEX \"DeathTest\")\n"
"set_tests_properties(\${death_tests} PROPERTIES WILL_FAIL YES)\n"
)
set_property(DIRECTORY
APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}"
)
endfunction()
```
### Steps to reproduce the problem
```
emcmake cmake -GNinja -DGOOGLETEST_VERSION=1.15.2 -Dgtest_build_tests=ON -S . -B build
cmake --build build
ctest --output-on-failure --test-dir build
```
### What version of GoogleTest are you using?
ff233bdd4cac0a0bf6e5cd45bda3406814cb2796
### What operating system and version are you using?
MacOS Sonoma 14.5 (23F79)
### What compiler and version are you using?
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.64-git
clang version 19.0.0git
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/emscripten/3.1.64/libexec/llvm/bin
### What build system are you using?
cmake version 3.30.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
### Additional context
_No response_
Contributor guide
Research direction
Reproduce the failure with the provided emcmake, Ninja, and ctest commands, then inspect the CMake test-discovery and death-test setup that generates googletest-death-test-test. Compare the generated tests with the proposed CTest WILL_FAIL approach; done means Emscripten test runs no longer report death tests as not started while preserving meaningful behavior on supported platforms.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake, cpp, wasm
- Domain
- build-system, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100