Skip to content

Commit 6990b94

Browse files
authored
Run ReactCommon C++ unit tests in GitHub CI
## Summary The C++ unit tests under `packages/react-native/ReactCommon/**/tests` run only inside Meta, under Buck. Public CI has no build for them, so a change that breaks one passes every GitHub check and fails only after import, as happened with #59686. This adds a `test_react_common_cxx` job to `Test All` that builds and runs them on Linux, reusing the Fantom tester's CMake build, which already compiles every ReactCommon library with folly, Hermes and codegen. **Changes** - Add `private/react-native-fantom/tester/react-common-tests/`: one GoogleTest executable per `tests` directory (38 today), linked against the libraries `fantom_tester` links. Each test case is registered with CTest through `gtest_discover_tests`, so it runs in its own process. - Add `:private:react-native-fantom:testReactCommon`, which builds them and runs `ctest`. GoogleTest 1.18.0 is downloaded like the Fantom tester's other dependencies. - Add the `test_react_common_cxx` job to `test-all.yml`, mirroring `build_fantom_runner` on the same container and runner. It adds no ccache steps: in this container compiles do not go through ccache, and `build_fantom_runner` on `main` reports 0 hits and 0 misses. **Notes** - NOTE: Three Hermes debugger cases in `jsinspector-modern/tests` fail in OSS on macOS and Linux alike, and are excluded by name in the CMake file (see Test Plan). - The job installs clang 18 from apt.llvm.org, since the tests use C++20 features the image's clang 14 lacks: `std::source_location` and parenthesised aggregate initialisation. - Each case runs in its own process because some suites leave global state behind. In `jsinspector-modern/tests`, `TracingTest` fails when run after the other suites in one process, and passes on its own. - The tests compile with `-Wno-error`, since the header-only ReactCommon libraries export `-Werror -Wpedantic` and a few tests call deprecated APIs. - `benchmarks/` directories are left out, as they need Google Benchmark. Changelog: [Internal] ## Test Plan On macOS (Apple clang 21, CMake 3.30.5 from the Android SDK, JDK 17): ``` yarn install ./gradlew :private:react-native-fantom:testReactCommon ``` All 38 test executables build, and 1522 test cases pass, one of which skips itself (`ReactInstanceTest.testRegistersRuntimeSchedulerAsEventLoopControl`: this Hermes build has no `ISetEventLoopControl`). Before the exclusions, 1525 cases ran and these 3 failed, with the same result on macOS and in this job on Linux: - `JsiIntegrationHermesTest.ResolveColumnlessBreakpointAfterEval`: `Debugger.setBreakpointByUrl` returns no `locations`. - `JsiIntegrationHermesTest.ResolveColumnlessBreakpointAfterReload`: the same. - `JsiIntegrationHermesTest.TwoConnectionsDebuggerLifecycle`: the second session gets no `Debugger.scriptParsed` after the first disables `Debugger`. `test_react_common_cxx` on this PR: [passed](https://github.com/react/react-native/actions/runs/41937402196/job/126950417455) in 14m15s, with all 1522 tests passing. Running them takes 30 seconds of that; the rest is the build. <img width="400" alt="image" src="https://github.com/user-attachments/assets/1db09ec6-0d7f-45a4-b2a4-e87128bd3f5e" /> <img width="400" alt="file-bccb81bc0762aa22ac5f3c2165826db4" src="https://github.com/user-attachments/assets/50acc10a-6551-4bde-90f6-7466c72083c2" /> <img width="400" alt="file-49c0f65500ec760461f11925dfbc4059" src="https://github.com/user-attachments/assets/6aeb7b06-b282-4316-a905-365bcc2c9bf5" /> Differential Revision: D129898681 Pull Request resolved: #59802
1 parent 676c516 commit 6990b94

5 files changed

Lines changed: 257 additions & 0 deletions

File tree

‎.github/workflows/test-all.yml‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,60 @@ jobs:
281281
fail-on-error: true
282282
secrets: inherit
283283

