rules.bzl 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. """Rules for building file tests.
  5. file_test uses the tests_as_input_file rule to transform test dependencies into
  6. a file which can be accessed as a list. This avoids long argument parsing.
  7. """
  8. load("//bazel/cc_rules:defs.bzl", "cc_test")
  9. load("//bazel/manifest:defs.bzl", "manifest", "manifest_as_cpp")
  10. def file_test(
  11. name,
  12. tests,
  13. srcs = [],
  14. deps = [],
  15. data = [],
  16. args = [],
  17. prebuilt_binary = None,
  18. **kwargs):
  19. """Generates tests using the file_test base.
  20. There will be one main test using `name` that can be sharded, and includes
  21. all files. Additionally, per-file tests will be generated as
  22. `name.file_path`; these per-file tests will be manual.
  23. Args:
  24. name: The base name of the tests.
  25. tests: The list of test files to use as data, typically a glob.
  26. srcs: Passed to cc_test.
  27. deps: Passed to cc_test.
  28. data: Passed to cc_test.
  29. args: Passed to cc_test.
  30. prebuilt_binary: If set, specifies a prebuilt test binary to use instead
  31. of building a new one.
  32. **kwargs: Passed to cc_test.
  33. """
  34. # Ensure tests are always a filegroup for tests_as_input_file_rule.
  35. if prebuilt_binary:
  36. # TODO: The prebuilt_binary support is only used by explorer. We should
  37. # remove this once explorer is removed, and think about better factoring
  38. # approaches if we need it later for toolchain.
  39. tests_file = "{0}.tests".format(name)
  40. manifest(
  41. name = tests_file,
  42. srcs = tests,
  43. testonly = 1,
  44. )
  45. args = ["--explorer_test_targets_file=$(rootpath :{0})".format(tests_file)] + args
  46. native.sh_test(
  47. name = name,
  48. srcs = srcs + [prebuilt_binary],
  49. deps = deps,
  50. data = [":" + tests_file] + tests + data,
  51. args = args,
  52. **kwargs
  53. )
  54. else:
  55. manifest_cpp = "{0}_autogen_manifest.cpp".format(name)
  56. manifest_as_cpp(
  57. name = manifest_cpp,
  58. var_name = "CarbonFileTestManifest",
  59. srcs = tests,
  60. testonly = 1,
  61. )
  62. cc_test(
  63. name = name,
  64. srcs = srcs + [":" + manifest_cpp],
  65. deps = deps + ["//testing/file_test:manifest_impl"],
  66. data = tests + data,
  67. args = args,
  68. **kwargs
  69. )