spec/base_spec.rb in fs-0.1.0 vs spec/base_spec.rb in fs-0.1.1
- old
+ new
@@ -320,6 +320,255 @@
FS.changedir('foo')
Dir.pwd.should eql(File.join(here, 'foo'))
end
end
+ describe 'tree' do
+ before(:each) do
+ FS.touch('a.file')
+ FS.makedir('baz')
+ FS.touch('baz/b.file')
+ FS.mkdir('baz/bar')
+ FS.touch('baz/bar/c.file')
+ FS.touch('baz/d.file')
+ FS.makedir('foo')
+ FS.touch('foo/e.file')
+ end
+
+ it 'returns the tree of the current dir' do
+ tree = <<-TXT
+.
+|-- a.file
+|-- baz
+| |-- b.file
+| |-- bar
+| | `-- c.file
+| `-- d.file
+`-- foo
+ `-- e.file
+TXT
+ FS.tree.should eql(tree.strip)
+ end
+
+ it 'returns the tree of a dir' do
+ tree = <<-TXT
+baz
+|-- b.file
+|-- bar
+| `-- c.file
+`-- d.file
+TXT
+ FS.tree('baz').should eql(tree.strip)
+ end
+
+ end
+
+ describe 'exist?' do
+ it 'returns if a path exist' do
+ FS.makedir('foo')
+ FS.touch('bar')
+ FS.exist?('foo').should be_true
+ FS.exist?('bar').should be_true
+ FS.exist?('baz').should be_false
+ end
+ end
+
+ describe 'directory?' do
+ it 'checks for a directory' do
+ FS.makedir('foo')
+ FS.touch('bar')
+ FS.directory?('foo').should be_true
+ FS.directory?('bar').should be_false
+ FS.directory?('baz').should be_false
+ end
+ end
+
+ describe 'file?' do
+ it 'checks for a file' do
+ FS.makedir('foo')
+ FS.touch('bar')
+ FS.file?('foo').should be_false
+ FS.file?('bar').should be_true
+ FS.file?('baz').should be_false
+ end
+ end
+
+ describe 'empty?' do
+ it 'returns nil if the path does not exist' do
+ FS.exist?('foobar').should be_false
+ FS.empty?('foobar').should be_nil
+ end
+
+ it 'returns if a file is empty' do
+ FS.touch('empty.file')
+ FS.write('content.file', 'something')
+ FS.empty?('empty.file').should be_true
+ FS.empty?('content.file').should be_false
+ end
+
+ it 'returns if a dir is empty' do
+ FS.mkdir('empty.dir')
+ FS.mkdir('content.dir')
+ FS.touch('content.dir/some.file')
+ FS.empty?('empty.dir').should be_true
+ FS.empty?('content.dir').should be_false
+ end
+ end
+
+ describe 'join' do
+ it 'joins pathes' do
+ FS.join('foo', 'bar').should eql('foo/bar')
+ FS.join('foo', '/bar').should eql('foo/bar')
+ FS.join('foo/', 'bar').should eql('foo/bar')
+ end
+ end
+
+ describe 'expand_path' do
+ it 'expands pathes' do
+ here = File.expand_path('.')
+ FS.expand_path('.').should eql(here)
+ FS.expand_path('foo').should eql(File.join(here, 'foo'))
+ FS.expand_path('foo/bar').should eql(File.join(here, 'foo', 'bar'))
+ end
+ end
+
+ describe 'absolute?' do
+ it 'checks for an absolute path' do
+ FS.absolute?('/').should be_true
+ FS.absolute?('/foo').should be_true
+ FS.absolute?('.').should be_false
+ FS.absolute?('foo').should be_false
+ end
+ end
+
+ describe 'dirname' do
+ it 'extracts the dir of a path' do
+ FS.dirname('tmp/foo/bar.todo').should eql('tmp/foo')
+ FS.dirname('tmp/foo').should eql('tmp')
+ FS.dirname('tmp/foo/').should eql('tmp')
+ FS.dirname('/tmp').should eql('/')
+ FS.dirname('/').should eql('/')
+ FS.dirname('.').should eql('.')
+ end
+ end
+
+ describe 'basename' do
+ it 'extracts the base of a path' do
+ FS.basename('tmp/foo/bar.todo').should eql('bar.todo')
+ FS.basename('tmp/foo').should eql('foo')
+ FS.basename('tmp/foo/').should eql('foo')
+ FS.basename('/tmp').should eql('tmp')
+ FS.basename('/').should eql('/')
+ FS.basename('.').should eql('.')
+ end
+ end
+
+ describe 'filename' do
+ it 'extracts the filename of a path' do
+ FS.filename('tmp/foo/bar.todo').should eql('bar')
+ FS.filename('tmp/foo').should eql('foo')
+ FS.filename('tmp/foo/').should eql('foo')
+ FS.filename('/tmp').should eql('tmp')
+ FS.filename('/').should eql('') # this is not like FS.basename
+ FS.filename('.').should eql('') # this is not like FS.basename
+ FS.filename('foo.bar.txt').should eql('foo.bar')
+ end
+ end
+
+ describe 'extname' do
+ it 'extracts the extension of a path' do
+ FS.extname('tmp/foo/bar.todo').should eql('.todo')
+ FS.extname('tmp/foo').should eql('')
+ FS.extname('tmp/foo/').should eql('')
+ FS.extname('/tmp').should eql('')
+ FS.extname('/').should eql('')
+ FS.extname('.').should eql('')
+ FS.extname('foo.bar.txt').should eql('.txt')
+ end
+ end
+
+ describe 'splitname' do
+ it 'splits the parts of a path' do
+ FS.splitname('tmp/foo/bar.todo').should eql(["tmp/foo", "bar", ".todo"])
+ FS.splitname('tmp/foo').should eql(['tmp', 'foo', ''])
+ FS.splitname('tmp/foo/').should eql(['tmp', 'foo', ''])
+ FS.splitname('/tmp').should eql(['/', 'tmp', ''])
+ FS.splitname('/').should eql(['/', '', ''])
+ FS.splitname('.').should eql(['.', '', ''])
+ end
+ end
+
+ describe 'this_file' do
+ it 'returns this file' do
+ FS.this_file.should eql(__FILE__)
+ end
+ end
+
+ describe 'this_dir' do
+ it 'returns the dir of this file' do
+ FS.this_dir.should eql(File.dirname(__FILE__))
+ end
+ end
+
+ describe 'tempdir' do
+ it 'returns the current temp dir' do
+ FS.tempdir.should eql(Dir.tmpdir)
+ end
+ end
+
+ describe 'maketempdir' do
+ it 'creates a new dir in the default temp dir' do
+ dir = FS.maketempdir
+ File.exist?(dir).should be_true
+ File.directory?(dir).should be_true
+ Dir.entries(dir).should eql([".", ".."])
+ Dir.entries(Dir.tmpdir).should include(File.basename(dir))
+ end
+
+ it 'creates a new temp dir with the given prefix' do
+ dir = FS.maketempdir('my_dir')
+ dir.should match(/\/my_dir/)
+ File.exist?(dir).should be_true
+ File.directory?(dir).should be_true
+ Dir.entries(dir).should eql([".", ".."])
+ Dir.entries(Dir.tmpdir).should include(File.basename(dir))
+ end
+
+ it 'creates a new temp dir inside of the given dir' do
+ parent_dir = FS.maketempdir('parent_dir')
+ dir = FS.maketempdir(nil, parent_dir)
+ File.exist?(dir).should be_true
+ File.directory?(dir).should be_true
+ Dir.entries(dir).should eql([".", ".."])
+ Dir.entries(parent_dir).should include(File.basename(dir))
+ end
+ end
+
+ describe 'maketempfile' do
+ it 'creates a new file in the default temp dir' do
+ file = FS.maketempfile
+ FS.exist?(file).should be_true
+ FS.file?(file).should be_true
+ FS.empty?(file).should be_true
+ FS.list(Dir.tmpdir).should include(File.basename(file))
+ end
+
+ it 'creates a new temp file with the given prefix' do
+ file = FS.maketempfile('my_file')
+ file.should match(/\/my_file/)
+ FS.exist?(file).should be_true
+ FS.file?(file).should be_true
+ FS.empty?(file).should be_true
+ FS.list(Dir.tmpdir).should include(File.basename(file))
+ end
+
+ it 'creates a new temp file inside of the given dir' do
+ parent_dir = FS.maketempdir('parent_dir')
+ file = FS.maketempfile(nil, parent_dir)
+ FS.exist?(file).should be_true
+ FS.file?(file).should be_true
+ FS.empty?(file).should be_true
+ FS.list(parent_dir).should include(File.basename(file))
+ end
+ end
+
end