rules.bzl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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_cc//cc:defs.bzl", "cc_test")
  9. load("//bazel/manifest:defs.bzl", "manifest")
  10. def file_test(
  11. name,
  12. tests,
  13. data = [],
  14. args = [],
  15. prebuilt_binary = None,
  16. **kwargs):
  17. """Generates tests using the file_test base.
  18. There will be one main test using `name` that can be sharded, and includes
  19. all files. Additionally, per-file tests will be generated as
  20. `name.file_path`; these per-file tests will be manual.
  21. Args:
  22. name: The base name of the tests.
  23. tests: The list of test files to use as data, typically a glob.
  24. data: Passed to cc_test.
  25. args: Passed to cc_test.
  26. prebuilt_binary: If set, specifies a prebuilt test binary to use instead
  27. of building a new one.
  28. **kwargs: Passed to cc_test.
  29. """
  30. # Ensure tests are always a filegroup for tests_as_input_file_rule.
  31. tests_file = "{0}.tests".format(name)
  32. manifest(
  33. name = tests_file,
  34. srcs = tests,
  35. testonly = 1,
  36. )
  37. args = ["--test_targets_file=$(rootpath :{0})".format(tests_file)] + args
  38. data = [":" + tests_file] + tests + data
  39. if prebuilt_binary:
  40. native.sh_test(
  41. name = name,
  42. srcs = [prebuilt_binary],
  43. data = data,
  44. args = args,
  45. **kwargs
  46. )
  47. else:
  48. cc_test(
  49. name = name,
  50. data = data,
  51. args = args,
  52. **kwargs
  53. )