memfs icon indicating copy to clipboard operation
memfs copied to clipboard

Pathname#write does not work

Open mikz opened this issue 11 years ago • 6 comments

Pathname is implemented in C and does not reflect MemFS filesystem.

mikz avatar Oct 01 '14 09:10 mikz

Hi @mikz, thanks for your feedback. Could you provide a code example that could help reproduce your issue? I know it's a large issue but having a starting point would be cool :)

simonc avatar Oct 01 '14 09:10 simonc

require 'memfs'
MemFs.activate!
path = Pathname('folder/file')
MemFs.touch(path)
File.exist?(path.to_s) or raise 'fail'
path.write('test')

also there is another bug with Pathname, and it is:

require 'memfs'
path = Pathname('folder/file')
File.exist?(path.to_s) == File.exist?(path) or raise 'failed'
MemFs.activate!
File.exist?(path.to_s) == File.exist?(path) or raise 'failed'

It raises ArgumentError: wrong number of arguments (2 for 0)

from /Users/mikz/.rvm/gems/ruby-2.1.2/gems/memfs-0.4.1/lib/memfs/fake/directory.rb:23:in `split'

because it tries to call split('/', 2) on Pathname which does not accept params and splits to dirname and basename.

mikz avatar Oct 01 '14 10:10 mikz

Thank you ! I'll try to work on this issue in the coming days ;)

simonc avatar Oct 01 '14 10:10 simonc

Ok I started to look into it and it will probably take time since I'll have to literally replace Pathname, or at least an important part of it. I'll focus on the completion of the File class for now and come back to afterward. I already have the switching process in a branch though ;)

simonc avatar Oct 05 '14 15:10 simonc

Hey @simonc, I am curious about the status of this issue, as there are other Pathname functions that don't work, for example:

require 'pathname'
Dir.mkdir("/directory")

File.directory?("/directory")
# => true

Pathname.new("/directory").directory?
# => false

I'd be happy to help with a PR for this, somehow, although I'm afraid I'm not sure how one would go about implementing C classes in MemFs.

It could be that we just need to tack on a .map(&:to_s) somewhere like in @craigw's PR, maybe in File::Stat#directory?. I hope?!

awolfson avatar Jun 07 '16 21:06 awolfson

Hey @awolfson.

Sadly I ended up not having enough time to dig the subject. The only thing I can think of, regarding Pathname is to rewrite the entire class the same way File and Dir are rewritten in MemFs. I guess some parts of it can be delegated to the original class but every method interacting with the file system would have to be rewritten.

Regarding the .map(&:to_s) technique it may work for certain situations but not for others.

Anyway, I pushed what I had started on the pathname-support branch if anyone is interested in taking a stab at it 😁

simonc avatar Jun 09 '16 07:06 simonc