Permalink
Browse files

Added specs for copy file.

  • Loading branch information...
1 parent ea5f783 commit 0d619a27b9e5d576a46d68cd6e990a94fd5079d9 @josevalim josevalim committed Jun 10, 2009
Showing with 83 additions and 4 deletions.
  1. +2 −1 .autotest
  2. +1 −1 lib/thor/actions/copy_file.rb
  3. +71 −0 spec/actions/copy_file_spec.rb
  4. +8 −1 spec/spec_helper.rb
  5. +1 −1 task.thor
View
@@ -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
@@ -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:
@@ -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
View
@@ -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'
@@ -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
View
@@ -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"

0 comments on commit 0d619a2

Please sign in to comment.