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