284+
test_react_common_cxx:
285+
runs-on: 8-core-ubuntu
286+
needs: [check_code_changes, lint]
287+
if: needs.check_code_changes.outputs.any_code_change == 'true'
288+
container:
289+
image: reactnativecommunity/react-native-android:latest
290+
env:
291+
# Set the encoding to resolve a known character encoding issue with decompressing tar.gz files in containers
292+
# via Gradle: https://github.com/gradle/gradle/issues/23391#issuecomment-1878979127
293+
LC_ALL: C.UTF8
294+
TERM: 'dumb'
295+
GRADLE_OPTS: '-Dorg.gradle.daemon=false'
296+
REACT_NATIVE_DOWNLOADS_DIR: /opt/react-native-downloads
297+
steps:
298+
- name: Checkout
299+
uses: actions/checkout@v6
300+
# The tests use C++20 features newer than the image's clang 14, such as
301+
# std::source_location and parenthesised aggregate initialisation.
302+
- name: Install dependencies
303+
shell: bash
304+
run: |
305+
sudo apt update
306+
sudo apt install -y git cmake openssl libssl-dev curl gnupg
307+
codename=$(. /etc/os-release && echo "$VERSION_CODENAME")
308+
curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc > /dev/null
309+
echo "deb http://apt.llvm.org/$codename/ llvm-toolchain-$codename-18 main" | sudo tee /etc/apt/sources.list.d/llvm-18.list
310+
sudo apt update
311+
sudo apt install -y clang-18
312+
- name: Setup git safe folders
313+
shell: bash
314+
run: git config --global --add safe.directory '*'
315+
- name: Setup node.js
316+
uses: ./.github/actions/setup-node
317+
- name: Install node dependencies
318+
uses: ./.github/actions/yarn-install
319+
- name: Setup gradle
320+
uses: ./.github/actions/setup-gradle
321+
with:
322+
cache-encryption-key: ${{ secrets.GRADLE_CACHE_ENCRYPTION_KEY }}
323+
- name: Run ReactCommon C++ tests
324+
shell: bash
325+
run: ./gradlew :private:react-native-fantom:testReactCommon
326+
env:
327+
CC: clang-18
328+
CXX: clang++-18
329+
- name: Upload test results
330+
if: ${{ always() }}
331+
uses: ./.github/actions/upload-artifact
332+
with:
333+
name: test-react-common-cxx-results
334+
compression-level: 1
335+
if-no-files-found: ignore
336+
path: private/react-native-fantom/build/reports
337+
284338
build_android:
285339
runs-on: 8-core-ubuntu
286340
needs: [set_release_type, check_code_changes]

‎packages/react-native/gradle/libs.versions.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ fmt="12.1.0"
5050
folly="2024.11.18.00"
5151
glog="0.3.5"
5252
gflags="2.2.0"
53+
googletest="1.18.0"
5354
nlohmannjson="3.12.0"
5455

5556
[libraries]

‎private/react-native-fantom/build.gradle.kts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fun getSDKPath(): String {
3535

3636
val FOLLY_VERSION = libs.versions.folly.get()
3737
val GFLAGS_VERSION = libs.versions.gflags.get()
38+
val GOOGLETEST_VERSION = libs.versions.googletest.get()
3839
val NLOHMANNJSON_VERSION = libs.versions.nlohmannjson.get()
3940

4041
val buildDir = project.layout.buildDirectory.get().asFile
@@ -104,6 +105,27 @@ val prepareGflags by
104105
outputDir.set(File(thirdParty, "gflags"))
105106
}
106107

