Skip to content

Commit

Permalink
Merge pull request #708 from scambra/patch-1
Browse files Browse the repository at this point in the history
Fix for #707
  • Loading branch information
rafaelfranca authored Jun 8, 2021
2 parents dcc2bf3 + baf7e46 commit efe79b5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/thor/actions/inject_into_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,14 @@ 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

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
Expand Down
5 changes: 5 additions & 0 deletions spec/actions/inject_into_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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__"
Expand Down

0 comments on commit efe79b5

Please sign in to comment.