Skip to content

Commit 620b685

Browse files
committed
feat: don't support erroring as an option on original gsub_file
1 parent 489b4f1 commit 620b685

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

lib/thor/actions/file_manipulation.rb

+18-14
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,12 @@ def inject_into_module(path, module_name, *args, &block)
263263
def gsub_file!(path, flag, *args, &block)
264264
config = args.last.is_a?(Hash) ? args.pop : {}
265265

266-
config[:error_on_no_change] = true
266+
return unless behavior == :invoke || config.fetch(:force, false)
267+
268+
path = File.expand_path(path, destination_root)
269+
say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)
267270

268-
gsub_file(path, flag, *args, config, &block)
271+
actually_gsub_file(path, flag, args, true, &block) unless options[:pretend]
269272
end
270273

271274
# Run a regular expression replacement on a file.
@@ -274,8 +277,7 @@ def gsub_file!(path, flag, *args, &block)
274277
# path<String>:: path of the file to be changed
275278
# flag<Regexp|String>:: the regexp or string to be replaced
276279
# replacement<String>:: the replacement, can be also given as a block
277-
# config<Hash>:: give :verbose => false to not log the status,
278-
# :error_on_no_change => true to raise an error if the file does not change, and
280+
# config<Hash>:: give :verbose => false to not log the status, and
279281
# :force => true, to force the replacement regardless of runner behavior.
280282
#
281283
# ==== Example
@@ -294,16 +296,7 @@ def gsub_file(path, flag, *args, &block)
294296
path = File.expand_path(path, destination_root)
295297
say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)
296298

297-
unless options[:pretend]
298-
content = File.binread(path)
299-
success = content.gsub!(flag, *args, &block)
300-
301-
if success.nil? && config.fetch(:error_on_no_change, false)
302-
raise Thor::Error, "The content of #{path} did not change"
303-
end
304-
305-
File.open(path, "wb") { |file| file.write(content) }
306-
end
299+
actually_gsub_file(path, flag, args, false, &block) unless options[:pretend]
307300
end
308301

309302
# Uncomment all lines matching a given regex. Preserves indentation before
@@ -389,6 +382,17 @@ def with_output_buffer(buf = "".dup) #:nodoc:
389382
self.output_buffer = old_buffer
390383
end
391384

385+
def actually_gsub_file(path, flag, args, error_on_no_change, &block)
386+
content = File.binread(path)
387+
success = content.gsub!(flag, *args, &block)
388+
389+
if success.nil? && error_on_no_change
390+
raise Thor::Error, "The content of #{path} did not change"
391+
end
392+
393+
File.open(path, "wb") { |file| file.write(content) }
394+
end
395+
392396
# Thor::Actions#capture depends on what kind of buffer is used in ERB.
393397
# Thus CapturableERB fixes ERB to use String buffer.
394398
class CapturableERB < ERB

spec/actions/file_manipulation_spec.rb

-15
Original file line numberDiff line numberDiff line change
@@ -421,21 +421,6 @@ def file
421421
action :gsub_file, "doc/README", "___start___", "START"
422422
expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
423423
end
424-
425-
context "with error_on_no_change" do
426-
it "replaces the content in the file" do
427-
action :gsub_file, "doc/README", "__start__", "START", error_on_no_change: true
428-
expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
429-
end
430-
431-
it "raises if the file contents did not change" do
432-
expect do
433-
action :gsub_file, "doc/README", "___start___", "START", error_on_no_change: true
434-
end.to raise_error(Thor::Error, "The content of #{destination_root}/doc/README did not change")
435-
436-
expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
437-
end
438-
end
439424
end
440425

441426
context "with revoke behavior" do

0 commit comments

Comments
 (0)