108+
val downloadGoogleTestDest = File(downloadsDir, "googletest-${GOOGLETEST_VERSION}.tar.gz")
109+
val downloadGoogleTest by
110+
tasks.registering(Download::class) {
111+
dependsOn(createNativeDepsDirectories)
112+
src("https://github.com/google/googletest/archive/v${GOOGLETEST_VERSION}.tar.gz")
113+
onlyIfModified(true)
114+
overwrite(false)
115+
retries(5)
116+
quiet(true)
117+
dest(downloadGoogleTestDest)
118+
}
119+
120+
val prepareGoogleTest by
121+
tasks.registering(Copy::class) {
122+
dependsOn(listOf(downloadGoogleTest))
123+
from(tarTree(downloadGoogleTestDest))
124+
eachFile { path = path.substringAfter("/") }
125+
includeEmptyDirs = false
126+
into("$thirdParty/googletest")
127+
}
128+
107129
val downloadNlohmannJsonDest = File(downloadsDir, "nlohmann_json-${NLOHMANNJSON_VERSION}.tar.gz")
108130
val downloadNlohmannJson by
109131
tasks.registering(Download::class) {
@@ -164,6 +186,7 @@ val prepareNative3pDependencies by
164186
tasks.registering {
165187
dependsOn(
166188
prepareGflags,
189+
prepareGoogleTest,
167190
prepareNlohmannJson,
168191
prepareFolly,
169192
":packages:react-native:ReactAndroid:prepareBoost",
@@ -229,3 +252,38 @@ val buildFantomTester by
229252
standardOutputFile.set(project.file("$buildDir/reports/build-fantom_tester.log"))
230253
errorOutputFile.set(project.file("$buildDir/reports/build-fantom_tester.error.log"))
231254
}
255+
256+
val buildReactCommonTests by
257+
tasks.registering(CustomExecTask::class) {
258+
dependsOn(configureFantomTester)
259+
workingDir(testerDir)
260+
inputs.files(testerBuildOutputFileTree)
261+
outputs.upToDateWhen { false }
262+
commandLine(
263+
cmakeBinaryPath,
264+
"--build",
265+
testerBuildDir.toString(),
266+
"--target",
267+
"react_common_tests",
268+
"-j",
269+
ndkBuildJobs,
270+
)
271+
standardOutputFile.set(project.file("$buildDir/reports/build-react_common_tests.log"))
272+
errorOutputFile.set(project.file("$buildDir/reports/build-react_common_tests.error.log"))
273+
}
274+
275+
val testReactCommon by
276+
tasks.registering(Exec::class) {
277+
dependsOn(buildReactCommonTests)
278+
outputs.upToDateWhen { false }
279+
commandLine(
280+
"$cmakePath/bin/ctest",
281+
"--test-dir",
282+
"$testerBuildDir/react-common-tests",
283+
"--output-on-failure",
284+
"--output-junit",
285+
"$reportsDir/react-common-tests.xml",
286+
"-j",
287+
ndkBuildJobs,
288+
)
289+
}

‎private/react-native-fantom/tester/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,5 @@ target_compile_options(fantom_tester
258258
-fexceptions
259259
-frtti
260260
-std=c++20)
261+
262+
add_subdirectory(react-common-tests)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
# Builds the C++ unit tests in ReactCommon/**/tests against the libraries the
7+
# Fantom tester already compiles, one GoogleTest executable per tests
8+
# directory. Run them with ctest from this directory's build directory.
9+
10+
cmake_minimum_required(VERSION 3.13)
11+
12+
enable_testing()
13+
include(GoogleTest)
14+
15+
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
16+
add_subdirectory(${FANTOM_THIRD_PARTY_DIR}/googletest googletest EXCLUDE_FROM_ALL)
17+
18+
add_react_common_subdir(react/networking)
19+
add_react_common_subdir(react/renderer/animations)
20+
add_react_common_subdir(react/renderer/element)
21+
22+
# rrc_textinput compiles the Android platform sources, which need the Android
23+
# text layout; the tests only use the cross-platform base.
24+
file(GLOB react_common_tests_textinput_SRC CONFIGURE_DEPENDS
25+
${REACT_COMMON_DIR}/react/renderer/components/textinput/*.cpp)
26+
add_library(react_common_tests_textinput OBJECT ${react_common_tests_textinput_SRC})
27+
target_link_libraries(react_common_tests_textinput
28+
PUBLIC
29+
react_renderer_attributedstring
30+
react_renderer_core
31+
rrc_text
32+
rrc_view)
33+
target_compile_reactnative_options(react_common_tests_textinput PRIVATE)
34+
35+
# The folly executors some tests use, and what they pull in, which
36+
# folly_runtime leaves out.
37+
set(folly_DIR ${FANTOM_THIRD_PARTY_DIR}/folly/folly)
38+
add_library(react_common_tests_folly STATIC
39+
${folly_DIR}/ExceptionString.cpp
40+
${folly_DIR}/Executor.cpp
41+
${folly_DIR}/SingletonThreadLocal.cpp
42+
${folly_DIR}/concurrency/ProcessLocalUniqueId.cpp
43+
${folly_DIR}/detail/MemoryIdler.cpp
44+
${folly_DIR}/detail/StaticSingletonManager.cpp
45+
${folly_DIR}/detail/ThreadLocalDetail.cpp
46+
${folly_DIR}/detail/thread_local_globals.cpp
47+
${folly_DIR}/executors/ManualExecutor.cpp
48+
${folly_DIR}/executors/QueuedImmediateExecutor.cpp
49+
${folly_DIR}/io/async/Request.cpp
50+
${folly_DIR}/memory/MallctlHelper.cpp
51+
${folly_DIR}/memory/ReentrantAllocator.cpp
52+
${folly_DIR}/portability/SysMembarrier.cpp
53+
${folly_DIR}/synchronization/AsymmetricThreadFence.cpp
54+
${folly_DIR}/synchronization/Hazptr.cpp
55+
${folly_DIR}/synchronization/HazptrDomain.cpp
56+
${folly_DIR}/system/Pid.cpp)
57+
target_compile_options(react_common_tests_folly PRIVATE -fexceptions -frtti)
58+
target_link_libraries(react_common_tests_folly PUBLIC folly_runtime)
59+
60+
# Test headers are included by the paths Buck exports them under.
61+
set(react_common_tests_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
62+
configure_file(
63+
${REACT_COMMON_DIR}/callinvoker/ReactCommon/tests/TestCallInvoker.h
64+
${react_common_tests_INCLUDE_DIR}/ReactCommon/TestCallInvoker.h
65+
COPYONLY)
66+
67+
get_target_property(react_common_tests_LIBS fantom_tester LINK_LIBRARIES)
68+
69+
# Pre-existing failures in OSS, on macOS and Linux alike, excluded by gtest
70+
# name per test target.
71+
set(jsinspector_modern_tests_EXCLUDED
72+
# Debugger.setBreakpointByUrl resolves no locations for a columnless
73+
# breakpoint.
74+
JsiIntegrationHermesTest/*.ResolveColumnlessBreakpointAfterEval
75+
JsiIntegrationHermesTest/*.ResolveColumnlessBreakpointAfterReload
76+
# The second session gets no Debugger.scriptParsed once the first disables
77+
# Debugger.
78+
JsiIntegrationHermesTest/*.TwoConnectionsDebuggerLifecycle)
79+
80+
file(GLOB_RECURSE react_common_tests_SRC CONFIGURE_DEPENDS ${REACT_COMMON_DIR}/*.cpp)
81+
list(FILTER react_common_tests_SRC INCLUDE REGEX "/tests/")
82+
list(FILTER react_common_tests_SRC EXCLUDE REGEX "/benchmarks/")
83+
84+
set(react_common_tests_DIRS)
85+
foreach(source ${react_common_tests_SRC})
86+
string(REGEX REPLACE "^(.*/tests)/.*$" "\\1" test_dir ${source})
87+
list(APPEND react_common_tests_DIRS ${test_dir})
88+
endforeach()
89+
list(REMOVE_DUPLICATES react_common_tests_DIRS)
90+
list(SORT react_common_tests_DIRS)
91+
92+
set(react_common_tests_TARGETS)
93+
foreach(test_dir ${react_common_tests_DIRS})
94+
file(RELATIVE_PATH test_name ${REACT_COMMON_DIR} ${test_dir})
95+
string(MAKE_C_IDENTIFIER ${test_name} test_target)
96+
97+
set(test_dir_SRC ${react_common_tests_SRC})
98+
list(FILTER test_dir_SRC INCLUDE REGEX "^${test_dir}/")
99+
100+
add_executable(${test_target} EXCLUDE_FROM_ALL ${test_dir_SRC})
101+
# Header-only ReactCommon libraries export -Werror -Wpedantic, and source
102+
# options are the only ones placed after them on the command line.
103+
set_source_files_properties(${test_dir_SRC} PROPERTIES COMPILE_OPTIONS -Wno-error)
104+
target_include_directories(${test_target}
105+
PRIVATE
106+
${react_common_tests_INCLUDE_DIR}
107+
${test_dir}
108+
${test_dir}/..)
109+
target_link_libraries(${test_target}
110+
PRIVATE
111+
${react_common_tests_LIBS}
112+
react_common_tests_folly
113+
react_common_tests_textinput
114+
react_networking
115+
react_renderer_animations
116+
react_renderer_element
117+
GTest::gmock_main)
118+
target_compile_options(${test_target}
119+
PRIVATE
120+
-fexceptions
121+
-frtti
122+
-std=c++20)
123+
124+
set(test_FILTER "*")
125+
if(DEFINED ${test_target}_EXCLUDED)
126+
list(JOIN ${test_target}_EXCLUDED ":" test_EXCLUDED)
127+
set(test_FILTER "*-${test_EXCLUDED}")
128+
endif()
129+
130+
# One process per test case: some suites leave global state behind, such as
131+
# registered HostTargets in jsinspector-modern, that later suites trip over.
132+
gtest_discover_tests(${test_target}
133+
TEST_PREFIX ${test_name}/
134+
TEST_FILTER ${test_FILTER}
135+
NO_PRETTY_TYPES
136+
NO_PRETTY_VALUES
137+
DISCOVERY_MODE PRE_TEST)
138+
list(APPEND react_common_tests_TARGETS ${test_target})
139+
endforeach()
140+
141+
add_custom_target(react_common_tests)
142+
add_dependencies(react_common_tests ${react_common_tests_TARGETS})

0 commit comments

Comments
 (0)