Skip to content

Commit

Permalink
merge fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey committed Mar 31, 2008
2 parents 1f96525 + 91bc892 commit f7c91ad
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 58 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make recursive permisions
1 change: 1 addition & 0 deletions lib/pow/directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def children(options={})
children = []
Dir.foreach(path) do |child|
child_path = ::File.join(path, child)

next if child == '.'
next if child == '..'
next if (::File.file?(child_path) and not options[:include_files])
Expand Down
2 changes: 1 addition & 1 deletion lib/pow/pow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def parent

def permissions=(mode)
mode = mode.to_s.to_i(8) # convert from octal
path.chmod(mode)
FileUtils.chmod(mode, path)
end

def permissions
Expand Down
62 changes: 31 additions & 31 deletions spec/directory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,69 @@

require File.dirname(__FILE__) + '/../lib/pow'

context "A Directory object" do
describe "A Directory object" do
setup do
FileUtils.mkpath "./test_dir/sub_dir"

@dir = Pow["./test_dir"]
@sub_dir = @dir["sub_dir"]
@dir = Pow("./test_dir")
@sub_dir = @dir/"sub_dir"
end

teardown do
FileUtils.rm_r @dir.to_s if FileTest.exist?(@dir.to_s)
end

specify "should have a correct name." do
it "has valid name" do
@dir.name.should == "test_dir"
end

specify "should know it exists." do
it "knows it exists" do
@sub_dir.exists?.should be_true
end

specify "should remove itself." do
it "removes itself" do
::Dir.should_receive(:rmdir).with(@sub_dir.path)
@sub_dir.delete
@sub_dir.should_not be_exist
end

specify "should remove all subdirectories." do
it "should remove all subdirectories." do
@dir.delete!
@dir.should_not be_exist
@sub_dir.should_not be_exist
end

specify "should an raise error if it tries to delete itself but is not empty." do
it "should an raise error if it tries to delete itself but is not empty." do
lambda {@dir.delete}.should raise_error(PowError)

@dir.should be_exist
@sub_dir.should be_exist
end

specify "should be able to set the permissions." do
it "should be able to set the permissions." do
@dir.permissions = 555
File.should_not be_writable(@dir.to_s)

@dir.permissions = 777
File.should be_writable(@dir.to_s)
end

specify "should be able to read the permissions." do
FileUtils.chmod(0555, @dir.path.to_s)
it "should be able to read the permissions." do
FileUtils.chmod(555, @dir.path.to_s)
@dir.permissions.should == 555

FileUtils.chmod(0777, @dir.path.to_s)
FileUtils.chmod(777, @dir.path.to_s)
@dir.permissions.should == 777
end

specify "should be copyable" do
it "should be copyable" do
copy_path = "./test_dir/sub_dir_copy"
@sub_dir.copy_to(copy_path)

File.exists?(copy_path)
Pow[copy_path].should be_kind_of(Pow::Directory)
end

specify "should be moveable" do
it "should be moveable" do
move_path = "./test_dir/moved_sub_dir"
@sub_dir.move_to(move_path)

Expand All @@ -78,12 +78,12 @@
Pow[move_path].should be_kind_of(Pow::Directory)
end

specify "should have a parent dir" do
it "should have a parent dir" do
@sub_dir.parent.should == @dir
end
end

context "The children of a Directory" do
describe "The children of a Directory" do
setup do
FileUtils.mkpath "./earth/people"
FileUtils.mkpath "./earth/places"
Expand All @@ -98,12 +98,12 @@
FileUtils.rm_r @dir.to_s if FileTest.exist?(@dir.to_s)
end

specify "should include all files and directories." do
it "should include all files and directories." do
@dir.should have(5).children
@dir.children.each { |child| [Pow::File, Pow::Directory].should be_member(child.class) }
end

specify "should allow you to select files." do
it "should allow you to select files." do
@dir.should have(2).children(true, false)
@dir.children(true, false).should == @dir.files

Expand All @@ -112,7 +112,7 @@
@dir.files.collect {|file| file.name}.should be_member("history.txt")
end

specify "should allow you to select directories." do
it "should allow you to select directories." do
@dir.should have(3).children(false, true)
@dir.children(false, true).should == @dir.directories

Expand All @@ -123,7 +123,7 @@
@dir.directories.collect {|directory| directory.name}.should be_member("things")
end

specify "should be accessable via glob." do
it "should be accessable via glob." do
@dir.glob("*").size.should == 5
@dir.glob("*").each { |child| [Pow::File, Pow::Directory].should be_member(child.class) }
@dir.glob("*").collect {|path| path.name}.should be_member("history.txt")
Expand All @@ -134,12 +134,12 @@
end
end

context "Then enumerable bits of a Directory" do
specify "should include enumerable." do
describe "Then enumerable bits of a Directory" do
it "should include enumerable." do
Pow::Directory.ancestors.should be_member(Enumerable)
end

specify "should call every child when each is called." do
it "should call every child when each is called." do
dir = Pow::Directory[("./test_dir")].create

child_one = mock("child_one")
Expand All @@ -155,7 +155,7 @@
end
end

