Skip to content

Commit

Permalink
Added specs for copy file.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jun 10, 2009
1 parent ea5f783 commit 0d619a2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .autotest
@@ -1,7 +1,8 @@
Autotest.add_hook :initialize do |at|
at.clear_mappings
at.add_exception(/\.git/)
at.add_exception(/spec\/sandbox/)
at.add_mapping(%r{^spec/.*_spec}) {|filename,_| at.files_matching %r{#{filename}}}
at.add_mapping(%r{^bin/thor}) {|_,_| at.files_matching %r{spec/thor_runner_spec}}
at.add_mapping(%r{}) {|_,_| at.files_matching %r{spec/.*_spec}}
end
end
2 changes: 1 addition & 1 deletion lib/thor/actions/copy_file.rb
Expand Up @@ -5,7 +5,7 @@ module Actions
# destination root respectively.
#
def copy_file(source, destination=nil)
action CopyFile.new(source, destination || source)
action CopyFile.new(self, source, destination || source)
end

class CopyFile #:nodoc:
Expand Down
71 changes: 71 additions & 0 deletions spec/actions/copy_file_spec.rb
@@ -0,0 +1,71 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'thor/actions'

describe Thor::Actions::CopyFile do
before(:each) do
::FileUtils.rm_r(destination_root, :force => true)
end

def copy_file(source, destination=nil)
@base ||= begin
base = Object.new
mock(base).source_root{ source_root }
mock(base).destination_root{ destination_root }
base
end

@action ||= Thor::Actions::CopyFile.new(base, source, destination || source)
end

describe "#source" do
it "sets the source based on the source root" do
copy_file("task.thor").source.must == File.join(source_root, 'task.thor')
end
end

describe "#destination" do
it "sets the destination based on the destinatino root" do
copy_file("task.thor").destination.must == File.join(destination_root, 'task.thor')
end
end

describe "#invoke!" do
it "copies the file to the destination root" do
copy_file("task.thor").invoke!
File.exists?(@action.destination).must be_true
FileUtils.identical?(@action.source, @action.destination).must be_true
end
end

describe "#revoke!" do
it "removes the destination file" do
copy_file("task.thor").invoke!
@action.revoke!
File.exists?(@action.destination).must be_false
end
end

describe "#show" do
it "shows file content" do
copy_file("task.thor").show.must == File.read(File.join(source_root, "task.thor"))
end
end

describe "#exists?" do
it "returns true if the destination file exists" do
copy_file("task.thor")
@action.exists?.must be_false
@action.invoke!
@action.exists?.must be_true
end
end

describe "#identical?" do
it "returns true if the destination file and is identical" do
copy_file("task.thor")
@action.identical?.must be_false
@action.invoke!
@action.identical?.must be_true
end
end
end
9 changes: 8 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -3,7 +3,6 @@
$:.push File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'thor'
require 'thor/group'
require 'stringio'
require 'rubygems'
require 'rr'
Expand Down Expand Up @@ -42,5 +41,13 @@ def capture(stream)
result
end

def source_root
File.join(File.dirname(__FILE__), 'fixtures')
end

def destination_root
File.join(File.dirname(__FILE__), 'sandbox')
end

alias silence capture
end
2 changes: 1 addition & 1 deletion task.thor
Expand Up @@ -7,7 +7,7 @@ class Amazing < Thor
ret = "#{name} is amazing"
puts options[:forcefully] ? ret.upcase : ret
end

desc "hello", "say hello"
def hello
puts "Hello"
Expand Down

0 comments on commit 0d619a2

Please sign in to comment.