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

Write to file created with add_real_file does not truncate #470

Closed
ian-h-chamberlain opened this issue Feb 19, 2019 · 3 comments
Closed
Labels

Comments

@ian-h-chamberlain
Copy link

Describe the bug
Adding a file with add_real_file(file, read_only=False), then writing to the file, does not properly truncate the fake in-memory file – instead it appears to continue reading from the real file on disk.
I'm not sure if this is related to #277, but I can reproduce with much simpler test code.

How To Reproduce
Using pytest:

def test_pyfakefs_real_file(fs):
    fs.add_real_file('foo.txt', read_only=False)

    with open('foo.txt', 'w') as foo:
        foo.write('Goodbye')

    with open('foo.txt', 'r') as foo:
        assert foo.read() == 'Goodbye'

foo.txt contents:

Hello,  world!

Output:

$ pytest test.py
============================================================= test session starts ==============================================================
platform darwin -- Python 3.6.5, pytest-4.2.1, py-1.7.0, pluggy-0.8.1
rootdir: /Users/ichamberlain/Documents/workspace, inifile:
plugins: pyfakefs-3.5.7
collected 1 item

test.py F                                                                                                                                [100%]

=================================================================== FAILURES ===================================================================
___________________________________________________________ test_pyfakefs_real_file ____________________________________________________________

fs = <pyfakefs.fake_filesystem.FakeFilesystem object at 0x1053f78d0>

    def test_pyfakefs_real_file(fs):
        fs.add_real_file('foo.txt', read_only=False)

        with open('foo.txt', 'w') as foo:
            foo.write('Goodbye')

        with open('foo.txt', 'r') as foo:
>           assert foo.read() == 'Goodbye'
E           AssertionError: assert 'Goodbye world!\n' == 'Goodbye'
E             - Goodbye world!
E             + Goodbye

/Users/ichamberlain/Documents/workspace/test.py:8: AssertionError
=========================================================== 1 failed in 0.37 seconds ===========================================================

Your enviroment
Using pyenv with pyfakefs and pytest installed in the "system" python provided by pyenv.

Darwin-18.2.0-x86_64-i386-64bit
Python 3.6.5 (default, Jan 29 2019, 13:07:11)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)]
pyfakefs 3.5.7
@mrbean-bremen
Copy link
Member

Thanks - I will check this. And this is not related to #277 - that happens only under Python 2, and only on MacOS, while I could reproduce this one also under Windows.

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Feb 19, 2019
@mrbean-bremen
Copy link
Member

Good catch - this has to do with lazily reading the file contents, which has been done even after writing to the file.

@ian-h-chamberlain
Copy link
Author

Wow, thanks for the quick fix! For reference in case anyone else needs a temporary workaround, what I did in the meantime is pretty simple: you can just remove the file, then use create_file() to replace the contents and everything works as expected.

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

No branches or pull requests

2 participants