context "Nested directory objects" do
describe "Nested directory objects" do
setup do
FileUtils.mkpath "./test_dir/sub_dir"
@dir = Pow::Directory["./test_dir"]
Expand All @@ -166,20 +166,20 @@
FileUtils.rm_r @dir.to_s if FileTest.exist?(@dir.to_s)
end

specify "should be accessible when joined" do
it "should be accessible when joined" do
path = Pow::Directory["test_dir", "sub_dir"]
path.exists?.should be_true
path.should be_instance_of(Pow::Directory)
end

specify "should be accessible from path object" do
it "should be accessible from path object" do
path = @dir["sub_dir"]
path.exists?.should be_true
path.should be_instance_of(Pow::Directory)
end
end

context "Using blocks to create Directory structure" do
describe "Using blocks to create Directory structure" do
setup do
FileUtils.mkpath "./test_dir/"
@dir = Pow.open("./test_dir")
Expand All @@ -189,13 +189,13 @@
FileUtils.rm_r @dir.to_s if FileTest.exist?(@dir.to_s)
end

specify "should change the working directory" do
it "should change the working directory" do
@dir.open do |path|
Dir.pwd.should == path.to_s
end
end

specify "should go back to old dir if block raises error" do
it "should go back to old dir if block raises error" do
current_dir = Dir.pwd

begin
Expand All @@ -208,7 +208,7 @@
Dir.pwd.should == current_dir
end

specify "should create sub directories when within a block" do
it "should create sub directories when within a block" do
@dir["sub_dir"].create do
Pow["sub_sub_dir"].create
end
Expand Down
74 changes: 48 additions & 26 deletions spec/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,107 @@

require File.dirname(__FILE__) + '/../lib/pow'

context Pow::File do
describe Pow::File do
setup do
@dir_pathname = "./test_dir"
@filename = "file.txt"
FileUtils.mkpath @dir_pathname
open("#{@dir_pathname}/#{@filename}", "w+") {|f| f.write "hello"}

@dir = Pow.open(@dir_pathname)
@file = Pow.open("#{@dir_pathname}/#{@filename}")
@dir = Pow(@dir_pathname)
@file = Pow("#{@dir_pathname}/#{@filename}")
end

teardown do
FileUtils.rm_r @dir_pathname
end

specify "should have a correct name." do
it "has correct name" do
@file.name.should == "file.txt"
end

specify "should have a correct extension." do
it "matches regular expression for extention" do
@file.name.should =~ /txt/
end

it "matches regular expression for basename" do
@file.name.should =~ /file/
end

it "has correct extension" do
@file.extention.should == "txt"
end

specify "should have an empty extention if there is none." do
it "returns nil if there is no extension" do
open("#{@dir_pathname}/README", "w+") {|f| f.write "readme"}
extensionless_file = Pow.open("#{@dir_pathname}/README")
extensionless_file = Pow("#{@dir_pathname}/README")

extensionless_file.extention.should == ""
extensionless_file.extention.should == nil
end

specify "should know it exists." do
it "is aware of its existence" do
@file.exists?.should be_true
end

specify "should remove itself." do
it "is aware of its inexistence" do
Pow(@dir, :this, :is, :a, :fake, :file).exists?.should be_false
end

it "knows when it is not empty" do
@file.empty?.should be_false
end

it "knows when it is empty" do
empty_file = Pow("#{@dir_pathname}/empty.txt")
open(empty_file.path, "w+") {|f| f.write ""}
empty_file.empty?.should be_true
end

it "deletes itself" do
::File.should_receive(:delete).with(@file.path)
@file.delete
@file.should_not be_exist
end

specify "should be able to set the permissions." do
@file.permissions = 555
it "can set its permissions" do
@file.permissions = 515
File.should_not be_writable(@file.to_s)

@file.permissions = 777
File.should be_writable(@file.to_s)
end

specify "should be able to read the permissions." do
FileUtils.chmod(0555, @file.path.to_s)
@file.permissions.should == 555
it "can read the permissions" do
FileUtils.chmod(0514, @file.path.to_s)
@file.permissions.should == 514

FileUtils.chmod(0777, @file.path.to_s)
@file.permissions.should == 777
FileUtils.chmod(0731, @file.path.to_s)
@file.permissions.should == 731
end

specify "should be openable!" do
it "can be read" do
@file.open do |file|
file.read.should == "hello"
end
end

specify "should be copyable" do
copy_path = "./test_dir/file_copy.txt"
it "can be copied" do
copy_path = @file.path + ".copy"
@file.copy_to(copy_path)

File.exists?(copy_path).should be_true
Pow[copy_path].should be_kind_of(Pow::File)
Pow(copy_path).should be_kind_of(Pow::File)
end

specify "should be moveable" do
move_path = "./test_dir/file_move.txt"
it "can be moved" do
move_path = @file.path + ".move"
@file.move_to(move_path)

File.exists?(move_path).should be_true
File.exists?(@file.to_s).should_not be_true
Pow[move_path].should be_kind_of(Pow::File)
Pow(move_path).should be_kind_of(Pow::File)
end

specify "should have a parent dir" do
it "has a parent dir" do
@file.parent.should == @dir
end
end

0 comments on commit f7c91ad

Please sign in to comment.