Permalink
Browse files

Prevent double injection unless force == true.

  • Loading branch information...
1 parent c999a8d commit 499f8e7e9c6ee2c20aff4c08d66f6d3b23dd40aa Davide D'Agostino committed with josevalim Jan 26, 2010
Showing with 32 additions and 2 deletions.
  1. +3 −2 lib/thor/actions/inject_into_file.rb
  2. +29 −0 spec/actions/inject_into_file_spec.rb
@@ -10,7 +10,8 @@ module Actions
# destination<String>:: Relative path to the destination root
# data<String>:: Data to add to the file. Can be given as a block.
# config<Hash>:: give :verbose => false to not log the status and the flag
- # for injection (:after or :before).
+ # for injection (:after or :before) or :force => true for
+ # insert two or more times the same content.
#
# ==== Examples
#
@@ -55,7 +56,7 @@ def invoke!
replacement + '\0'
end
- replace!(/#{flag}/, content)
+ replace!(/#{flag}/, content) if config[:force] || !File.binread(destination).include?(replacement)
end
def revoke!
@@ -55,6 +55,35 @@ def file
invoke! "doc/README", "\nmore content", :after => "__start__"
File.read(file).must == "__start__\nREADME\n__end__\n"
end
+
+ it "does not change the file if already include content" do
+ invoke! "doc/README", :before => "__end__" do
+ "more content\n"
+ end
+
+ File.read(file).must == "__start__\nREADME\nmore content\n__end__\n"
+
+ invoke! "doc/README", :before => "__end__" do
+ "more content\n"
+ end
+
+ File.read(file).must == "__start__\nREADME\nmore content\n__end__\n"
+ end
+
+ it "does change the file if already include content and :force == true" do
+ invoke! "doc/README", :before => "__end__" do
+ "more content\n"
+ end
+
+ File.read(file).must == "__start__\nREADME\nmore content\n__end__\n"
+
+ invoke! "doc/README", :before => "__end__", :force => true do
+ "more content\n"
+ end
+
+ File.read(file).must == "__start__\nREADME\nmore content\nmore content\n__end__\n"
+ end
+
end
describe "#revoke!" do

0 comments on commit 499f8e7

Please sign in to comment.