Skip to content

Instantly share code, notes, and snippets.

@njonsson
Created April 23, 2010 11:56
Show Gist options
  • Save njonsson/376471 to your computer and use it in GitHub Desktop.
Save njonsson/376471 to your computer and use it in GitHub Desktop.
module DirectoriesAndFiles
module ClassMethods
def directories(path)
new(path).directories
end
def files(path)
new(path).files
end
end
def self.included(other_module)
other_module.extend ClassMethods
end
def directories
Dir.glob "#{path}/*/", File::FNM_DOTMATCH
end
def files
filter_entries do |path|
File.file? path
end
end
private
def filter_entries
entries.select do |e|
yield "#{path}/#{e}"
end
end
end
Dir.class_eval do
include DirectoriesAndFiles
end
require 'spec'
require 'directories_and_files'
describe Dir do
SANDBOX = 'tmp'
def create_sandbox(options={})
FileUtils.mkdir_p SANDBOX
Array(options[:files]).each do |filename|
FileUtils.touch "#{SANDBOX}/#{filename}"
end
Array(options[:directories]).each do |directory|
FileUtils.mkdir_p "#{SANDBOX}/#{directory}"
end
Dir.new SANDBOX
end
def destroy_sandbox
FileUtils.rm_rf SANDBOX
end
after :each do
destroy_sandbox
end
describe 'class' do
it 'should show DirectoriesAndFiles as an ancestor' do
Dir.ancestors.should include(DirectoriesAndFiles)
end
end
describe '-- when a path' do
describe 'does not exist,' do
describe 'and when sent class method' do
describe "'directories' with that path --" do
it 'should raise the expected error' do
lambda {
Dir.directories 'NONEXISTENT_PATH'
}.should raise_error(Errno::ENOENT,
'No such file or directory - NONEXISTENT_PATH')
end
end
describe "'files' with that path --" do
it 'should raise the expected error' do
lambda {
Dir.files 'NONEXISTENT_PATH'
}.should raise_error(Errno::ENOENT,
'No such file or directory - NONEXISTENT_PATH')
end
end
end
end
describe 'contains' do
describe 'no entries,' do
before :each do
create_sandbox
end
describe 'and when sent class method' do
describe "'directories' with that path --" do
it 'should return the expected names' do
Dir.directories(SANDBOX).should == ["#{SANDBOX}/./",
"#{SANDBOX}/../"]
end
end
describe "'files' with that path --" do
it 'should return an empty array' do
Dir.files(SANDBOX).should == []
end
end
end
end
describe 'directories but no files,' do
before :each do
create_sandbox :directories => ['.foo', 'b a r']
end
describe 'and when sent class method' do
describe "'directories' with that path --" do
it 'should return the expected names' do
Dir.directories(SANDBOX).should == ["#{SANDBOX}/./",
"#{SANDBOX}/../",
"#{SANDBOX}/.foo/",
"#{SANDBOX}/b a r/"]
end
end
describe "'files' with that path --" do
it 'should return an empty array' do
Dir.files(SANDBOX).should == []
end
end
end
end
describe 'files but no directories,' do
before :each do
create_sandbox :files => ['.foo', 'b a r']
end
describe 'and when sent class method' do
describe "'directories' with that path --" do
it 'should return the expected names' do
Dir.directories(SANDBOX).should == ["#{SANDBOX}/./",
"#{SANDBOX}/../"]
end
end
describe "'files' with that path --" do
it 'should return the expected names' do
Dir.files(SANDBOX).should == ['.foo', 'b a r']
end
end
end
end
describe 'both directories and files,' do
before :each do
create_sandbox :directories => ['.foo', 'b a r'],
:files => ['.baz', 'b a t']
end
describe 'and when sent class method' do
describe "'directories' with that path --" do
it 'should return the expected names' do
Dir.directories(SANDBOX).should == ["#{SANDBOX}/./",
"#{SANDBOX}/../",
"#{SANDBOX}/.foo/",
"#{SANDBOX}/b a r/"]
end
end
describe "'files' with that path --" do
it 'should return the expected names' do
Dir.files(SANDBOX).should == ['.baz', 'b a t']
end
end
end
end
end
end
describe 'with a path that contains' do
describe 'no entries,' do
before :each do
@dir = create_sandbox
end
describe 'and when sent class method' do
describe "'directories' with that path --" do
it 'should return the expected names' do
@dir.directories.should == ["#{SANDBOX}/./", "#{SANDBOX}/../"]
end
end
describe "'files' with that path --" do
it 'should return an empty array' do
@dir.files.should == []
end
end
end
end
describe 'directories but no files,' do
before :each do
@dir = create_sandbox(:directories => ['.foo', 'b a r'])
end
describe 'and when sent class method' do
describe "'directories' with that path --" do
it 'should return the expected names' do
@dir.directories.should == ["#{SANDBOX}/./",
"#{SANDBOX}/../",
"#{SANDBOX}/.foo/",
"#{SANDBOX}/b a r/"]
end
end
describe "'files' with that path --" do
it 'should return an empty array' do
@dir.files.should == []
end
end
end
end
describe 'files but no directories,' do
before :each do
@dir = create_sandbox(:files => ['.foo', 'b a r'])
end
describe 'and when sent class method' do
describe "'directories' with that path --" do
it 'should return the expected names' do
@dir.directories.should == ["#{SANDBOX}/./", "#{SANDBOX}/../"]
end
end
describe "'files' with that path --" do
it 'should return the expected names' do
@dir.files.should == ['.foo', 'b a r']
end
end
end
end
describe 'both directories and files,' do
before :each do
@dir = create_sandbox(:directories => ['.foo', 'b a r'],
:files => ['.baz', 'b a t'])
end
describe 'and when sent class method' do
describe "'directories' with that path --" do
it 'should return the expected names' do
@dir.directories.should == ["#{SANDBOX}/./",
"#{SANDBOX}/../",
"#{SANDBOX}/.foo/",
"#{SANDBOX}/b a r/"]
end
end
describe "'files' with that path --" do
it 'should return the expected names' do
@dir.files.should == ['.baz', 'b a t']
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment