python.bzl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. load("@bazel_skylib//lib:paths.bzl", "paths")
  2. # py_test_module_list creates a py_test target for each
  3. # Python file in `files`
  4. def doctest(files, gpu = False, name="doctest", deps=[], srcs=[], data=[], args=[], size="medium", tags=[], **kwargs):
  5. # NOTE: If you run `pytest` on `__init__.py`, it tries to test all files in that
  6. # package. We don't want that, so we exclude it from the list of input files.
  7. files = native.glob(include=files, exclude=["__init__.py"])
  8. if gpu:
  9. name += "[gpu]"
  10. tags = tags + ["gpu"]
  11. else:
  12. tags = tags + ["cpu"]
  13. native.py_test(
  14. name = name,
  15. srcs = ["//bazel:pytest_wrapper.py"] + srcs,
  16. main = "//bazel:pytest_wrapper.py",
  17. size = size,
  18. args = [
  19. "--doctest-modules",
  20. "--doctest-glob='*.md'",
  21. "-c=$(location //bazel:conftest.py)",
  22. "--disable-warnings",
  23. "-v"
  24. ] + args + ["$(location :%s)" % file for file in files],
  25. data = ["//bazel:conftest.py"] + files + data,
  26. python_version = "PY3",
  27. srcs_version = "PY3",
  28. tags = ["doctest"] + tags,
  29. deps = ["//:ray_lib"] + deps,
  30. **kwargs
  31. )
  32. def py_test_module_list(files, size, deps, extra_srcs=[], name_suffix="", **kwargs):
  33. for file in files:
  34. # remove .py
  35. name = paths.split_extension(file)[0] + name_suffix
  36. if name == file:
  37. basename = basename + "_test"
  38. native.py_test(
  39. name = name,
  40. size = size,
  41. main = file,
  42. srcs = extra_srcs + [file],
  43. deps = deps,
  44. **kwargs
  45. )
  46. def py_test_run_all_subdirectory(include, exclude, extra_srcs, **kwargs):
  47. for file in native.glob(include = include, exclude = exclude, allow_empty=False):
  48. basename = paths.split_extension(file)[0]
  49. if basename == file:
  50. basename = basename + "_test"
  51. native.py_test(
  52. name = basename,
  53. srcs = extra_srcs + [file],
  54. **kwargs
  55. )
  56. # Runs all included notebooks as py_test targets, by first converting them to .py files with "test_myst_doc.py".
  57. def py_test_run_all_notebooks(include, exclude, allow_empty=False, **kwargs):
  58. for file in native.glob(include = include, exclude = exclude, allow_empty=allow_empty):
  59. print(file)
  60. basename = paths.split_extension(file)[0]
  61. if basename == file:
  62. basename = basename + "_test"
  63. native.py_test(
  64. name = basename,
  65. main = "test_myst_doc.py",
  66. srcs = ["//doc:test_myst_doc.py"],
  67. # --find-recursively will look for file in all
  68. # directories inside cwd recursively if it cannot
  69. # find it right away. This allows to deal with
  70. # mismatches between `name` and `data` args.
  71. args = ["--find-recursively", "--path", file],
  72. **kwargs
  73. )