Skip to content

Commit 5f2abca

Browse files
committed
Revert "Merge pull request #735 from excid3/improve-insert-into-file"
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.
1 parent 75beec8 commit 5f2abca

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

lib/thor/actions/inject_into_file.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ def say_status(behavior, warning: nil, color: nil)
107107
#
108108
def replace!(regexp, string, force)
109109
content = File.read(destination)
110-
before, after = content.split(regexp, 2)
111-
snippet = (behavior == :after ? after : before).to_s
112-
113-
if force || !snippet.include?(replacement)
110+
if force || !content.include?(replacement)
114111
success = content.gsub!(regexp, string)
115112

116113
File.open(destination, "wb") { |file| file.write(content) } unless pretend?

spec/actions/inject_into_file_spec.rb

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,11 @@ def file
3434
expect(File.read(file)).to eq("__start__\nmore content\nREADME\n__end__\n")
3535
end
3636

37-
it "ignores duplicates before the after flag" do
38-
invoke! "doc/README", "\nREADME", :after => "README"
39-
expect(File.read(file)).to eq("__start__\nREADME\nREADME\n__end__\n")
40-
end
41-
4237
it "changes the file adding content before the flag" do
4338
invoke! "doc/README", "more content\n", :before => "__end__"
4439
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
4540
end
4641

47-
it "ignores duplicates before the before flag" do
48-
invoke! "doc/README", "README\n", :before => "README"
49-
expect(File.read(file)).to eq("__start__\nREADME\nREADME\n__end__\n")
50-
end
51-
5242
it "appends content to the file if before and after arguments not provided" do
5343
invoke!("doc/README", "more content\n")
5444
expect(File.read(file)).to eq("__start__\nREADME\n__end__\nmore content\n")
@@ -97,6 +87,34 @@ def file
9787
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
9888
end
9989

90+
it "does not change the file if already includes content using before with capture" do
91+
invoke! "doc/README", :before => /(__end__)/ do
92+
"more content\n"
93+
end
94+
95+
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
96+
97+
invoke! "doc/README", :before => /(__end__)/ do
98+
"more content\n"
99+
end
100+
101+
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
102+
end
103+
104+
it "does not change the file if already includes content using after with capture" do
105+
invoke! "doc/README", :after => /(README\n)/ do
106+
"more content\n"
107+
end
108+
109+
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
110+
111+
invoke! "doc/README", :after => /(README\n)/ do
112+
"more content\n"
113+
end
114+
115+
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
116+
end
117+
100118
it "does not attempt to change the file if it doesn't exist - instead raises Thor::Error" do
101119
expect do
102120
invoke! "idontexist", :before => "something" do

0 commit comments

Comments
 (0)