Skip to content

Commit

Permalink
feat: don't support erroring as an option on original gsub_file
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Feb 29, 2024
1 parent 88446b9 commit 8eae746
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
32 changes: 18 additions & 14 deletions lib/thor/actions/file_manipulation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,12 @@ def inject_into_module(path, module_name, *args, &block)
def gsub_file!(path, flag, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}

config[:error_on_no_change] = true
return unless behavior == :invoke || config.fetch(:force, false)

path = File.expand_path(path, destination_root)
say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)

gsub_file(path, flag, *args, config, &block)
actually_gsub_file(path, flag, args, true, &block) unless options[:pretend]
end

# Run a regular expression replacement on a file.
Expand All @@ -275,8 +278,7 @@ def gsub_file!(path, flag, *args, &block)
# path<String>:: path of the file to be changed
# flag<Regexp|String>:: the regexp or string to be replaced
# replacement<String>:: the replacement, can be also given as a block
# config<Hash>:: give :verbose => false to not log the status,
# :error_on_no_change => true to raise an error if the file does not change, and
# config<Hash>:: give :verbose => false to not log the status, and
# :force => true, to force the replacement regardless of runner behavior.
#
# ==== Example
Expand All @@ -295,16 +297,7 @@ def gsub_file(path, flag, *args, &block)
path = File.expand_path(path, destination_root)
say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)

unless options[:pretend]
content = File.binread(path)
success = content.gsub!(flag, *args, &block)

if success.nil? && config.fetch(:error_on_no_change, false)
raise Thor::Error, "The content of #{path} did not change"
end

File.open(path, "wb") { |file| file.write(content) }
end
actually_gsub_file(path, flag, args, false, &block) unless options[:pretend]
end

# Uncomment all lines matching a given regex. It will leave the space
Expand Down Expand Up @@ -391,6 +384,17 @@ def with_output_buffer(buf = "".dup) #:nodoc:
self.output_buffer = old_buffer
end

def actually_gsub_file(path, flag, args, error_on_no_change, &block)
content = File.binread(path)
success = content.gsub!(flag, *args, &block)

if success.nil? && error_on_no_change
raise Thor::Error, "The content of #{path} did not change"
end

File.open(path, "wb") { |file| file.write(content) }
end

# Thor::Actions#capture depends on what kind of buffer is used in ERB.
# Thus CapturableERB fixes ERB to use String buffer.
class CapturableERB < ERB
Expand Down
15 changes: 0 additions & 15 deletions spec/actions/file_manipulation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -421,21 +421,6 @@ def file
action :gsub_file, "doc/README", "___start___", "START"
expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
end

context "with error_on_no_change" do
it "replaces the content in the file" do
action :gsub_file, "doc/README", "__start__", "START", error_on_no_change: true
expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
end

it "raises if the file contents did not change" do
expect do
action :gsub_file, "doc/README", "___start___", "START", error_on_no_change: true
end.to raise_error(Thor::Error, "The content of #{destination_root}/doc/README did not change")

expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
end
end
end

context "with revoke behavior" do
Expand Down

0 comments on commit 8eae746

Please sign in to comment.