Skip to content

Commit

Permalink
Make sure pytest will work without pyfakefs installed
Browse files Browse the repository at this point in the history
- import fs in conftest.py used in tests
- remove tests for deprecated function
- see #550
  • Loading branch information
mrbean-bremen committed Aug 29, 2020
1 parent b390245 commit 5a2d8f0
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .travis/run_pytest_plugin_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ if [[ $TRAVIS_OS_NAME == 'osx' ]]; then
source ~/.venv/bin/activate
fi

python -m pytest pyfakefs/pytest_tests/pytest_plugin_failing_test.py > ./testresult.txt
python -m pytest pyfakefs/pytest_tests/pytest_plugin_failing_helper.py > ./testresult.txt
python -m pytest pyfakefs/pytest_tests/pytest_check_failed_plugin_test.py && \
python -m pytest pyfakefs/pytest_tests/pytest_plugin_test.py
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The released versions correspond to PyPi releases.

#### Infrastructure
* fixed another problem with CI test scripts not always propagating errors
* make sure pytest will work without pyfakefs installed
(see [#550](../../issues/550))

## [Version 4.1.0](https://pypi.python.org/pypi/pyfakefs/4.1.0)

Expand Down
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ environment:
install:
- "%PYTHON%\\python.exe -m pip install -r requirements.txt"
- "%PYTHON%\\python.exe -m pip install -r extra_requirements.txt"
- "%PYTHON%\\python.exe -m pip install ."

build: off

Expand All @@ -20,5 +19,5 @@ test_script:
- "%PYTHON%\\python.exe -m pytest pyfakefs\\pytest_tests\\pytest_plugin_test.py"
- ps: If ($env:PYTHON -Match ".*3[678]-x64") { "$env:PYTHON\\python.exe -m pytest pyfakefs\\pytest_tests\\pytest_fixture_test.py" }
- ps: If ($env:PYTHON -Match ".*3[678]-x64") { "$env:PYTHON\\python.exe -m pytest pyfakefs\\pytest_tests\\pytest_fixture_param_test.py" }
- "%PYTHON%\\python.exe -m pytest pyfakefs\\pytest_tests\\pytest_plugin_failing_test.py > testresult.txt | echo."
- "%PYTHON%\\python.exe -m pytest pyfakefs\\pytest_tests\\pytest_plugin_failing_helper.py > testresult.txt | echo."
- "%PYTHON%\\python.exe -m pytest pyfakefs\\pytest_tests\\pytest_check_failed_plugin_test.py"
2 changes: 2 additions & 0 deletions pyfakefs/pytest_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import pytest

from pyfakefs.fake_filesystem_unittest import Patcher
# import the fs fixture to be visible if pyfakefs is not installed
from pyfakefs.pytest_plugin import fs # noqa: F401

Patcher.SKIPMODULES.add(pytest)
Patcher.SKIPMODULES.add(py)
Expand Down
7 changes: 6 additions & 1 deletion pyfakefs/pytest_tests/pytest_check_failed_plugin_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Tests that a failed pytest properly displays the call stack.
Uses the output from running pytest with pytest_plugin_failing_test.py.
Uses the output from running pytest with pytest_plugin_failing_helper.py.
Regression test for #381.
"""
import os

import pytest


@pytest.mark.skipif(not os.path.exists('testresult.txt'),
reason='Only run in CI tests')
def test_failed_testresult_stacktrace():
with open('testresult.txt') as f:
contents = f.read()
Expand Down
2 changes: 1 addition & 1 deletion pyfakefs/tests/fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2192,7 +2192,7 @@ def test_side_effect_file_object(self):
self.side_effect_called = False
with fake_open('/a/b/file_one', 'w') as handle:
handle.write('foo')
self.assertEquals(self.side_effect_file_object_content, 'foo')
self.assertEqual(self.side_effect_file_object_content, 'foo')


if __name__ == '__main__':
Expand Down
98 changes: 0 additions & 98 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,104 +471,6 @@ def test_pause_resume_contextmanager(self):
self.assertTrue(os.path.exists(fake_temp_file.name))


class TestCopyOrAddRealFile(TestPyfakefsUnittestBase):
"""Tests the `fake_filesystem_unittest.TestCase.copyRealFile()` method.
Note that `copyRealFile()` is deprecated in favor of
`FakeFilesystem.add_real_file()`.
"""
filepath = None

@classmethod
def setUpClass(cls):
filename = __file__
if filename.endswith('.pyc'): # happens on windows / py27
filename = filename[:-1]
cls.filepath = os.path.abspath(filename)
with open(cls.filepath) as f:
cls.real_string_contents = f.read()
with open(cls.filepath, 'rb') as f:
cls.real_byte_contents = f.read()
cls.real_stat = os.stat(cls.filepath)

@unittest.skipIf(sys.platform == 'darwin', 'Different copy behavior')
def test_copy_real_file(self):
"""Typical usage of deprecated copyRealFile()"""
# Use this file as the file to be copied to the fake file system
fake_file = self.copyRealFile(self.filepath)

self.assertTrue(
'class TestCopyOrAddRealFile(TestPyfakefsUnittestBase)'
in self.real_string_contents,
'Verify real file string contents')
self.assertTrue(
b'class TestCopyOrAddRealFile(TestPyfakefsUnittestBase)'
in self.real_byte_contents,
'Verify real file byte contents')

# note that real_string_contents may differ to fake_file.contents
# due to newline conversions in open()
self.assertEqual(fake_file.byte_contents, self.real_byte_contents)

self.assertEqual(oct(fake_file.st_mode), oct(self.real_stat.st_mode))
self.assertEqual(fake_file.st_size, self.real_stat.st_size)
self.assertAlmostEqual(fake_file.st_ctime,
self.real_stat.st_ctime, places=5)
self.assertAlmostEqual(fake_file.st_atime,
self.real_stat.st_atime, places=5)
self.assertLess(fake_file.st_atime, self.real_stat.st_atime + 10)
self.assertAlmostEqual(fake_file.st_mtime,
self.real_stat.st_mtime, places=5)
self.assertEqual(fake_file.st_uid, self.real_stat.st_uid)
self.assertEqual(fake_file.st_gid, self.real_stat.st_gid)

def test_copy_real_file_deprecated_arguments(self):
"""Deprecated copyRealFile() arguments"""
self.assertFalse(self.fs.exists(self.filepath))
# Specify redundant fake file path
self.copyRealFile(self.filepath, self.filepath)
self.assertTrue(self.fs.exists(self.filepath))

# Test deprecated argument values
with self.assertRaises(ValueError):
self.copyRealFile(self.filepath, '/different/filename')
with self.assertRaises(ValueError):
self.copyRealFile(self.filepath, create_missing_dirs=False)

def test_add_real_file(self):
"""Add a real file to the fake file system to be read on demand"""

# this tests only the basic functionality inside a unit test, more
# thorough tests are done in
# fake_filesystem_test.RealFileSystemAccessTest
fake_file = self.fs.add_real_file(self.filepath)
self.assertTrue(self.fs.exists(self.filepath))
self.assertIsNone(fake_file._byte_contents)
self.assertEqual(self.real_byte_contents, fake_file.byte_contents)

def test_add_real_directory(self):
"""Add a real directory and the contained files to the fake file system
to be read on demand"""

# This tests only the basic functionality inside a unit test,
# more thorough tests are done in
# fake_filesystem_test.RealFileSystemAccessTest.
# Note: this test fails (add_real_directory raises) if 'genericpath'
# is not added to SKIPNAMES
real_dir_path = os.path.split(os.path.dirname(self.filepath))[0]
self.fs.add_real_directory(real_dir_path)
self.assertTrue(self.fs.exists(real_dir_path))
self.assertTrue(self.fs.exists(
os.path.join(real_dir_path, 'fake_filesystem.py')))

def test_add_real_directory_with_backslash(self):
"""Add a real directory ending with a path separator."""
real_dir_path = os.path.split(os.path.dirname(self.filepath))[0]
self.fs.add_real_directory(real_dir_path + os.sep)
self.assertTrue(self.fs.exists(real_dir_path))
self.assertTrue(self.fs.exists(
os.path.join(real_dir_path, 'fake_filesystem.py')))


class TestPyfakefsTestCase(unittest.TestCase):
def setUp(self):
class TestTestCase(fake_filesystem_unittest.TestCase):
Expand Down

0 comments on commit 5a2d8f0

Please sign in to comment.