rules.bzl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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("@rules_shell//shell:sh_test.bzl", "sh_test")
  9. load("//bazel/cc_rules:defs.bzl", "cc_test")
  10. load("//bazel/manifest:defs.bzl", "manifest", "manifest_as_cpp")
  11. def file_test(
  12. name,
  13. tests,
  14. srcs = [],
  15. deps = [],
  16. data = [],
  17. args = [],
  18. prebuilt_binary = None,
  19. **kwargs):
  20. """Generates tests using the file_test base.
  21. There will be one main test using `name` that can be sharded, and includes
  22. all files. Additionally, per-file tests will be generated as
  23. `name.file_path`; these per-file tests will be manual.
  24. Args:
  25. name: The base name of the tests.
  26. tests: The list of test files to use as data, typically a glob.
  27. srcs: Passed to cc_test.
  28. deps: Passed to cc_test.
  29. data: Passed to cc_test.
  30. args: Passed to cc_test.
  31. prebuilt_binary: If set, specifies a prebuilt test binary to use instead
  32. of building a new one.
  33. **kwargs: Passed to cc_test.
  34. """
  35. # Ensure tests are always a filegroup for tests_as_input_file_rule.
  36. if prebuilt_binary:
  37. # TODO: The prebuilt_binary support is only used by explorer. We should
  38. # remove this once explorer is removed, and think about better factoring
  39. # approaches if we need it later for toolchain.
  40. tests_file = "{0}.tests".format(name)
  41. manifest(
  42. name = tests_file,
  43. srcs = tests,
  44. testonly = 1,
  45. )
  46. args = ["--explorer_test_targets_file=$(rootpath :{0})".format(tests_file)] + args
  47. sh_test(
  48. name = name,
  49. srcs = srcs + [prebuilt_binary],
  50. deps = deps,
  51. data = [":" + tests_file] + tests + data,
  52. args = args,
  53. **kwargs
  54. )
  55. else:
  56. manifest_cpp = "{0}_autogen_manifest.cpp".format(name)
  57. manifest_as_cpp(
  58. name = manifest_cpp,
  59. var_name = "CarbonFileTestManifest",
  60. srcs = tests,
  61. testonly = 1,
  62. )
  63. cc_test(
  64. name = name,
  65. srcs = srcs + [":" + manifest_cpp],
  66. deps = deps + ["//testing/file_test:manifest_impl"],
  67. data = tests + data,
  68. args = args,
  69. **kwargs
  70. )