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

add_real_directory does not work after chdir #355

Closed
abbec opened this issue Mar 12, 2018 · 12 comments
Closed

add_real_directory does not work after chdir #355

abbec opened this issue Mar 12, 2018 · 12 comments
Labels

Comments

@abbec
Copy link

abbec commented Mar 12, 2018

If I do an os.chdir() call before calling add_real_directory with an absolute path, os.listdir() on that directory returns nothing since target path is set to the combination of the current directory and the specified path to add_real_directory.

Example

os.chdir("/Users/abbe/test")
fs.add_real_directory("/Users/abbe/real")
os.listdir("/Users/abbe/real")

the call to os.listdir() returns nothing in this case since the file gets added as /Users/abbe/test/Users/abbe/real instead of /Users/abbe/real.

@mrbean-bremen
Copy link
Member

Thanks for the report - I will have a look!

@mrbean-bremen
Copy link
Member

So, to understand this: this is inside a unit test, or after setting up Patcher? E.g. os here is the faked version?
In this case, you already have a directory "/Users/abbe/test" in your fake file system - did you add that also using add_real_directory()?

@mrbean-bremen
Copy link
Member

I'm trying to reproduce this, but until now it works correctly for me - so I'm obviously doing something different. I need a bit more context to be able to reproduce this...

@abbec
Copy link
Author

abbec commented Mar 13, 2018

Yes, this is inside a unit test/after patcher is set up (os is the faked version) and /Users/abbe/test was added to the fake filesystem with create_dir() prior to the above lines.

@abbec
Copy link
Author

abbec commented Mar 13, 2018

This code

import os

def test_something(fs):
    fs.create_dir("/Users/acervin/somethingfake")
    os.chdir("/Users/acervin/somethingfake")
    fs.add_real_directory("/Users/acervin/test")

    print(os.listdir("/Users/acervin/test"))
    print(os.listdir("/Users/acervin/somethingfake/Users/acervin/test"))

    assert os.listdir("/Users/acervin/test")

gives this output

screen shot 2018-03-13 at 09 34 57

and the folder /Users/acervin/test contains one file called test.txt.

@abbec
Copy link
Author

abbec commented Mar 13, 2018

And if I remove the call to os.chdir in the above code (effectively leaving cwd at /) it works.

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Mar 13, 2018

Ok, thanks! I will have another look in the evening.

@mrbean-bremen
Copy link
Member

Unfortunately I couldn't reproduce this under Windows, so I think it is Unix-specific. My Ubuntu got trashed with the latest Windows 10 reinstall, and I couldn't get the grub menu to show up even after an Ubuntu reinstall, grub-update and the like, so I'm giving up for today. Got to love Windows 10...
I may have another go at the weekend.

@abbec
Copy link
Author

abbec commented Mar 14, 2018

Yeah, it is specific to *NIX and I think it caused by the code here: https://github.com/jmcgeheeiv/pyfakefs/blob/master/pyfakefs/fake_filesystem.py#L394. When it is normalizing it ignores the leading slash (that denotes the root) since it says names[1:]. The function absnormpath will then hit this code:

elif not self._starts_with_root_path(path):
    # Prefix relative paths with cwd, if cwd is not root.
    ...

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Mar 14, 2018
@mrbean-bremen
Copy link
Member

Thanks - that helped! So the real bug is in FakeDirectory.path, I just added a fix for this. Please check if this fixes your problem.

@mrbean-bremen
Copy link
Member

I could reproduce the problem under Linux (got it running again) before the fix, it is gone now - closing.

@abbec
Copy link
Author

abbec commented Mar 21, 2018

Awesome thanks!

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