fakefs / fakefs

A fake filesystem. Use it in your tests.
MIT License
1.04k stars 188 forks source link

Pathname's #realdirpath and #realpath do not work (as intended) #489

Open sergio-bobillier opened 1 year ago

sergio-bobillier commented 1 year ago

According to Ruby's documentation for Pathname#realdirpath and Pathname#realpath they are supposed to return the real, absolute path to the actual entity in the File System. I'm currently using them precisely with this purpose:

def validate_path(path)
    path = Pathname.new(path).realpath
    workspace = Pathname.new(WORKSPACE).realpath

    # The following statement goes up one level at a time starting from the
    # given path until it finds a path that matches the Workspace. If it
    # never does then the result is set to false.
    path.ascend { |parent| break true if parent == workspace } || false
end

But when I use FakeFS these no longer work. They seem to be trying to return the the actual real path (on the real File System) and since the directories do not exist there they fail:

For example:

pry> path
=> #<FakeFS::Pathname:/Workspace/other/project/spec/routing>
pry> path.exist?
=> true
pry> path.realdirpath
Errno::ENOENT: No such file or directory @ realpath_rec - /Workspace
pry> path.realpath
Errno::ENOENT: No such file or directory @ rb_check_realpath_internal - /Workspace/other/project/spec/routing

This is what I'm doing:

require 'fakefs/safe'

RSpec.describe PathValidator do
  subject(:path_validator) { described_class.new }

  let(:workspace) { '/Workspace/project' }
  let(:path) { '/Workspace/other/project/spec/routing' }

  before do
    FakeFS.activate!
    FileUtils.mkdir_p(workspace)
    FileUtils.mkdir_p(path)
  end

  after { FakeFS.deactivate! }

  it 'returns false' do
    expect(path_validator.validate_path(path)).to eq(false)
  end
end

I was expecting false to be returned but instead:

  1) PathValidator returns false
     Failure/Error: expect(path_validator.validate_path(path)).to eq(false)

     Errno::ENOENT:
       No such file or directory @ rb_check_realpath_internal - /Workspace/other/project/spec/routing

I'm running with:

Ruby 2.7.7
RSpec 3.10.0
fakefs 2.0.0
grosser commented 1 year ago

nice digging! PR welcome