1# Copyright 2020 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14 15import("//build_overrides/pigweed.gni") 16 17import("$dir_pw_build/module_config.gni") 18import("$dir_pw_build/python_action_test.gni") 19import("$dir_pw_build/target_types.gni") 20import("$dir_pw_docgen/docs.gni") 21import("$dir_pw_protobuf_compiler/proto.gni") 22import("$dir_pw_toolchain/traits.gni") 23import("$dir_pw_unit_test/test.gni") 24 25declare_args() { 26 # The build target that overrides the default configuration options for this 27 # module. This should point to a source set that provides defines through a 28 # public config (which may -include a file or add defines directly). 29 pw_unit_test_CONFIG = pw_build_DEFAULT_MODULE_CONFIG 30} 31 32# This pool limits the maximum number of unit tests that may run in parallel. 33# Despite the fact that this is a single GN "target", each toolchain owns its 34# own version of this pool, meaning pw_unit_test_POOL_DEPTH may be set 35# differently across targets in a single build, and build steps in one toolchain 36# will not consume pool resources of steps from another toolchain. 37pool("unit_test_pool") { 38 depth = pw_unit_test_POOL_DEPTH 39} 40 41config("public_include_path") { 42 include_dirs = [ "public" ] 43} 44 45config("public_overrides_include_path") { 46 include_dirs = [ "public_overrides" ] 47} 48 49config("light_public_overrides_include_path") { 50 include_dirs = [ "light_public_overrides" ] 51} 52 53config("googletest_public_overrides_include_path") { 54 include_dirs = [ "googletest_public_overrides" ] 55} 56 57pw_source_set("config") { 58 public = [ "public/pw_unit_test/config.h" ] 59 public_configs = [ ":public_include_path" ] 60 public_deps = [ 61 dir_pw_polyfill, 62 pw_unit_test_CONFIG, 63 ] 64 visibility = [ ":*" ] 65} 66 67# pw_unit_test facade. This provides a GoogleTest-compatible test framework. 68pw_source_set("pw_unit_test") { 69 testonly = pw_unit_test_TESTONLY 70 public = [ "public/pw_unit_test/framework.h" ] 71 public_configs = [ ":public_include_path" ] 72 public_deps = [ 73 ":status_macros", 74 pw_unit_test_BACKEND, 75 ] 76} 77 78# Lightweight unit test backend that implements a subset of GoogleTest. 79pw_source_set("light") { 80 public_configs = [ 81 ":light_public_overrides_include_path", 82 ":public_include_path", 83 ":public_overrides_include_path", 84 ] 85 public_deps = [ 86 ":config", 87 ":event_handler", 88 ":status_macros", 89 "$dir_pw_bytes:alignment", 90 "$dir_pw_string:builder", 91 dir_pw_polyfill, 92 dir_pw_preprocessor, 93 dir_pw_span, 94 dir_pw_status, 95 ] 96 97 deps = [ dir_pw_assert ] 98 99 public = [ 100 "light_public_overrides/pw_unit_test/framework_backend.h", 101 102 # The facade header is included here since public_overrides/gtest/gtest.h 103 # includes it. This avoids a circular dependency in the build system. 104 "public/pw_unit_test/framework.h", 105 "public_overrides/gtest/gtest.h", 106 ] 107 sources = [ "framework_light.cc" ] 108} 109 110# Unit test framework backend that redirects to GoogleTest. 111pw_source_set("googletest") { 112 public_configs = [ ":googletest_public_overrides_include_path" ] 113 public = [ "googletest_public_overrides/pw_unit_test/framework_backend.h" ] 114 115 public_deps = [ 116 "$dir_pw_third_party/googletest", 117 dir_pw_result, 118 dir_pw_status, 119 ] 120} 121 122pw_source_set("event_handler") { 123 public_configs = [ ":public_include_path" ] 124 public = [ "public/pw_unit_test/event_handler.h" ] 125} 126 127pw_source_set("status_macros") { 128 public_configs = [ ":public_include_path" ] 129 public = [ "public/pw_unit_test/status_macros.h" ] 130 public_deps = [ 131 "$dir_pw_third_party/fuchsia:stdcompat", 132 dir_pw_status, 133 ] 134} 135 136# Unit test event handler that provides GoogleTest-style output. 137pw_source_set("googletest_style_event_handler") { 138 public_deps = [ 139 ":event_handler", 140 dir_pw_preprocessor, 141 ] 142 public = [ "public/pw_unit_test/googletest_style_event_handler.h" ] 143 sources = [ "googletest_style_event_handler.cc" ] 144} 145 146pw_source_set("googletest_handler_adapter") { 147 public_configs = [ ":public_include_path" ] 148 public_deps = [ 149 ":event_handler", 150 "$dir_pw_third_party/googletest", 151 dir_pw_preprocessor, 152 ] 153 public = [ "public/pw_unit_test/googletest_handler_adapter.h" ] 154 sources = [ "googletest_handler_adapter.cc" ] 155} 156 157pw_source_set("googletest_test_matchers") { 158 public_configs = [ ":public_include_path" ] 159 public = [ "public/pw_unit_test/googletest_test_matchers.h" ] 160 public_deps = [ 161 "$dir_pw_third_party/googletest", 162 dir_pw_result, 163 dir_pw_status, 164 ] 165} 166 167pw_test("googletest_test_matchers_test") { 168 enable_if = pw_unit_test_BACKEND != "" && 169 pw_unit_test_BACKEND != "$dir_pw_unit_test:light" 170 sources = [ "googletest_test_matchers_test.cc" ] 171 deps = [ ":googletest_test_matchers" ] 172} 173 174# Library providing an event handler which outputs human-readable text. 175pw_source_set("simple_printing_event_handler") { 176 public_deps = [ 177 ":googletest_style_event_handler", 178 "$dir_pw_preprocessor", 179 ] 180 public = [ "public/pw_unit_test/simple_printing_event_handler.h" ] 181 sources = [ "simple_printing_event_handler.cc" ] 182} 183 184# Library providing a standard desktop main function for the pw_unit_test 185# framework. Unit test files can link against this library to build runnable 186# unit test executables. 187pw_source_set("simple_printing_main") { 188 testonly = pw_unit_test_TESTONLY 189 deps = [ 190 ":pw_unit_test", 191 ":simple_printing_event_handler", 192 "$dir_pw_sys_io", 193 dir_pw_span, 194 ] 195 sources = [ "simple_printing_main.cc" ] 196} 197 198pw_source_set("printf_event_handler") { 199 public_deps = [ 200 ":googletest_style_event_handler", 201 dir_pw_preprocessor, 202 ] 203 public = [ "public/pw_unit_test/printf_event_handler.h" ] 204} 205 206pw_source_set("printf_main") { 207 testonly = pw_unit_test_TESTONLY 208 deps = [ 209 ":printf_event_handler", 210 ":pw_unit_test", 211 ] 212 sources = [ "printf_main.cc" ] 213} 214 215# Library providing an event handler which logs using pw_log. 216pw_source_set("logging_event_handler") { 217 public_deps = [ 218 ":googletest_style_event_handler", 219 dir_pw_log, 220 ] 221 public = [ "public/pw_unit_test/logging_event_handler.h" ] 222 sources = [ "logging_event_handler.cc" ] 223} 224 225# Provides logging to either the light framework or an external GoogleTest. 226group("logging") { 227 public_deps = [ ":logging_event_handler" ] 228 deps = [] 229 if (pw_unit_test_BACKEND != "$dir_pw_unit_test:light") { 230 deps += [ ":googletest_handler_adapter" ] 231 } 232} 233 234pw_source_set("logging_main") { 235 testonly = pw_unit_test_TESTONLY 236 deps = [ 237 ":logging", 238 ":pw_unit_test", 239 ] 240 sources = [ "logging_main.cc" ] 241} 242 243pw_source_set("constexpr") { 244 public = [ "public/pw_unit_test/constexpr.h" ] 245 public_configs = [ ":public_include_path" ] 246 public_deps = [ 247 "$dir_pw_third_party/fuchsia:stdcompat", 248 dir_pw_preprocessor, 249 ] 250} 251 252pw_test("constexpr_test") { 253 sources = [ "constexpr_test.cc" ] 254 deps = [ ":constexpr" ] 255 negative_compilation_tests = true 256} 257 258# Library providing an event handler adapter that allows for multiple 259# event handlers to be registered for a given test run 260pw_source_set("multi_event_handler") { 261 public_deps = [ ":event_handler" ] 262 public = [ "public/pw_unit_test/multi_event_handler.h" ] 263} 264 265pw_test("multi_event_handler_test") { 266 sources = [ "multi_event_handler_test.cc" ] 267 deps = [ 268 ":multi_event_handler", 269 ":pw_unit_test", 270 ] 271} 272 273# Library providing an event handler which outputs a test summary artifact. 274pw_source_set("test_record_event_handler") { 275 public_deps = [ 276 ":googletest_style_event_handler", 277 "$dir_pw_json:builder", 278 ] 279 deps = [ "$dir_pw_assert" ] 280 public = [ "public/pw_unit_test/test_record_event_handler.h" ] 281 sources = [ "public/pw_unit_test/internal/test_record_trie.h" ] 282} 283 284pw_test("test_record_event_handler_test") { 285 sources = [ "test_record_event_handler_test.cc" ] 286 deps = [ 287 ":pw_unit_test", 288 ":test_record_event_handler", 289 ] 290} 291 292config("rpc_service_backend_light") { 293 include_dirs = [ "rpc_light_public" ] 294} 295 296config("rpc_service_backend_gtest") { 297 include_dirs = [ "rpc_gtest_public" ] 298} 299 300pw_source_set("rpc_service") { 301 testonly = pw_unit_test_TESTONLY 302 public_configs = [ ":public_include_path" ] 303 public_deps = [ 304 ":config", 305 ":event_handler", 306 ":pw_unit_test", 307 ":unit_test_proto.pwpb", 308 ":unit_test_proto.raw_rpc", 309 "$dir_pw_containers:vector", 310 ] 311 deps = [ dir_pw_log ] 312 public = [ "public/pw_unit_test/unit_test_service.h" ] 313 sources = [ "unit_test_service.cc" ] 314 defines = [] 315 316 if (pw_unit_test_BACKEND == "$dir_pw_unit_test:light") { 317 public_configs += [ ":rpc_service_backend_light" ] 318 sources += [ "rpc_light_event_handler.cc" ] 319 public += [ "rpc_light_public/pw_unit_test/internal/rpc_event_handler.h" ] 320 } else { 321 public_configs += [ ":rpc_service_backend_gtest" ] 322 sources += [ "rpc_gtest_event_handler.cc" ] 323 public += [ "rpc_gtest_public/pw_unit_test/internal/rpc_event_handler.h" ] 324 } 325} 326 327pw_source_set("rpc_main") { 328 testonly = pw_unit_test_TESTONLY 329 public_deps = [ ":pw_unit_test" ] 330 deps = [ 331 ":rpc_service", 332 "$dir_pw_rpc/system_server", 333 dir_pw_log, 334 ] 335 sources = [ "rpc_main.cc" ] 336} 337 338if (pw_unit_test_BACKEND == "$dir_pw_unit_test:light") { 339 pw_source_set("static_library_support") { 340 public_configs = [ ":public_include_path" ] 341 public_deps = [ pw_unit_test_BACKEND ] 342 public = [ "public/pw_unit_test/static_library_support.h" ] 343 sources = [ "static_library_support.cc" ] 344 } 345} else { 346 group("static_library_support") { 347 } 348} 349 350pw_executable("test_rpc_server") { 351 testonly = pw_unit_test_TESTONLY 352 sources = [ "test_rpc_server.cc" ] 353 deps = [ 354 ":pw_unit_test", 355 ":rpc_service", 356 "$dir_pw_rpc/system_server", 357 "$dir_pw_rpc/system_server:socket", 358 dir_pw_log, 359 ] 360} 361 362pw_proto_library("unit_test_proto") { 363 sources = [ "pw_unit_test_proto/unit_test.proto" ] 364} 365 366pw_doc_group("docs") { 367 sources = [ "docs.rst" ] 368 other_deps = [ "py" ] 369 inputs = [ "constexpr_test.cc" ] 370} 371 372pw_test("metadata_only_test") { 373 extra_metadata = { 374 extra_key = "extra_value" 375 } 376} 377 378# pw_test_group produces the metadata file for its tests. 379pw_test_group("metadata_only_group") { 380 tests = [ ":metadata_only_test" ] 381 output_metadata = true 382} 383 384pw_python_action_test("test_group_metadata_test") { 385 _metadata_path = 386 rebase_path(get_label_info(":metadata_only_group", "target_out_dir")) + 387 "/metadata_only_group.testinfo.json" 388 testonly = pw_unit_test_TESTONLY 389 sources = [ "py/test_group_metadata_test.py" ] 390 args = [ 391 "--metadata-path", 392 "$_metadata_path", 393 ] 394 deps = [ ":metadata_only_group" ] 395} 396 397pw_test("framework_test") { 398 sources = [ "framework_test.cc" ] 399 deps = [ 400 dir_pw_assert, 401 dir_pw_result, 402 dir_pw_status, 403 ] 404 405 # TODO: https://pwbug.dev/325509758 - Passes but hangs on cleanup. 406 if (pw_build_EXECUTABLE_TARGET_TYPE == "pico_executable") { 407 enable_if = false 408 } 409} 410 411pw_test("framework_light_test") { 412 enable_if = pw_unit_test_BACKEND == "$dir_pw_unit_test:light" 413 sources = [ "framework_light_test.cc" ] 414 deps = [ 415 "$dir_pw_string:builder", 416 dir_pw_status, 417 ] 418} 419 420pw_static_library("tests_in_archive") { 421 testonly = pw_unit_test_TESTONLY 422 sources = [ 423 "static_library_archived_tests.cc", 424 "static_library_missing_archived_tests.cc", 425 ] 426 deps = [ ":pw_unit_test" ] 427 visibility = [ ":*" ] 428} 429 430pw_test("static_library_support_test") { 431 enable_if = pw_unit_test_BACKEND == "$dir_pw_unit_test:light" 432 sources = [ "static_library_support_test.cc" ] 433 deps = [ 434 ":static_library_support", 435 ":tests_in_archive", 436 dir_pw_assert, 437 ] 438} 439 440pw_test_group("tests") { 441 tests = [ 442 ":constexpr_test", 443 ":framework_test", 444 ":framework_light_test", 445 ":static_library_support_test", 446 ":multi_event_handler_test", 447 ":test_record_event_handler_test", 448 ] 449 if (dir_pw_third_party_googletest != "") { 450 tests += [ ":googletest_test_matchers_test" ] 451 } 452} 453