diff options
author | mrbean-bremen <hansemrbean@googlemail.com> | 2023-08-23 20:11:08 +0200 |
---|---|---|
committer | mrbean-bremen <mrbean-bremen@users.noreply.github.com> | 2023-08-24 20:02:18 +0200 |
commit | 4ea3c5feaa9d440022c128219bad959e9ea6d1eb (patch) | |
tree | 03b0e2fdfaed9024f19af0ff8357896593c541e7 | |
parent | fddbd9165d64e425ac594d73e2d37c56de830c6a (diff) | |
download | pyfakefs-4ea3c5feaa9d440022c128219bad959e9ea6d1eb.tar.gz |
Fix tests if HOME environment is missing
- fixes #870
-rw-r--r-- | CHANGES.md | 1 | ||||
-rw-r--r-- | pyfakefs/tests/fake_filesystem_test.py | 26 | ||||
-rw-r--r-- | pyfakefs/tests/fake_filesystem_unittest_test.py | 1 | ||||
-rw-r--r-- | pyfakefs/tests/fake_pathlib_test.py | 41 |
4 files changed, 43 insertions, 26 deletions
@@ -5,6 +5,7 @@ The released versions correspond to PyPI releases. ### Fixes * removed a leftover debug print statement (see [#869](../../issues/869)) +* make sure tests work without HOME environment set (see [#870](../../issues/870)) ## [Version 5.2.3](https://pypi.python.org/pypi/pyfakefs/5.2.3) (2023-08-18) Fixes a rare problem on pytest shutdown. diff --git a/pyfakefs/tests/fake_filesystem_test.py b/pyfakefs/tests/fake_filesystem_test.py index dda4638..696f8a8 100644 --- a/pyfakefs/tests/fake_filesystem_test.py +++ b/pyfakefs/tests/fake_filesystem_test.py @@ -20,6 +20,7 @@ import os import stat import sys import unittest +from unittest.mock import patch from pyfakefs import fake_filesystem, fake_os, fake_open from pyfakefs.fake_filesystem import ( @@ -1061,17 +1062,22 @@ class FakePathModuleTest(TestCase): components = [b"foo", b"bar", b"baz"] self.assertEqual(b"foo!bar!baz", self.path.join(*components)) + @unittest.skipIf(sys.platform != "win32", "Windows specific test") + @patch.dict(os.environ, {"USERPROFILE": r"C:\Users\John"}) + def test_expand_user_windows(self): + self.assertEqual(self.path.expanduser("~"), "C:!Users!John") + + @unittest.skipIf(sys.platform == "win32", "Posix specific test") + @patch.dict(os.environ, {"HOME": "/home/john"}) def test_expand_user(self): - if self.is_windows: - self.assertEqual( - self.path.expanduser("~"), - self.os.environ["USERPROFILE"].replace("\\", "!"), - ) - else: - self.assertEqual( - self.path.expanduser("~"), - self.os.environ["HOME"].replace("/", "!"), - ) + self.assertEqual(self.path.expanduser("~"), "!home!john") + + @patch.dict(os.environ, {}, clear=True) + def test_expand_user_no_home_environment(self): + """Make sure this also works without HOME / USERPROFILE set""" + # we just check that it does not crash and has some result, + # as the result is system-dependent + self.assertTrue(self.path.expanduser("~")) @unittest.skipIf( TestCase.is_windows or TestCase.is_cygwin, diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py index 6d3e6e6..86f1ae6 100644 --- a/pyfakefs/tests/fake_filesystem_unittest_test.py +++ b/pyfakefs/tests/fake_filesystem_unittest_test.py @@ -815,6 +815,7 @@ class TestOtherFS(fake_filesystem_unittest.TestCase): def setUp(self): self.setUpPyfakefs() + @mock.patch.dict(os.environ, {"HOME": "/home/john"}) def test_real_file_with_home(self): """Regression test for #558""" self.fs.is_windows_fs = os.name != "nt" diff --git a/pyfakefs/tests/fake_pathlib_test.py b/pyfakefs/tests/fake_pathlib_test.py index d487f32..12820ba 100644 --- a/pyfakefs/tests/fake_pathlib_test.py +++ b/pyfakefs/tests/fake_pathlib_test.py @@ -27,6 +27,7 @@ import sys import unittest from collections import namedtuple from unittest import mock +from unittest.mock import patch from pyfakefs import fake_pathlib, fake_filesystem, fake_filesystem_unittest, fake_os from pyfakefs.fake_filesystem import OSType @@ -461,23 +462,31 @@ class FakePathlibFileObjectPropertyTest(RealPathlibTestCase): self.path.cwd(), self.path(self.os.path.realpath(dir_path)) ) - def test_expanduser(self): - if is_windows: - self.assertEqual( - self.path("~").expanduser(), - self.path(os.environ["USERPROFILE"].replace("\\", "/")), - ) - else: - self.assertEqual(self.path("~").expanduser(), self.path(os.environ["HOME"])) + @unittest.skipIf(sys.platform != "win32", "Windows specific test") + @patch.dict(os.environ, {"USERPROFILE": r"C:\Users\John"}) + def test_expanduser_windows(self): + self.assertEqual( + self.path("~").expanduser(), + self.path("C:/Users/John"), + ) - def test_home(self): - if is_windows: - self.assertEqual( - self.path(os.environ["USERPROFILE"].replace("\\", "/")), - self.path.home(), - ) - else: - self.assertEqual(self.path(os.environ["HOME"]), self.path.home()) + @unittest.skipIf(sys.platform == "win32", "Posix specific test") + @patch.dict(os.environ, {"HOME": "/home/john"}) + def test_expanduser_posix(self): + self.assertEqual(self.path("~").expanduser(), self.path("/home/john")) + + @unittest.skipIf(sys.platform != "win32", "Windows specific test") + @patch.dict(os.environ, {"USERPROFILE": r"C:\Users\John"}) + def test_home_windows(self): + self.assertEqual( + self.path(self.path("C:/Users/John")), + self.path.home(), + ) + + @unittest.skipIf(sys.platform == "win32", "Posix specific test") + @patch.dict(os.environ, {"HOME": "/home/john"}) + def test_home_posix(self): + self.assertEqual(self.path("/home/john"), self.path.home()) class RealPathlibFileObjectPropertyTest(FakePathlibFileObjectPropertyTest): |