diff options
author | mrbean-bremen <mrbean-bremen@users.noreply.github.com> | 2023-05-30 19:56:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-30 19:56:01 +0200 |
commit | 510e2bfb40ddf53bd20a35c4f00ffb27faecce24 (patch) | |
tree | 88226f6c216380c96760f53ea09d1dbccbc0a8be | |
parent | 403e21d79af73416ac91fa0e311657cb573844e4 (diff) | |
download | pyfakefs-510e2bfb40ddf53bd20a35c4f00ffb27faecce24.tar.gz |
Adapt to changes in Python 3.12 beta1 (#835)
- distutils has been removed
- variable renamed in pathlib
- add workaround for patching open, comment out not working tests
- hopefully fixes #830 and #831
-rw-r--r-- | .github/workflows/testsuite.yml | 4 | ||||
-rw-r--r-- | CHANGES.md | 2 | ||||
-rw-r--r-- | pyfakefs/fake_filesystem_unittest.py | 8 | ||||
-rw-r--r-- | pyfakefs/fake_pathlib.py | 6 | ||||
-rw-r--r-- | pyfakefs/tests/fake_filesystem_unittest_test.py | 61 |
5 files changed, 52 insertions, 29 deletions
diff --git a/.github/workflows/testsuite.yml b/.github/workflows/testsuite.yml index 77654f3..33aee2b 100644 --- a/.github/workflows/testsuite.yml +++ b/.github/workflows/testsuite.yml @@ -14,7 +14,7 @@ jobs: with: python-version: "3.10" - name: install pytype - run: pip install pytype pytest scandir pathlib2 pandas xlrd django + run: pip install setuptools pytype pytest scandir pathlib2 pandas xlrd django - name: Run pytype run: | pytype pyfakefs --keep-going --exclude pyfakefs/tests/* --exclude pyfakefs/pytest_tests/* @@ -54,7 +54,7 @@ jobs: - name: Install dependencies run: | - pip install wheel + pip install setuptools wheel pip install -r requirements.txt - name: Run unit tests without extra packages as non-root user run: | @@ -8,6 +8,8 @@ The released versions correspond to PyPI releases. (see [#814](../../issues/814)). * Exclude pytest `pathlib` modules from patching to avoid mixup of patched/unpatched code (see [#814](../../issues/814)). +* Adapt to changes in Python 3.12 beta1 (only working partially, + see [#830](../../issues/830) and [#831](../../issues/831)) ### Infrastructure * Added pytype check for non-test modules in CI (see [#599](../../issues/599)). diff --git a/pyfakefs/fake_filesystem_unittest.py b/pyfakefs/fake_filesystem_unittest.py index ea5edef..386f215 100644 --- a/pyfakefs/fake_filesystem_unittest.py +++ b/pyfakefs/fake_filesystem_unittest.py @@ -36,6 +36,7 @@ pyfakefs by simply changing their base class from `:py:class`unittest.TestCase` to `:py:class`pyfakefs.fake_filesystem_unittest.TestCase`. """ import _io # type:ignore[import] +import builtins import doctest import functools import genericpath @@ -455,7 +456,7 @@ class Patcher: os.path, } if sys.platform == "win32": - import nt # type:ignore [import] + import nt # type:ignore[import] import ntpath SKIPMODULES.add(nt) @@ -559,6 +560,7 @@ class Patcher: # save the original open function for use in pytest plugin self.original_open = open self.patch_open_code = patch_open_code + self.fake_open: fake_open.FakeFileOpen if additional_skip_names is not None: skip_names = [ @@ -844,6 +846,7 @@ class Patcher: self.fs = fake_filesystem.FakeFilesystem(patcher=self, create_temp_dir=True) self.fs.patch_open_code = self.patch_open_code + self.fake_open = fake_open.FakeFileOpen(self.fs) for name in self._fake_module_classes: self.fake_modules[name] = self._fake_module_classes[name](self.fs) if hasattr(self.fake_modules[name], "skip_names"): @@ -922,6 +925,9 @@ class Patcher: for module, attr in modules: if attr in self.unfaked_modules: self._stubs.smart_set(module, name, self.unfaked_modules[attr]) + if sys.version_info >= (3, 12): + # workaround for patching open - does not work with skip modules + self._stubs.smart_set(builtins, "open", self.fake_open) def patch_defaults(self) -> None: for fct, idx, ft in self.FS_DEFARGS: diff --git a/pyfakefs/fake_pathlib.py b/pyfakefs/fake_pathlib.py index fbca1bb..17c3ebb 100644 --- a/pyfakefs/fake_pathlib.py +++ b/pyfakefs/fake_pathlib.py @@ -764,12 +764,12 @@ class FakePath(pathlib.Path): return os.path.isabs(self._path()) def is_reserved(self): - if not self.filesystem.is_windows_fs or not self._parts: + if not self.filesystem.is_windows_fs or not self._tail: return False - if self._parts[0].startswith("\\\\"): + if self._tail[0].startswith("\\\\"): # UNC paths are never reserved. return False - name = self._parts[-1].partition(".")[0].partition(":")[0].rstrip(" ") + name = self._tail[-1].partition(".")[0].partition(":")[0].rstrip(" ") return name.upper() in pathlib._WIN_RESERVED_NAMES diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py index 88b08d1..6d3e6e6 100644 --- a/pyfakefs/tests/fake_filesystem_unittest_test.py +++ b/pyfakefs/tests/fake_filesystem_unittest_test.py @@ -28,7 +28,6 @@ import sys import tempfile import unittest import warnings -from distutils.dir_util import copy_tree, remove_tree from pathlib import Path from unittest import TestCase, mock @@ -44,6 +43,10 @@ from pyfakefs.fake_filesystem_unittest import ( ) from pyfakefs.tests.fixtures import module_with_attributes +if sys.version_info < (3, 12): + # distutils removed in Python 3.12 + from distutils.dir_util import copy_tree, remove_tree + class TestPatcher(TestCase): def test_context_manager(self): @@ -152,9 +155,9 @@ class TestPyfakefsUnittest(TestPyfakefsUnittestBase): # pylint: disable=R0904 self.assertFalse(self.fs.exists("/test/dir1")) def test_fakepathlib(self): - with pathlib.Path("/fake_file.txt") as p: - with p.open("w") as f: - f.write("text") + p = pathlib.Path("/fake_file.txt") + with p.open("w") as f: + f.write("text") is_windows = sys.platform.startswith("win") if is_windows: self.assertTrue(self.fs.exists(r"\fake_file.txt")) @@ -225,12 +228,14 @@ class TestPatchingImports(TestPyfakefsUnittestBase): stat_result = pyfakefs.tests.import_as_example.file_stat2(file_path) self.assertEqual(3, stat_result.st_size) + @unittest.skipIf(sys.version_info >= (3, 12), "Currently not working in 3.12") def test_import_open_as_other_name(self): file_path = "/foo/bar" self.fs.create_file(file_path, contents=b"abc") contents = pyfakefs.tests.import_as_example.file_contents1(file_path) self.assertEqual("abc", contents) + @unittest.skipIf(sys.version_info >= (3, 12), "Currently not working in 3.12") def test_import_io_open_as_other_name(self): file_path = "/foo/bar" self.fs.create_file(file_path, contents=b"abc") @@ -393,6 +398,10 @@ class AdditionalSkipNamesTest(fake_filesystem_unittest.TestCase): self.fs.create_file("foo") self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists7("foo")) + @unittest.skipIf( + sys.version_info >= (3, 12), + "Skip modules currently not working for open in 3.12", + ) def test_open_succeeds(self): pyfakefs.tests.import_as_example.open_this_file() @@ -438,6 +447,10 @@ class AdditionalSkipNamesModuleTest(fake_filesystem_unittest.TestCase): self.fs.create_file("foo") self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists7("foo")) + @unittest.skipIf( + sys.version_info >= (3, 12), + "Skip modules currently not working for open in 3.12", + ) def test_open_succeeds(self): pyfakefs.tests.import_as_example.open_this_file() @@ -704,31 +717,33 @@ class TestShutilWithZipfile(fake_filesystem_unittest.TestCase): shutil.make_archive("archive", "zip", root_dir="foo") -class TestDistutilsCopyTree(fake_filesystem_unittest.TestCase): - """Regression test for #501.""" +if sys.version_info < (3, 12): - def setUp(self): - self.setUpPyfakefs() - self.fs.create_dir("./test/subdir/") - self.fs.create_dir("./test/subdir2/") - self.fs.create_file("./test2/subdir/1.txt") + class TestDistutilsCopyTree(fake_filesystem_unittest.TestCase): + """Regression test for #501.""" + + def setUp(self): + self.setUpPyfakefs() + self.fs.create_dir("./test/subdir/") + self.fs.create_dir("./test/subdir2/") + self.fs.create_file("./test2/subdir/1.txt") - def test_file_copied(self): - copy_tree("./test2/", "./test/") - remove_tree("./test2/") + def test_file_copied(self): + copy_tree("./test2/", "./test/") + remove_tree("./test2/") - self.assertTrue(os.path.isfile("./test/subdir/1.txt")) - self.assertFalse(os.path.isdir("./test2/")) + self.assertTrue(os.path.isfile("./test/subdir/1.txt")) + self.assertFalse(os.path.isdir("./test2/")) - def test_file_copied_again(self): - # used to fail because 'test2' could not be found - self.assertTrue(os.path.isfile("./test2/subdir/1.txt")) + def test_file_copied_again(self): + # used to fail because 'test2' could not be found + self.assertTrue(os.path.isfile("./test2/subdir/1.txt")) - copy_tree("./test2/", "./test/") - remove_tree("./test2/") + copy_tree("./test2/", "./test/") + remove_tree("./test2/") - self.assertTrue(os.path.isfile("./test/subdir/1.txt")) - self.assertFalse(os.path.isdir("./test2/")) + self.assertTrue(os.path.isfile("./test/subdir/1.txt")) + self.assertFalse(os.path.isdir("./test2/")) class PathlibTest(TestCase): |