From 5a2d8f0a86540151e1ffb6d371c7cf8637f7018b Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Sat, 29 Aug 2020 21:11:33 +0200 Subject: [PATCH] Make sure pytest will work without pyfakefs installed - import fs in conftest.py used in tests - remove tests for deprecated function - see #550 --- .travis/run_pytest_plugin_test.sh | 2 +- CHANGES.md | 2 + appveyor.yml | 3 +- pyfakefs/pytest_tests/conftest.py | 2 + .../pytest_check_failed_plugin_test.py | 7 +- ...est.py => pytest_plugin_failing_helper.py} | 0 pyfakefs/tests/fake_filesystem_test.py | 2 +- .../tests/fake_filesystem_unittest_test.py | 98 ------------------- 8 files changed, 13 insertions(+), 103 deletions(-) rename pyfakefs/pytest_tests/{pytest_plugin_failing_test.py => pytest_plugin_failing_helper.py} (100%) diff --git a/.travis/run_pytest_plugin_test.sh b/.travis/run_pytest_plugin_test.sh index 0d49ec61..a0dc7cb2 100755 --- a/.travis/run_pytest_plugin_test.sh +++ b/.travis/run_pytest_plugin_test.sh @@ -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 diff --git a/CHANGES.md b/CHANGES.md index 23f3adbc..67505e74 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/appveyor.yml b/appveyor.yml index da82a820..4e5524c2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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 @@ -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" diff --git a/pyfakefs/pytest_tests/conftest.py b/pyfakefs/pytest_tests/conftest.py index 92b49ff8..5a573aa2 100644 --- a/pyfakefs/pytest_tests/conftest.py +++ b/pyfakefs/pytest_tests/conftest.py @@ -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) diff --git a/pyfakefs/pytest_tests/pytest_check_failed_plugin_test.py b/pyfakefs/pytest_tests/pytest_check_failed_plugin_test.py index aebf948c..42ff87ea 100644 --- a/pyfakefs/pytest_tests/pytest_check_failed_plugin_test.py +++ b/pyfakefs/pytest_tests/pytest_check_failed_plugin_test.py @@ -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() diff --git a/pyfakefs/pytest_tests/pytest_plugin_failing_test.py b/pyfakefs/pytest_tests/pytest_plugin_failing_helper.py similarity index 100% rename from pyfakefs/pytest_tests/pytest_plugin_failing_test.py rename to pyfakefs/pytest_tests/pytest_plugin_failing_helper.py diff --git a/pyfakefs/tests/fake_filesystem_test.py b/pyfakefs/tests/fake_filesystem_test.py index ffda097f..5e4f3d3c 100644 --- a/pyfakefs/tests/fake_filesystem_test.py +++ b/pyfakefs/tests/fake_filesystem_test.py @@ -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__': diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py index 5c599273..3cdf5d8c 100644 --- a/pyfakefs/tests/fake_filesystem_unittest_test.py +++ b/pyfakefs/tests/fake_filesystem_unittest_test.py @@ -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):