Skip to content

Commit

Permalink
Fix tests if HOME environment is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed Aug 23, 2023
1 parent 2253714 commit 11d5d78
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 16 additions & 10 deletions pyfakefs/tests/fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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 (
Expand Down Expand Up @@ -1061,17 +1062,22 @@ def test_join_bytes(self):
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,
Expand Down
1 change: 1 addition & 0 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 25 additions & 16 deletions pyfakefs/tests/fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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
Expand Down Expand Up @@ -461,23 +462,31 @@ def test_cwd(self):
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):
Expand Down

0 comments on commit 11d5d78

Please sign in to comment.