• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = [ pw_unit_test_BACKEND ]
73
74  # Temporarily redirect deprecated googletest pointer to new pointer.
75  if ("$pw_unit_test_GOOGLETEST_BACKEND" == "$dir_pw_third_party/googletest" &&
76      "$pw_unit_test_MAIN" == "$dir_pw_third_party/googletest:gmock_main" &&
77      "$pw_unit_test_BACKEND" == "$dir_pw_unit_test:light") {
78    print("Warning: pw_unit_test_GOOGLETEST_BACKEND is deprecated.")
79    print("Set pw_unit_test_BACKEND to //pw_unit_test:googletest.")
80    public_deps = []  # The list must be empty before overriding.
81    public_deps = [ ":googletest" ]
82  }
83}
84
85# Lightweight unit test backend that implements a subset of GoogleTest.
86pw_source_set("light") {
87  public_configs = [
88    ":light_public_overrides_include_path",
89    ":public_include_path",
90    ":public_overrides_include_path",
91  ]
92  public_deps = [
93    ":config",
94    ":event_handler",
95    "$dir_pw_bytes:alignment",
96    dir_pw_polyfill,
97    dir_pw_preprocessor,
98    dir_pw_span,
99  ]
100
101  # If C++17 is supported, depend on StringBuilder.
102  if (pw_toolchain_CXX_STANDARD >= pw_toolchain_STANDARD.CXX17) {
103    public_deps += [ "$dir_pw_string:builder" ]
104  }
105
106  deps = [ dir_pw_assert ]
107
108  public = [
109    "light_public_overrides/pw_unit_test/framework_backend.h",
110
111    # The facade header is included here since public_overrides/gtest/gtest.h
112    # includes it. This avoids a circular dependency in the build system.
113    "public/pw_unit_test/framework.h",
114    "public_overrides/gtest/gtest.h",
115  ]
116  sources = [ "framework_light.cc" ]
117}
118
119# Unit test framework backend that redirects to GoogleTest.
120if (pw_unit_test_BACKEND != "") {
121  pw_source_set("googletest") {
122    public_configs = [ ":googletest_public_overrides_include_path" ]
123    public = [ "googletest_public_overrides/pw_unit_test/framework_backend.h" ]
124
125    public_deps = [ pw_unit_test_GOOGLETEST_BACKEND ]
126  }
127}
128
129pw_source_set("event_handler") {
130  public_configs = [ ":public_include_path" ]
131  public = [ "public/pw_unit_test/event_handler.h" ]
132}
133
134# Unit test event handler that provides GoogleTest-style output.
135pw_source_set("googletest_style_event_handler") {
136  public_deps = [
137    ":event_handler",
138    dir_pw_preprocessor,
139  ]
140  public = [ "public/pw_unit_test/googletest_style_event_handler.h" ]
141  sources = [ "googletest_style_event_handler.cc" ]
142}
143
144pw_source_set("googletest_handler_adapter") {
145  public_configs = [ ":public_include_path" ]
146  public_deps = [
147    ":event_handler",
148    "$dir_pw_third_party/googletest",
149    dir_pw_preprocessor,
150  ]
151  public = [ "public/pw_unit_test/googletest_handler_adapter.h" ]
152  sources = [ "googletest_handler_adapter.cc" ]
153}
154
155pw_source_set("googletest_test_matchers") {
156  public_configs = [ ":public_include_path" ]
157  public = [ "public/pw_unit_test/googletest_test_matchers.h" ]
158  public_deps = [
159    "$dir_pw_third_party/googletest",
160    dir_pw_result,
161    dir_pw_status,
162  ]
163}
164
165pw_test("googletest_test_matchers_test") {
166  enable_if = pw_unit_test_BACKEND != "" &&
167              pw_unit_test_BACKEND != "$dir_pw_unit_test:light"
168  sources = [ "googletest_test_matchers_test.cc" ]
169  deps = [ ":googletest_test_matchers" ]
170}
171
172# Library providing an event handler which outputs human-readable text.
173pw_source_set("simple_printing_event_handler") {
174  public_deps = [
175    ":googletest_style_event_handler",
176    "$dir_pw_preprocessor",
177  ]
178  public = [ "public/pw_unit_test/simple_printing_event_handler.h" ]
179  sources = [ "simple_printing_event_handler.cc" ]
180}
181
182# Library providing a standard desktop main function for the pw_unit_test
183# framework. Unit test files can link against this library to build runnable
184# unit test executables.
185pw_source_set("simple_printing_main") {
186  testonly = pw_unit_test_TESTONLY
187  deps = [
188    ":pw_unit_test",
189    ":simple_printing_event_handler",
190    "$dir_pw_sys_io",
191    dir_pw_span,
192  ]
193  sources = [ "simple_printing_main.cc" ]
194}
195
196pw_source_set("printf_event_handler") {
197  public_deps = [
198    ":googletest_style_event_handler",
199    dir_pw_preprocessor,
200  ]
201  public = [ "public/pw_unit_test/printf_event_handler.h" ]
202}
203
204pw_source_set("printf_main") {
205  testonly = pw_unit_test_TESTONLY
206  deps = [
207    ":printf_event_handler",
208    ":pw_unit_test",
209  ]
210  sources = [ "printf_main.cc" ]
211}
212
213# Library providing an event handler which logs using pw_log.
214pw_source_set("logging_event_handler") {
215  public_deps = [
216    ":googletest_style_event_handler",
217    dir_pw_log,
218  ]
219  public = [ "public/pw_unit_test/logging_event_handler.h" ]
220  sources = [ "logging_event_handler.cc" ]
221}
222
223# Provides logging to either the light framework or an external GoogleTest.
224group("logging") {
225  public_deps = [ ":logging_event_handler" ]
226  deps = []
227  if (pw_unit_test_BACKEND != "$dir_pw_unit_test:light") {
228    deps += [ ":googletest_handler_adapter" ]
229  }
230}
231
232pw_source_set("logging_main") {
233  testonly = pw_unit_test_TESTONLY
234  deps = [
235    ":logging",
236    ":pw_unit_test",
237  ]
238  sources = [ "logging_main.cc" ]
239}
240
241# Library providing an event handler adapter that allows for multiple
242# event handlers to be registered for a given test run
243pw_source_set("multi_event_handler") {
244  public_deps = [ ":event_handler" ]
245  public = [ "public/pw_unit_test/multi_event_handler.h" ]
246}
247
248pw_test("multi_event_handler_test") {
249  sources = [ "multi_event_handler_test.cc" ]
250  deps = [
251    ":multi_event_handler",
252    ":pw_unit_test",
253  ]
254}
255
256# Library providing an event handler which outputs a test summary artifact.
257pw_source_set("test_record_event_handler") {
258  public_deps = [
259    ":googletest_style_event_handler",
260    "$dir_pw_json:builder",
261  ]
262  deps = [ "$dir_pw_assert" ]
263  public = [ "public/pw_unit_test/test_record_event_handler.h" ]
264  sources = [ "public/pw_unit_test/internal/test_record_trie.h" ]
265}
266
267pw_test("test_record_event_handler_test") {
268  sources = [ "test_record_event_handler_test.cc" ]
269  deps = [
270    ":pw_unit_test",
271    ":test_record_event_handler",
272  ]
273}
274
275config("rpc_service_backend_light") {
276  include_dirs = [ "rpc_light_public" ]
277}
278
279config("rpc_service_backend_gtest") {
280  include_dirs = [ "rpc_gtest_public" ]
281}
282
283pw_source_set("rpc_service") {
284  testonly = pw_unit_test_TESTONLY
285  public_configs = [ ":public_include_path" ]
286  public_deps = [
287    ":config",
288    ":event_handler",
289    ":pw_unit_test",
290    ":unit_test_proto.pwpb",
291    ":unit_test_proto.raw_rpc",
292    "$dir_pw_containers:vector",
293  ]
294  deps = [ dir_pw_log ]
295  public = [ "public/pw_unit_test/unit_test_service.h" ]
296  sources = [ "unit_test_service.cc" ]
297  defines = []
298
299  if (pw_unit_test_BACKEND == "$dir_pw_unit_test:light") {
300    public_configs += [ ":rpc_service_backend_light" ]
301    sources += [ "rpc_light_event_handler.cc" ]
302    public += [ "rpc_light_public/pw_unit_test/internal/rpc_event_handler.h" ]
303  } else {
304    public_configs += [ ":rpc_service_backend_gtest" ]
305    sources += [ "rpc_gtest_event_handler.cc" ]
306    public += [ "rpc_gtest_public/pw_unit_test/internal/rpc_event_handler.h" ]
307  }
308}
309
310pw_source_set("rpc_main") {
311  testonly = pw_unit_test_TESTONLY
312  public_deps = [ ":pw_unit_test" ]
313  deps = [
314    ":rpc_service",
315    "$dir_pw_rpc/system_server",
316    dir_pw_log,
317  ]
318  sources = [ "rpc_main.cc" ]
319}
320
321if (pw_unit_test_BACKEND == "$dir_pw_unit_test:light") {
322  pw_source_set("static_library_support") {
323    public_configs = [ ":public_include_path" ]
324    public_deps = [ pw_unit_test_BACKEND ]
325    public = [ "public/pw_unit_test/static_library_support.h" ]
326    sources = [ "static_library_support.cc" ]
327  }
328} else {
329  group("static_library_support") {
330  }
331}
332
333pw_executable("test_rpc_server") {
334  testonly = pw_unit_test_TESTONLY
335  sources = [ "test_rpc_server.cc" ]
336  deps = [
337    ":pw_unit_test",
338    ":rpc_service",
339    "$dir_pw_rpc/system_server",
340    "$dir_pw_rpc/system_server:socket",
341    dir_pw_log,
342  ]
343}
344
345pw_proto_library("unit_test_proto") {
346  sources = [ "pw_unit_test_proto/unit_test.proto" ]
347}
348
349pw_doc_group("docs") {
350  sources = [ "docs.rst" ]
351  other_deps = [ "py" ]
352}
353
354pw_test("metadata_only_test") {
355  extra_metadata = {
356    extra_key = "extra_value"
357  }
358}
359
360# pw_test_group produces the metadata file for its tests.
361pw_test_group("metadata_only_group") {
362  tests = [ ":metadata_only_test" ]
363  output_metadata = true
364}
365
366pw_python_action_test("test_group_metadata_test") {
367  testonly = pw_unit_test_TESTONLY
368  sources = [ "py/test_group_metadata_test.py" ]
369  args = [
370    "--stamp-path",
371    "<TARGET_FILE(:metadata_only_group)>",
372  ]
373  deps = [ ":metadata_only_group" ]
374}
375
376pw_test("framework_test") {
377  sources = [ "framework_test.cc" ]
378  deps = [
379    dir_pw_assert,
380    dir_pw_status,
381  ]
382
383  # TODO: https://pwbug.dev/325509758 - Passes but hangs on cleanup.
384  if (pw_build_EXECUTABLE_TARGET_TYPE == "pico_executable") {
385    enable_if = false
386  }
387}
388
389pw_test("framework_light_test") {
390  enable_if = pw_unit_test_BACKEND == "$dir_pw_unit_test:light"
391  sources = [ "framework_light_test.cc" ]
392  deps = [
393    "$dir_pw_string:builder",
394    dir_pw_status,
395  ]
396}
397
398pw_static_library("tests_in_archive") {
399  testonly = pw_unit_test_TESTONLY
400  sources = [
401    "static_library_archived_tests.cc",
402    "static_library_missing_archived_tests.cc",
403  ]
404  deps = [ ":pw_unit_test" ]
405  visibility = [ ":*" ]
406}
407
408pw_test("static_library_support_test") {
409  enable_if = pw_unit_test_BACKEND == "$dir_pw_unit_test:light"
410  sources = [ "static_library_support_test.cc" ]
411  deps = [
412    ":static_library_support",
413    ":tests_in_archive",
414    dir_pw_assert,
415  ]
416}
417
418pw_test_group("tests") {
419  tests = [
420    ":framework_test",
421    ":framework_light_test",
422    ":static_library_support_test",
423    ":multi_event_handler_test",
424    ":test_record_event_handler_test",
425  ]
426  if (dir_pw_third_party_googletest != "") {
427    tests += [ ":googletest_test_matchers_test" ]
428  }
429}
430