rules.bzl 1.9 KB

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