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

os.path.isfile incorrect result for file without read permissions. #588

Closed
jacobbogdanov opened this issue Feb 24, 2021 · 4 comments
Closed
Labels

Comments

@jacobbogdanov
Copy link

Describe the bug
under pyfakefs os.path.isfile (and pathlib.Path.is_file) return False for a file without read permissions set, while the original implementation returns True.

How To Reproduce

import os
from typing import Iterator

import pytest
from pyfakefs import fake_filesystem


class TestRealFs:
    @pytest.fixture
    def unreadable_file(self, tmp_path) -> Iterator[str]:
        path = tmp_path / "foo"
        try:
            path.touch()
            path.chmod(0o000)
            yield str(path)
        finally:
            path.chmod(0o644)

    def test_exists(self, unreadable_file):
        assert os.path.exists(unreadable_file)

    def test_isfile(self, unreadable_file):
        assert os.path.isfile(unreadable_file)

    def test_not_exists(self):
        assert not os.path.exists("/does/not/exist")

    def test_not_isfile(self):
        assert not os.path.isfile("/does/not/exist")


class TestPyfakeFs:
    @pytest.fixture
    def unreadable_file(self, fs: fake_filesystem.FakeFilesystem) -> str:
        path = "/tmp/foo"
        fs.create_file(path, st_mode=0o000)
        return path

    def test_exists(self, unreadable_file):
        assert os.path.exists(unreadable_file)

    def test_isfile(self, unreadable_file):
        assert os.path.isfile(unreadable_file)  # this fails (and differs from TestRealFs::test_isfile)

    def test_not_exists(self, fs):
        assert not os.path.exists("/does/not/exist")

    def test_not_isfile(self, fs):
        assert not os.path.isfile("/does/not/exist")
$ py.test pyfakefs_test.py --randomly-dont-reorganize -vvv
====================================================== test session starts ======================================================
platform darwin -- Python 3.6.12, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 -- /bin/python3.6
cachedir: .pytest_cache
Using --randomly-seed=1614180334
rootdir: /Users/jbogdanov/code/p2
plugins: timeout-1.3.4, mongodb-2.1.2, mock-3.1.0, randomly-3.1.0, requests-mock-1.7.0, stubprocess-0.1.0, pyfakefs-3.5.8, aiohttp-0.3.0, asyncio-0.12.0, lazy-fixture-0.6.3, cov-2.8.1
collected 8 items                                                                                                               

pyfakefs_test.py::TestRealFs::test_exists PASSED                                                                          [ 12%]
pyfakefs_test.py::TestRealFs::test_isfile PASSED                                                                          [ 25%]
pyfakefs_test.py::TestRealFs::test_not_exists PASSED                                                                      [ 37%]
pyfakefs_test.py::TestRealFs::test_not_isfile PASSED                                                                      [ 50%]
pyfakefs_test.py::TestPyfakeFs::test_exists PASSED                                                                        [ 62%]
pyfakefs_test.py::TestPyfakeFs::test_isfile FAILED                                                                        [ 75%]
pyfakefs_test.py::TestPyfakeFs::test_not_exists PASSED                                                                    [ 87%]
pyfakefs_test.py::TestPyfakeFs::test_not_isfile PASSED                                                                    [100%]

=========================================================== FAILURES ============================================================
___________________________________________________ TestPyfakeFs.test_isfile ____________________________________________________

self = <pyfakefs_test.TestPyfakeFs object at 0x10c4a8b38>, unreadable_file = '/tmp/foo'

    def test_isfile(self, unreadable_file):
>       assert os.path.isfile(unreadable_file)
E       AssertionError: assert False
E        +  where False = <bound method FakePathModule.isfile of <pyfakefs.fake_filesystem.FakePathModule object at 0x10c330c50>>('/tmp/foo')
E        +    where <bound method FakePathModule.isfile of <pyfakefs.fake_filesystem.FakePathModule object at 0x10c330c50>> = <pyfakefs.fake_filesystem.FakePathModule object at 0x10c330c50>.isfile
E        +      where <pyfakefs.fake_filesystem.FakePathModule object at 0x10c330c50> = os.path

pyfakefs_test.py:58: AssertionError
==================================================== short test summary info ====================================================
FAILED pyfakefs_test.py::TestPyfakeFs::test_isfile - AssertionError: assert False
================================================== 1 failed, 7 passed in 1.27s ==================================================

Your environment

Darwin-19.6.0-x86_64-i386-64bit
Python 3.6.12 (default, Dec  2 2020, 12:31:12) 
[GCC Apple LLVM 12.0.0 (clang-1200.0.32.27)]
pyfakefs 4.3.3

and

Linux-3.10.0-862.2.3.el7.x86_64-x86_64-with-centos-7.5.1804-Core
Python 3.6.6 (default, Aug 13 2018, 18:24:23) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
pyfakefs 3.3
@mrbean-bremen
Copy link
Member

Thanks - I will have a look!

github-actions bot pushed a commit that referenced this issue Feb 24, 2021
…in FakeFile st_mode arg - add convenience permission argument to create_file() - fixes #588
@mrbean-bremen
Copy link
Member

Should be fixed now in master - please check!
Do you need a new release?

@jacobbogdanov
Copy link
Author

Thanks for the speedy fix!

A new release would be awesome

@mrbean-bremen
Copy link
Member

Ok, done.

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