test_check_mypy_stronglist.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import re
  2. from tools.mypy_helpers.check_stronglist import _glob_to_re, main
  3. def test_glob_to_re_exact_matches():
  4. pat = re.compile(_glob_to_re("a.b.c"))
  5. assert pat.fullmatch("a.b.c")
  6. assert not pat.fullmatch("a.b.c.d")
  7. assert not pat.fullmatch("a_b_c")
  8. def test_glob_to_re_wildcards():
  9. pat = re.compile(_glob_to_re("a.b.c.*"))
  10. assert pat.fullmatch("a.b.c")
  11. assert pat.fullmatch("a.b.c.d")
  12. assert not pat.fullmatch("a_b_c")
  13. def test_ok(tmp_path):
  14. src = """\
  15. [[tool.mypy.overrides]]
  16. module = ["a.b.c", "d.e.f", "g.h.i"]
  17. disable_error_code = ["misc"]
  18. [[tool.mypy.overrides]]
  19. module = ["j.k.*"]
  20. disallow_untyped_defs = true
  21. """
  22. f = tmp_path.joinpath("f")
  23. f.write_text(src)
  24. assert main((str(f),)) == 0
  25. def test_errors_on_exact_module(tmp_path, capsys):
  26. src = """\
  27. [[tool.mypy.overrides]]
  28. module = ["a.b.c", "d.e.f", "g.h.i"]
  29. disable_error_code = ["misc"]
  30. [[tool.mypy.overrides]]
  31. module = ["a.b.c", "d.e.f"]
  32. disallow_untyped_defs = true
  33. """
  34. f = tmp_path.joinpath("f")
  35. f.write_text(src)
  36. assert main((str(f),)) == 1
  37. expected = f"""\
  38. {f}: a.b.c is in the typing errors allowlist *and* stronglist
  39. {f}: d.e.f is in the typing errors allowlist *and* stronglist
  40. """
  41. assert capsys.readouterr().out == expected
  42. def test_errors_on_globbed_module(tmp_path, capsys):
  43. src = """\
  44. [[tool.mypy.overrides]]
  45. module = ["a.b.c", "a.b.c.d", "a.b.c.e"]
  46. disable_error_code = ["misc"]
  47. [[tool.mypy.overrides]]
  48. module = ["a.b.c.*"]
  49. disallow_untyped_defs = true
  50. """
  51. f = tmp_path.joinpath("f")
  52. f.write_text(src)
  53. assert main((str(f),)) == 1
  54. expected = f"""\
  55. {f}: a.b.c is in the typing errors allowlist *and* stronglist
  56. {f}: a.b.c.d is in the typing errors allowlist *and* stronglist
  57. {f}: a.b.c.e is in the typing errors allowlist *and* stronglist
  58. """
  59. assert capsys.readouterr().out == expected