rules.bzl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. DataFilesInfo = provider(
  10. "Data files for this target.",
  11. fields = {
  12. "data_files": "Data files for this target",
  13. },
  14. )
  15. def _tests_as_input_file_rule_impl(ctx):
  16. data_files = []
  17. for tests in ctx.attr.data:
  18. data_files.extend(
  19. [f.path for f in tests[DefaultInfo].data_runfiles.files.to_list()],
  20. )
  21. data_files.extend(
  22. [f.path for f in tests[DefaultInfo].files.to_list()],
  23. )
  24. ctx.actions.write(ctx.outputs.data_files, "\n".join(data_files) + "\n")
  25. _tests_as_input_file_rule = rule(
  26. attrs = {
  27. "data": attr.label_list(allow_files = True),
  28. },
  29. outputs = {
  30. "data_files": "%{name}.txt",
  31. },
  32. implementation = _tests_as_input_file_rule_impl,
  33. )
  34. def file_test(name, tests, data = [], args = [], **kwargs):
  35. """Generates tests using the file_test base.
  36. There will be one main test using `name` that can be sharded, and includes
  37. all files. Additionally, per-file tests will be generated as
  38. `name.file_path`; these per-file tests will be manual.
  39. Args:
  40. name: The base name of the tests.
  41. tests: The list of test files to use as data, typically a glob.
  42. data: Passed to cc_test.
  43. args: Passed to cc_test.
  44. **kwargs: Passed to cc_test.
  45. """
  46. # Ensure tests are always a filegroup for tests_as_input_file_rule.
  47. tests_file = "{0}.tests".format(name)
  48. _tests_as_input_file_rule(
  49. name = tests_file,
  50. data = tests,
  51. testonly = 1,
  52. )
  53. cc_test(
  54. name = name,
  55. data = [tests_file] + tests + data,
  56. args = ["--test_targets_file=$(rootpath :{0})".format(tests_file)] + args,
  57. **kwargs
  58. )