Project

General

Profile

Bug #9716

FileTest.exist? does not convert relative path to absolute path before testing existence

Added by 101b147ch (Mario Maia) over 5 years ago. Updated over 5 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
2.1.1p76 (2014-02-24 revision 45161) [i686-linux]
[ruby-core:61911]

Description

FileTest.exist?(Pathname.new('~/derp').expand_path) => true
FileTest.exist?(Pathname.new('~/derp')) => false

History

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Open to Rejected

Methods of FileTest don't expand the arguments implicitly.

Updated by 101b147ch (Mario Maia) over 5 years ago

Nobuyoshi Nakada wrote:

Methods of FileTest don't expand the arguments implicitly.

Shouldn't they do that? I mean, I can understand FileTest not doing it, but Pathname#exist? has the same behavior, and I think this is kinda silly. Can you at least explain to me this decision?

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

If they do it, you have no way to test files start with '~'.

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

Or always File.exist?(path.sub(/\A~/, './~')) and so on.
Do you prefer this?

Updated by 101b147ch (Mario Maia) over 5 years ago

Nobuyoshi Nakada wrote:

If they do it, you have no way to test files start with '~'.

Good point, I hadn't thought about this borderline case. And from this case, I take that this is not a bug on FileTest, but on #expand_path.

I have played with a bit and it operates in an awkward way. It can expand ~/~derp_folder~/~derp, but it cant expand ~derp.

Or always File.exist?(path.sub(/\A~/, './~')) and so on.

It is not about what I prefer, but about what works. In my humble opinion, I think this #sub (or some other method) will indeed have to be called, but not on FileTest. It should be called on the first lines of #expand_path. And this pattern will have to be refined a bit more. I tried it on some cases and it works for some, but for others it produces a path totally different. I think we will have to use #sub passing a block to it to have more control over the pattern substitution.

What do you think? And, by the way, should we continue this conversation here or should I open a new bug reporting #expand_path?

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

Mario Maia wrote:

What do you think? And, by the way, should we continue this conversation here or should I open a new bug reporting #expand_path?

Use File.absolute_path, instead of File.expand_path.

Also available in: Atom PDF