Skip to content

Commit

Permalink
Adding the basics for Pathname support (ref #13)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonc committed Jun 9, 2016
1 parent 8945558 commit e22c325
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 8 deletions.
16 changes: 16 additions & 0 deletions lib/memfs.rb
Expand Up @@ -22,10 +22,16 @@ module MemFs
# Keeps track of the original Ruby File class.
OriginalFile = ::File

if defined?(::Pathname)
# Keeps track of the original Ruby Pathname class.
OriginalPathname = ::Pathname
end

require 'memfs/file_system'
require 'memfs/dir'
require 'memfs/file'
require 'memfs/file/stat'
require 'memfs/pathname'

# Calls the given block with MemFs activated.
#
Expand Down Expand Up @@ -77,6 +83,11 @@ def activate!

const_set :Dir, MemFs::Dir
const_set :File, MemFs::File

if defined?(::Pathname)
remove_const :Pathname
const_set :Pathname, MemFs::Pathname
end
end

MemFs::FileSystem.instance.clear!
Expand All @@ -96,6 +107,11 @@ def deactivate!

const_set :Dir, MemFs::OriginalDir
const_set :File, MemFs::OriginalFile

if defined?(::Pathname)
remove_const :Pathname
const_set :Pathname, MemFs::OriginalPathname
end
end
end
module_function :deactivate!
Expand Down
20 changes: 20 additions & 0 deletions lib/memfs/pathname.rb
@@ -0,0 +1,20 @@
require 'memfs/filesystem_access'

module MemFs
class Pathname
extend FilesystemAccess

def initialize(path)
@path = '' + path
end

def to_s
path
end
alias_method :to_str, :to_s

private

attr_reader :path
end
end
6 changes: 6 additions & 0 deletions spec/memfs/file_spec.rb
Expand Up @@ -944,6 +944,12 @@ module MemFs
expect(path).to eq '/some/path'
end
end

context 'when the path cannot be converted to a string' do
it 'raises an exception' do
expect { subject.path(42) }.to raise_error(ArgumentError)
end
end
end

describe '.pipe?' do
Expand Down
40 changes: 40 additions & 0 deletions spec/memfs/pathname_spec.rb
@@ -0,0 +1,40 @@
require 'spec_helper'

module MemFs
describe Pathname do
describe 'Class Methods' do
subject { MemFs::Pathname }

describe '.new' do
it 'creates a Pathname object from the given path' do
pathname = subject.new('some/path')
expect(pathname.to_s).to eq 'some/path'
end

context 'when the given path is not a string' do
it 'creates a Pathname object from the string representation of the path' do
path = MemFs::Pathname.new('some/path')
pathname = MemFs::Pathname.new(path)
expect(pathname.to_s).to eq 'some/path'
end
end
end
end

describe "Instance Methods" do
subject(:pathname) { MemFs::Pathname.new('some/path') }

describe '#to_str' do
it 'can be converted to a string object' do
expect('test ' + pathname).to eq 'test some/path'
end
end

describe '#to_s' do
it "returns the path as a String" do
expect(pathname.to_s).to eq 'some/path'
end
end
end
end
end
52 changes: 44 additions & 8 deletions spec/memfs_spec.rb
Expand Up @@ -23,31 +23,67 @@
end

describe '.activate!' do
before(:each) { described_class.activate! }
after(:each) { described_class.deactivate! }

it 'replaces Ruby Dir class with a fake one' do
subject.activate!
expect(::Dir).to be(described_class::Dir)
subject.deactivate!
end

it 'replaces Ruby File class with a fake one' do
subject.activate!
expect(::File).to be(described_class::File)
subject.deactivate!
end
end

describe '.deactivate!' do
before :each do
described_class.activate!
described_class.deactivate!
context 'when Pathname is defined' do
it 'replaces Ruby Pathname class with a fake one' do
class ::Pathname; end
subject.activate!
expect(::Pathname).to be(described_class::Pathname)
subject.deactivate!
Object.send(:remove_const, :Pathname)
end
end

context 'when Pathname is not defined' do
it 'does not replace Ruby Pathname class with a fake one' do
subject.activate!
expect { Object.const_get(:Pathname) }.to raise_error(NameError)
subject.deactivate!
end
end
end

describe '.deactivate!' do
it 'sets back the Ruby Dir class to the original one' do
subject.activate!
subject.deactivate!
expect(::Dir).to be(described_class::OriginalDir)
end

it 'sets back the Ruby File class to the original one' do
subject.activate!
subject.deactivate!
expect(::File).to be(described_class::OriginalFile)
end

context 'when Pathname is defined' do
it 'sets back the Ruby Pathname class to the original one' do
class ::Pathname; end
subject.activate!
subject.deactivate!
expect(::Pathname).to be(described_class::OriginalPathname)
Object.send(:remove_const, :Pathname)
end
end

context 'when Pathname is not defined' do
it 'does not set the Pathname constant' do
subject.activate!
subject.deactivate!
expect { Object.const_get(:Pathname) }.to raise_error(NameError)
end
end
end

describe '.touch' do
Expand Down

0 comments on commit e22c325

Please sign in to comment.