Skip to content

Commit

Permalink
Revert "Merge pull request #735 from excid3/improve-insert-into-file"
Browse files Browse the repository at this point in the history
This reverts commit 0d3a1dd, reversing
changes made to ae18824.

This breaks case when the regular expression passed to after and before
contains capture.

Since `String#split` returns the captures inside the array we can't know
with the current implementation what is the before and after the regular
expression.
  • Loading branch information
rafaelfranca committed Jan 4, 2022
1 parent 75beec8 commit 5f2abca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
5 changes: 1 addition & 4 deletions lib/thor/actions/inject_into_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ def say_status(behavior, warning: nil, color: nil)
#
def replace!(regexp, string, force)
content = File.read(destination)
before, after = content.split(regexp, 2)
snippet = (behavior == :after ? after : before).to_s

if force || !snippet.include?(replacement)
if force || !content.include?(replacement)
success = content.gsub!(regexp, string)

File.open(destination, "wb") { |file| file.write(content) } unless pretend?
Expand Down
38 changes: 28 additions & 10 deletions spec/actions/inject_into_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,11 @@ def file
expect(File.read(file)).to eq("__start__\nmore content\nREADME\n__end__\n")
end

it "ignores duplicates before the after flag" do
invoke! "doc/README", "\nREADME", :after => "README"
expect(File.read(file)).to eq("__start__\nREADME\nREADME\n__end__\n")
end

it "changes the file adding content before the flag" do
invoke! "doc/README", "more content\n", :before => "__end__"
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
end

it "ignores duplicates before the before flag" do
invoke! "doc/README", "README\n", :before => "README"
expect(File.read(file)).to eq("__start__\nREADME\nREADME\n__end__\n")
end

it "appends content to the file if before and after arguments not provided" do
invoke!("doc/README", "more content\n")
expect(File.read(file)).to eq("__start__\nREADME\n__end__\nmore content\n")
Expand Down Expand Up @@ -97,6 +87,34 @@ def file
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
end

it "does not change the file if already includes content using before with capture" do
invoke! "doc/README", :before => /(__end__)/ do
"more content\n"
end

expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")

invoke! "doc/README", :before => /(__end__)/ do
"more content\n"
end

expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
end

it "does not change the file if already includes content using after with capture" do
invoke! "doc/README", :after => /(README\n)/ do
"more content\n"
end

expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")

invoke! "doc/README", :after => /(README\n)/ do
"more content\n"
end

expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
end

it "does not attempt to change the file if it doesn't exist - instead raises Thor::Error" do
expect do
invoke! "idontexist", :before => "something" do
Expand Down

0 comments on commit 5f2abca

Please sign in to comment.