test_check_stronglist.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. tmp_path.joinpath("j/k").mkdir(parents=True)
  25. tmp_path.joinpath("j/k/l.py").touch()
  26. assert main((str(f),)) == 0
  27. def test_errors_on_exact_module(tmp_path, capsys):
  28. src = """\
  29. [[tool.mypy.overrides]]
  30. module = ["a.b.c", "d.e.f", "g.h.i"]
  31. disable_error_code = ["misc"]
  32. [[tool.mypy.overrides]]
  33. module = ["a.b.c", "d.e.f"]
  34. disallow_untyped_defs = true
  35. """
  36. f = tmp_path.joinpath("f")
  37. f.write_text(src)
  38. tmp_path.joinpath("a/b").mkdir(parents=True)
  39. tmp_path.joinpath("a/b/c.py").touch()
  40. tmp_path.joinpath("d/e").mkdir(parents=True)
  41. tmp_path.joinpath("d/e/f.py").touch()
  42. assert main((str(f),)) == 1
  43. expected = f"""\
  44. {f}: a.b.c is in the typing errors allowlist *and* stronglist
  45. {f}: d.e.f is in the typing errors allowlist *and* stronglist
  46. """
  47. assert capsys.readouterr().out == expected
  48. def test_errors_on_globbed_module(tmp_path, capsys):
  49. src = """\
  50. [[tool.mypy.overrides]]
  51. module = ["a.b.c", "a.b.c.d", "a.b.c.e"]
  52. disable_error_code = ["misc"]
  53. [[tool.mypy.overrides]]
  54. module = ["a.b.c.*"]
  55. disallow_untyped_defs = true
  56. """
  57. f = tmp_path.joinpath("f")
  58. f.write_text(src)
  59. tmp_path.joinpath("a/b/c").mkdir(parents=True)
  60. tmp_path.joinpath("a/b/c/d.py").touch()
  61. assert main((str(f),)) == 1
  62. expected = f"""\
  63. {f}: a.b.c is in the typing errors allowlist *and* stronglist
  64. {f}: a.b.c.d is in the typing errors allowlist *and* stronglist
  65. {f}: a.b.c.e is in the typing errors allowlist *and* stronglist
  66. """
  67. assert capsys.readouterr().out == expected
  68. def test_stronglist_existence_file_missing(tmp_path, capsys):
  69. src = """\
  70. [[tool.mypy.overrides]]
  71. module = []
  72. disable_error_code = ["misc"]
  73. [[tool.mypy.overrides]]
  74. module = ["a.b"]
  75. disallow_untyped_defs = true
  76. """
  77. f = tmp_path.joinpath("f")
  78. f.write_text(src)
  79. assert main((str(f),)) == 1
  80. expected = f"""\
  81. {f}: a.b in stronglist does not match any files!
  82. """
  83. assert capsys.readouterr().out == expected
  84. def test_stronglist_existence_glob_missing(tmp_path, capsys):
  85. src = """\
  86. [[tool.mypy.overrides]]
  87. module = []
  88. disable_error_code = ["misc"]
  89. [[tool.mypy.overrides]]
  90. module = ["a.*"]
  91. disallow_untyped_defs = true
  92. """
  93. f = tmp_path.joinpath("f")
  94. f.write_text(src)
  95. assert main((str(f),)) == 1
  96. expected = f"""\
  97. {f}: a.* in stronglist does not match any files!
  98. """
  99. assert capsys.readouterr().out == expected
  100. def test_stronglist_existence_ok_src_layout(tmp_path):
  101. src = """\
  102. [[tool.mypy.overrides]]
  103. module = []
  104. disable_error_code = ["misc"]
  105. [[tool.mypy.overrides]]
  106. module = ["a.b"]
  107. disallow_untyped_defs = true
  108. """
  109. f = tmp_path.joinpath("f")
  110. f.write_text(src)
  111. tmp_path.joinpath("src/a").mkdir(parents=True)
  112. tmp_path.joinpath("src/a/b.py").touch()
  113. assert main((str(f),)) == 0
  114. def test_stronglist_existence_ok_glob(tmp_path):
  115. src = """\
  116. [[tool.mypy.overrides]]
  117. module = []
  118. disable_error_code = ["misc"]
  119. [[tool.mypy.overrides]]
  120. module = ["a.*"]
  121. disallow_untyped_defs = true
  122. """
  123. f = tmp_path.joinpath("f")
  124. f.write_text(src)
  125. tmp_path.joinpath("a").mkdir(parents=True)
  126. tmp_path.joinpath("a/c.py").touch()
  127. assert main((str(f),)) == 0
  128. def test_stronglist_existince_ok_init_py(tmp_path):
  129. src = """\
  130. [[tool.mypy.overrides]]
  131. module = []
  132. disable_error_code = ["misc"]
  133. [[tool.mypy.overrides]]
  134. module = ["a.b"]
  135. disallow_untyped_defs = true
  136. """
  137. f = tmp_path.joinpath("f")
  138. f.write_text(src)
  139. tmp_path.joinpath("a/b").mkdir(parents=True)
  140. tmp_path.joinpath("a/b/__init__.py").touch()
  141. assert main((str(f),)) == 0