Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pathlib.Path is not patched in unittest test fixuture #440

Closed
ghost opened this issue Oct 10, 2018 · 5 comments
Closed

pathlib.Path is not patched in unittest test fixuture #440

ghost opened this issue Oct 10, 2018 · 5 comments

Comments

@ghost
Copy link

ghost commented Oct 10, 2018

Similar to issue #420 however this has been observed with unittest rather than pytest. I did search for workarounds including module patching and reloading however I was not successful in finding a working solution.

pyfakefs==3.4.3
python==3.7.0

On Ubuntu 16.04

Test cases below: the test_Path_exists method demonstrates a test case that should pass but does not pass. The test would pass if pathlib.Path had been successful at interacting with the fake filesystem.

import unittest
from pathlib import Path
from pyfakefs.fake_filesystem_unittest import TestCase
import os

class TestFileExists(TestCase):

    def setUp(self):
        self.setUpPyfakefs()
    
    def test_os_path_exists(self):
        self.assertFalse(os.path.exists('/test.txt'))
        self.fs.create_file('/test.txt', contents='abc')
        self.assertTrue(os.path.exists('/test.txt'))

    def test_Path_exists(self):
        self.assertFalse(Path('/test.txt').exists())
        self.fs.create_file('/test.txt', contents='abc')
        self.assertTrue(Path('/test.txt').exists())


if __name__ == '__main__':
    unittest.main()

Test output:

F.
======================================================================
FAIL: test_Path_exists (__main__.TestFileExists)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "main.py", line 26, in test_Path_exists
    self.assertTrue(Path('/test.txt').exists())
AssertionError: False is not true

----------------------------------------------------------------------
Ran 2 tests in 0.006s

FAILED (failures=1)

Anecdotally, I have experienced the same issue on windows and with other methods from pathlib.Path including pathlib.Path.glob.

@mrbean-bremen
Copy link
Member

Thanks for the report! I'm traveling right now, but will have a look at this somewhere next week.

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Oct 15, 2018

Ok, first: this is a known shortcoming of pyfakefs described in the documentation - it will not work out of the box, as you probably know.
If I use the solution given as an example in the documentation under "modules_to_patch", in works for me. There is also a test that tests this in Travis, so I'm not sure what went wrong with your solution. Can you show the code you used to patch this?

That being said, I think it makes sense to add patching this specific import (e.g. from pathlib import Path) to regular pyfakefs, so that there is no need for additional customizing. I already had this on my to-do list, so this issue is a good incentive to actually do it... I would be interested to understand why the patching did not work for you anyway.

@mrbean-bremen
Copy link
Member

@Shredd - it shall work now out of the box in master. Please check if it works for you!

@ghost
Copy link
Author

ghost commented Oct 17, 2018

@mrbean-bremen Pulled master, installed pyfakefs 3.5 locally, and confirmed that this example now passes (ubuntu 16.04, python 3.7.0). Glad to help!

For retrospect: Yes the sample that I posted is a known issue and I did see the requirement to use the particular import statement as a workaround (I was not able to use this workaround import statement successfully in testing). Initially I tried setting modules_to_reload and/or modules_to_patch (in varying combinations) as keyword arguments in the call to setUpPyFakefs() however I did not attempt to use the Patcher class for context management. I do not have access to the original code that prompted this (not checked in).

@mrbean-bremen
Copy link
Member

Ok, thanks - I'm closing the issue in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant