Skip to content

Commit b698451

Browse files
authored
Merge pull request #745 from AMHOL/feature/add-force-option-to-gsub_file-action
Add force option to the gsub_file action
2 parents e94bfe9 + 3fcdeb4 commit b698451

File tree

2 files changed

+92
-26
lines changed

2 files changed

+92
-26
lines changed

lib/thor/actions/file_manipulation.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ def inject_into_module(path, module_name, *args, &block)
251251
# path<String>:: path of the file to be changed
252252
# flag<Regexp|String>:: the regexp or string to be replaced
253253
# replacement<String>:: the replacement, can be also given as a block
254-
# config<Hash>:: give :verbose => false to not log the status.
254+
# config<Hash>:: give :verbose => false to not log the status, and
255+
# :force => true, to force the replacement regardles of runner behavior.
255256
#
256257
# ==== Example
257258
#
@@ -262,9 +263,10 @@ def inject_into_module(path, module_name, *args, &block)
262263
# end
263264
#
264265
def gsub_file(path, flag, *args, &block)
265-
return unless behavior == :invoke
266266
config = args.last.is_a?(Hash) ? args.pop : {}
267267

268+
return unless behavior == :invoke || config.fetch(:force, false)
269+
268270
path = File.expand_path(path, destination_root)
269271
say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)
270272

spec/actions/file_manipulation_spec.rb

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class Application; end
44
module ApplicationHelper; end
55

66
describe Thor::Actions do
7-
def runner(options = {})
8-
@runner ||= MyCounter.new([1], options, :destination_root => destination_root)
7+
def runner(options = {}, behavior = :invoke)
8+
@runner ||= MyCounter.new([1], options, :destination_root => destination_root, :behavior => behavior)
99
end
1010

1111
def action(*args, &block)
@@ -280,28 +280,92 @@ def file
280280
end
281281

282282
describe "#gsub_file" do
283-
it "replaces the content in the file" do
284-
action :gsub_file, "doc/README", "__start__", "START"
285-
expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
286-
end
287-
288-
it "does not replace if pretending" do
289-
runner(:pretend => true)
290-
action :gsub_file, "doc/README", "__start__", "START"
291-
expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
292-
end
293-
294-
it "accepts a block" do
295-
action(:gsub_file, "doc/README", "__start__") { |match| match.gsub("__", "").upcase }
296-
expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
297-
end
298-
299-
it "logs status" do
300-
expect(action(:gsub_file, "doc/README", "__start__", "START")).to eq(" gsub doc/README\n")
301-
end
302-
303-
it "does not log status if required" do
304-
expect(action(:gsub_file, file, "__", :verbose => false) { |match| match * 2 }).to be_empty
283+
context "with invoke behavior" do
284+
it "replaces the content in the file" do
285+
action :gsub_file, "doc/README", "__start__", "START"
286+
expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
287+
end
288+
289+
it "does not replace if pretending" do
290+
runner(:pretend => true)
291+
action :gsub_file, "doc/README", "__start__", "START"
292+
expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
293+
end
294+
295+
it "accepts a block" do
296+
action(:gsub_file, "doc/README", "__start__") { |match| match.gsub("__", "").upcase }
297+
expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
298+
end
299+
300+
it "logs status" do
301+
expect(action(:gsub_file, "doc/README", "__start__", "START")).to eq(" gsub doc/README\n")
302+
end
303+
304+
it "does not log status if required" do
305+
expect(action(:gsub_file, file, "__", :verbose => false) { |match| match * 2 }).to be_empty
306+
end
307+
end
308+
309+
context "with revoke behavior" do
310+
context "and no force option" do
311+
it "does not replace the content in the file" do
312+
runner({}, :revoke)
313+
action :gsub_file, "doc/README", "__start__", "START"
314+
expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
315+
end
316+
317+
it "does not replace if pretending" do
318+
runner({ :pretend => true }, :revoke)
319+
action :gsub_file, "doc/README", "__start__", "START"
320+
expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
321+
end
322+
323+
it "does not replace the content in the file when given a block" do
324+
runner({}, :revoke)
325+
action(:gsub_file, "doc/README", "__start__") { |match| match.gsub("__", "").upcase }
326+
expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
327+
end
328+
329+
it "does not log status" do
330+
runner({}, :revoke)
331+
expect(action(:gsub_file, "doc/README", "__start__", "START")).to be_empty
332+
end
333+
334+
it "does not log status if required" do
335+
runner({}, :revoke)
336+
expect(action(:gsub_file, file, "__", :verbose => false) { |match| match * 2 }).to be_empty
337+
end
338+
end
339+
340+
context "and force option" do
341+
it "replaces the content in the file" do
342+
runner({}, :revoke)
343+
action :gsub_file, "doc/README", "__start__", "START", :force => true
344+
expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
345+
end
346+
347+
it "does not replace if pretending" do
348+
runner({ :pretend => true }, :revoke)
349+
action :gsub_file, "doc/README", "__start__", "START", :force => true
350+
expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
351+
end
352+
353+
it "replaces the content in the file when given a block" do
354+
runner({}, :revoke)
355+
action(:gsub_file, "doc/README", "__start__", :force => true) { |match| match.gsub("__", "").upcase }
356+
expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
357+
end
358+
359+
it "logs status" do
360+
runner({}, :revoke)
361+
expect(action(:gsub_file, "doc/README", "__start__", "START", :force => true)).to eq(" gsub doc/README\n")
362+
end
363+
364+
it "does not log status if required" do
365+
runner({}, :revoke)
366+
expect(action(:gsub_file, file, "__", :verbose => false, :force => true) { |match| match * 2 }).to be_empty
367+
end
368+
end
305369
end
306370
end
307371

0 commit comments

Comments
 (0)