Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/utils/diff_utils/simple_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def apply(content, search, replace)
raise ArgumentError, "content cannot be nil" if content.nil?
raise ArgumentError, "search cannot be nil" if search.nil?
raise ArgumentError, "replace cannot be nil" if replace.nil?
raise ArgumentError, "search cannot be empty" if search.empty?

return content.gsub(search, replace) if content.include?(search)

lines = content.split("\n")
search_lines = search.split("\n")
Expand Down
13 changes: 10 additions & 3 deletions spec/lib/utils/diff_utils/simple_diff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
lin 1
TEXT

expect(subject.apply(content, search, replace)).to eq(expected.strip)
expect(subject.apply(content, search, replace).strip).to eq(expected.strip)
end

it "raises error when no match is found" do
Expand Down Expand Up @@ -61,10 +61,10 @@
end

it "is forgiving of whitespace differences" do
content = "line1\n line2\nline3"
content = "line1\n line2\nline3"
search = "line2"
replace = "new_line2"
expect(subject.apply(content, search, replace)).to eq("line1\nnew_line2\nline3")
expect(subject.apply(content, search, replace).strip).to eq("line1\n new_line2\nline3")
end

it "is forgiving of small character differences" do
Expand Down Expand Up @@ -121,6 +121,13 @@
expect(subject.apply(content, search, replace)).to eq(expected.strip)
end

it "handles partial line matches" do
content = "abc hello efg\nabc hello efg"
search = "hello"
replace = "bob"
expect(subject.apply(content, search, replace)).to eq("abc bob efg\nabc bob efg")
end

it "handles JavaScript blocks in different orders" do
content = <<~JS
function first() {
Expand Down