google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.93k stars 10.16k forks source link

[Bug]: Death tests don't work with emscripten #4595

Open dabrahams opened 3 months ago

dabrahams commented 3 months ago

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:

# Turns all tests discovered in <test_executable> 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