lit.bzl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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, **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. **kwargs: Any additional parameters for the generated py_test.
  15. """
  16. test_files = native.glob(
  17. ["**"],
  18. exclude_directories = 1,
  19. )
  20. data.append("@llvm-project//llvm:lit")
  21. suites = dict()
  22. for f in test_files:
  23. if f.split(".")[-1] not in test_file_exts:
  24. continue
  25. test = "%s.test" % f
  26. native.py_test(
  27. name = test,
  28. srcs = ["//bazel/testing:lit_test.py"],
  29. main = "//bazel/testing:lit_test.py",
  30. data = data + [driver, f],
  31. args = ["--package_name=%s" % native.package_name(), "--"],
  32. **kwargs
  33. )
  34. # Cluster tests by directory in order to produce suites. For example,
  35. # foo/bar/baz.carbon.test is added to suites :foo and :foo/bar.
  36. dirs = f.split("/")[:-1]
  37. for num_parts in range(1, 1 + len(dirs)):
  38. dir = "/".join(dirs[:num_parts])
  39. if dir not in suites:
  40. suites[dir] = []
  41. suites[dir].append(test)
  42. for suite, tests in suites.items():
  43. native.test_suite(name = suite, tests = tests)