testfscache.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """Unit tests for file system cache."""
  2. from __future__ import annotations
  3. import os
  4. import shutil
  5. import sys
  6. import tempfile
  7. import unittest
  8. from mypy.fscache import FileSystemCache
  9. class TestFileSystemCache(unittest.TestCase):
  10. def setUp(self) -> None:
  11. self.tempdir = tempfile.mkdtemp()
  12. self.oldcwd = os.getcwd()
  13. os.chdir(self.tempdir)
  14. self.fscache = FileSystemCache()
  15. def tearDown(self) -> None:
  16. os.chdir(self.oldcwd)
  17. shutil.rmtree(self.tempdir)
  18. def test_isfile_case_1(self) -> None:
  19. self.make_file("bar.py")
  20. self.make_file("pkg/sub_package/__init__.py")
  21. self.make_file("pkg/sub_package/foo.py")
  22. # Run twice to test both cached and non-cached code paths.
  23. for i in range(2):
  24. assert self.isfile_case("bar.py")
  25. assert self.isfile_case("pkg/sub_package/__init__.py")
  26. assert self.isfile_case("pkg/sub_package/foo.py")
  27. assert not self.isfile_case("non_existent.py")
  28. assert not self.isfile_case("pkg/non_existent.py")
  29. assert not self.isfile_case("pkg/")
  30. assert not self.isfile_case("bar.py/")
  31. for i in range(2):
  32. assert not self.isfile_case("Bar.py")
  33. assert not self.isfile_case("pkg/sub_package/__init__.PY")
  34. assert not self.isfile_case("pkg/Sub_Package/foo.py")
  35. assert not self.isfile_case("Pkg/sub_package/foo.py")
  36. def test_isfile_case_2(self) -> None:
  37. self.make_file("bar.py")
  38. self.make_file("pkg/sub_package/__init__.py")
  39. self.make_file("pkg/sub_package/foo.py")
  40. # Run twice to test both cached and non-cached code paths.
  41. # This reverses the order of checks from test_isfile_case_1.
  42. for i in range(2):
  43. assert not self.isfile_case("Bar.py")
  44. assert not self.isfile_case("pkg/sub_package/__init__.PY")
  45. assert not self.isfile_case("pkg/Sub_Package/foo.py")
  46. assert not self.isfile_case("Pkg/sub_package/foo.py")
  47. for i in range(2):
  48. assert self.isfile_case("bar.py")
  49. assert self.isfile_case("pkg/sub_package/__init__.py")
  50. assert self.isfile_case("pkg/sub_package/foo.py")
  51. assert not self.isfile_case("non_existent.py")
  52. assert not self.isfile_case("pkg/non_existent.py")
  53. def test_isfile_case_3(self) -> None:
  54. self.make_file("bar.py")
  55. self.make_file("pkg/sub_package/__init__.py")
  56. self.make_file("pkg/sub_package/foo.py")
  57. # Run twice to test both cached and non-cached code paths.
  58. for i in range(2):
  59. assert self.isfile_case("bar.py")
  60. assert not self.isfile_case("non_existent.py")
  61. assert not self.isfile_case("pkg/non_existent.py")
  62. assert not self.isfile_case("Bar.py")
  63. assert not self.isfile_case("pkg/sub_package/__init__.PY")
  64. assert not self.isfile_case("pkg/Sub_Package/foo.py")
  65. assert not self.isfile_case("Pkg/sub_package/foo.py")
  66. assert self.isfile_case("pkg/sub_package/__init__.py")
  67. assert self.isfile_case("pkg/sub_package/foo.py")
  68. def test_isfile_case_other_directory(self) -> None:
  69. self.make_file("bar.py")
  70. with tempfile.TemporaryDirectory() as other:
  71. self.make_file("other_dir.py", base=other)
  72. self.make_file("pkg/other_dir.py", base=other)
  73. assert self.isfile_case(os.path.join(other, "other_dir.py"))
  74. assert not self.isfile_case(os.path.join(other, "Other_Dir.py"))
  75. assert not self.isfile_case(os.path.join(other, "bar.py"))
  76. if sys.platform in ("win32", "darwin"):
  77. # We only check case for directories under our prefix, and since
  78. # this path is not under the prefix, case difference is fine.
  79. assert self.isfile_case(os.path.join(other, "PKG/other_dir.py"))
  80. def make_file(self, path: str, base: str | None = None) -> None:
  81. if base is None:
  82. base = self.tempdir
  83. fullpath = os.path.join(base, path)
  84. os.makedirs(os.path.dirname(fullpath), exist_ok=True)
  85. if not path.endswith("/"):
  86. with open(fullpath, "w") as f:
  87. f.write("# test file")
  88. def isfile_case(self, path: str) -> bool:
  89. return self.fscache.isfile_case(os.path.join(self.tempdir, path), self.tempdir)