Skip to content

Commit c9f8765

Browse files
committed
Run ReactCommon C++ unit tests in CI
1 parent 38f6e19 commit c9f8765

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
@@ -331,6 +331,60 @@ jobs:
331331
fail-on-error: true
332332
secrets: inherit
333333

334+
test_react_common_cxx:
335+
runs-on: 8-core-ubuntu
336+
needs: [check_code_changes, lint]
337+
if: needs.check_code_changes.outputs.any_code_change == 'true'
338+
container:
339+
image: reactnativecommunity/react-native-android:latest
340+
env:
341+
# Set the encoding to resolve a known character encoding issue with decompressing tar.gz files in containers
342+
# via Gradle: https://github.com/gradle/gradle/issues/23391#issuecomment-1878979127
343+
LC_ALL: C.UTF8
344+
TERM: 'dumb'
345+
GRADLE_OPTS: '-Dorg.gradle.daemon=false'
346+
REACT_NATIVE_DOWNLOADS_DIR: /opt/react-native-downloads
347+
steps:
348+
- name: Checkout
349+
uses: actions/checkout@v6
350+
# The tests use C++20 features newer than the image's clang 14, such as
351+
# std::source_location and parenthesised aggregate initialisation.
352+
- name: Install dependencies
353+
shell: bash
354+
run: |
355+
sudo apt update
356+
sudo apt install -y git cmake openssl libssl-dev curl gnupg
357+
codename=$(. /etc/os-release && echo "$VERSION_CODENAME")
358+
curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc > /dev/null
359+
echo "deb http://apt.llvm.org/$codename/ llvm-toolchain-$codename-18 main" | sudo tee /etc/apt/sources.list.d/llvm-18.list
360+
sudo apt update
361+
sudo apt install -y clang-18
362+
- name: Setup git safe folders
363+
shell: bash
364+
run: git config --global --add safe.directory '*'
365+
- name: Setup node.js
366+
uses: ./.github/actions/setup-node
367+
- name: Install node dependencies
368+
uses: ./.github/actions/yarn-install
369+
- name: Setup gradle
370+
uses: ./.github/actions/setup-gradle
371+
with:
372+
cache-encryption-key: ${{ secrets.GRADLE_CACHE_ENCRYPTION_KEY }}
373+
- name: Run ReactCommon C++ tests
374+
shell: bash
375+
run: ./gradlew :private:react-native-fantom:testReactCommon
376+
env:
377+
CC: clang-18
378+
CXX: clang++-18
379+
- name: Upload test results
380+
if: ${{ always() }}
381+
uses: ./.github/actions/upload-artifact
382+
with:
383+
name: test-react-common-cxx-results
384+
compression-level: 1
385+
if-no-files-found: ignore
386+
path: private/react-native-fantom/build/reports
387+
334388
build_android:
335389
runs-on: 8-core-ubuntu
336390
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)