rules.bzl 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. """Rule for a lit test."""
  5. def glob_lit_tests(driver, data, test_file_exts, exclude = None, **kwargs):
  6. """Runs `lit` on test_dir.
  7. `lit` reference:
  8. https://llvm.org/docs/CommandGuide/lit.html
  9. Args:
  10. driver: The path to the lit config.
  11. data: A list of tools to provide to the tests. These will be aliased for
  12. execution.
  13. test_file_exts: A list of extensions to use for tests.
  14. exclude: A list of paths to exclude from the glob.
  15. **kwargs: Any additional parameters for the generated py_test.
  16. """
  17. if not exclude:
  18. exclude = []
  19. test_files = native.glob(
  20. ["**"],
  21. exclude = exclude,
  22. exclude_directories = 1,
  23. )
  24. data.append("@llvm-project//llvm:lit")
  25. suites = dict()
  26. for f in test_files:
  27. if f.split(".")[-1] not in test_file_exts:
  28. continue
  29. test = "%s.test" % f
  30. native.py_test(
  31. name = test,
  32. srcs = ["//testing/lit_test:lit_test.py"],
  33. main = "//testing/lit_test:lit_test.py",
  34. data = data + [driver, f],
  35. args = ["--package_name=%s" % native.package_name(), "--"],
  36. size = "small",
  37. **kwargs
  38. )
  39. # Cluster tests by directory in order to produce suites. For example,
  40. # foo/bar/baz.carbon.test is added to suites :foo and :foo/bar.
  41. dirs = f.split("/")[:-1]
  42. for num_parts in range(1, 1 + len(dirs)):
  43. dir = "/".join(dirs[:num_parts])
  44. if dir not in suites:
  45. suites[dir] = []
  46. suites[dir].append(test)
  47. for suite, tests in suites.items():
  48. native.test_suite(name = suite, tests = tests)