diff --git a/lib/thor/actions/inject_into_file.rb b/lib/thor/actions/inject_into_file.rb index 4f7791da..89e83af0 100644 --- a/lib/thor/actions/inject_into_file.rb +++ b/lib/thor/actions/inject_into_file.rb @@ -106,7 +106,6 @@ def say_status(behavior, warning: nil, color: nil) # Adds the content to the file. # def replace!(regexp, string, force) - return if pretend? content = File.read(destination) before, after = content.split(regexp, 2) snippet = (behavior == :after ? after : before).to_s @@ -114,7 +113,7 @@ def replace!(regexp, string, force) if force || !snippet.include?(replacement) success = content.gsub!(regexp, string) - File.open(destination, "wb") { |file| file.write(content) } + File.open(destination, "wb") { |file| file.write(content) } unless pretend? success end end diff --git a/spec/actions/inject_into_file_spec.rb b/spec/actions/inject_into_file_spec.rb index 36b8d7b5..6305046e 100644 --- a/spec/actions/inject_into_file_spec.rb +++ b/spec/actions/inject_into_file_spec.rb @@ -72,6 +72,11 @@ def file expect(invoke!("doc/README", "\nmore content", :after => "__start__")).to eq(" insert doc/README\n") end + it "logs status if pretending" do + invoker(:pretend => true) + expect(invoke!("doc/README", "\nmore content", :after => "__start__")).to eq(" insert doc/README\n") + end + it "does not change the file if pretending" do invoker :pretend => true invoke! "doc/README", "\nmore content", :after => "__start__"