rules.bzl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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(name, 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. This will generate three types of rules:
  10. 1. For each test file, a rule with ".test" at the end. For example,
  11. "foo/bar/baz.carbon" will generate ":foo/bar/baz.carbon.test"
  12. 2. If any test exists in subdirectory, a test_suite is generated for each
  13. directory. For example, ":foo" will run all tests under directory "foo",
  14. and ":foo/bar" will run all tests under "foo/bar".
  15. 3. A test_suite containing all tests. The name of this will be the "name"
  16. arg passed to the glob_lit_tests rule.
  17. Args:
  18. name: The name of the test_suite rule to generate for running all tests.
  19. driver: The path to the lit config.
  20. data: A list of tools to provide to the tests. These will be aliased for
  21. execution.
  22. test_file_exts: A list of extensions to use for tests.
  23. exclude: A list of paths to exclude from the glob.
  24. **kwargs: Any additional parameters for the generated py_test.
  25. """
  26. if not exclude:
  27. exclude = []
  28. test_files = native.glob(
  29. ["**"],
  30. exclude = exclude,
  31. exclude_directories = 1,
  32. )
  33. data.append("@llvm-project//llvm:lit")
  34. suites = dict()
  35. all_tests = list()
  36. for f in test_files:
  37. if f.split(".")[-1] not in test_file_exts:
  38. continue
  39. test = "%s.test" % f
  40. all_tests.append(test)
  41. native.py_test(
  42. name = test,
  43. srcs = ["//testing/lit_test:lit_test.py"],
  44. main = "//testing/lit_test:lit_test.py",
  45. data = data + [driver, f],
  46. args = ["--package_name=%s" % native.package_name(), "--"],
  47. size = "small",
  48. **kwargs
  49. )
  50. # Cluster tests by directory in order to produce suites. For example,
  51. # foo/bar/baz.carbon.test is added to suites :foo and :foo/bar.
  52. dirs = f.split("/")[:-1]
  53. for num_parts in range(1, 1 + len(dirs)):
  54. dir = "/".join(dirs[:num_parts])
  55. if dir not in suites:
  56. suites[dir] = []
  57. suites[dir].append(test)
  58. native.test_suite(name = name, tests = all_tests)
  59. for suite, tests in suites.items():
  60. native.test_suite(name = suite, tests = tests)