@@ -316,6 +316,104 @@ def file
316316 end
317317 end
318318
319+ describe "#gsub_file!" do
320+ context "with invoke behavior" do
321+ it "replaces the content in the file" do
322+ action :gsub_file! , "doc/README" , "__start__" , "START"
323+ expect ( File . binread ( file ) ) . to eq ( "START\n README\n __end__\n " )
324+ end
325+
326+ it "does not replace if pretending" do
327+ runner ( pretend : true )
328+ action :gsub_file! , "doc/README" , "__start__" , "START"
329+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
330+ end
331+
332+ it "accepts a block" do
333+ action ( :gsub_file! , "doc/README" , "__start__" ) { |match | match . gsub ( "__" , "" ) . upcase }
334+ expect ( File . binread ( file ) ) . to eq ( "START\n README\n __end__\n " )
335+ end
336+
337+ it "logs status" do
338+ expect ( action ( :gsub_file! , "doc/README" , "__start__" , "START" ) ) . to eq ( " gsub doc/README\n " )
339+ end
340+
341+ it "does not log status if required" do
342+ expect ( action ( :gsub_file! , file , "__" , verbose : false ) { |match | match * 2 } ) . to be_empty
343+ end
344+
345+ it "cares if the file contents did not change" do
346+ expect do
347+ action :gsub_file! , "doc/README" , "___start___" , "START"
348+ end . to raise_error ( Thor ::Error , "The content of #{ destination_root } /doc/README did not change" )
349+
350+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
351+ end
352+ end
353+
354+ context "with revoke behavior" do
355+ context "and no force option" do
356+ it "does not replace the content in the file" do
357+ runner ( { } , :revoke )
358+ action :gsub_file! , "doc/README" , "__start__" , "START"
359+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
360+ end
361+
362+ it "does not replace if pretending" do
363+ runner ( { pretend : true } , :revoke )
364+ action :gsub_file! , "doc/README" , "__start__" , "START"
365+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
366+ end
367+
368+ it "does not replace the content in the file when given a block" do
369+ runner ( { } , :revoke )
370+ action ( :gsub_file! , "doc/README" , "__start__" ) { |match | match . gsub ( "__" , "" ) . upcase }
371+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
372+ end
373+
374+ it "does not log status" do
375+ runner ( { } , :revoke )
376+ expect ( action ( :gsub_file! , "doc/README" , "__start__" , "START" ) ) . to be_empty
377+ end
378+
379+ it "does not log status if required" do
380+ runner ( { } , :revoke )
381+ expect ( action ( :gsub_file! , file , "__" , verbose : false ) { |match | match * 2 } ) . to be_empty
382+ end
383+ end
384+
385+ context "and force option" do
386+ it "replaces the content in the file" do
387+ runner ( { } , :revoke )
388+ action :gsub_file! , "doc/README" , "__start__" , "START" , force : true
389+ expect ( File . binread ( file ) ) . to eq ( "START\n README\n __end__\n " )
390+ end
391+
392+ it "does not replace if pretending" do
393+ runner ( { pretend : true } , :revoke )
394+ action :gsub_file! , "doc/README" , "__start__" , "START" , force : true
395+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
396+ end
397+
398+ it "replaces the content in the file when given a block" do
399+ runner ( { } , :revoke )
400+ action ( :gsub_file! , "doc/README" , "__start__" , force : true ) { |match | match . gsub ( "__" , "" ) . upcase }
401+ expect ( File . binread ( file ) ) . to eq ( "START\n README\n __end__\n " )
402+ end
403+
404+ it "logs status" do
405+ runner ( { } , :revoke )
406+ expect ( action ( :gsub_file! , "doc/README" , "__start__" , "START" , force : true ) ) . to eq ( " gsub doc/README\n " )
407+ end
408+
409+ it "does not log status if required" do
410+ runner ( { } , :revoke )
411+ expect ( action ( :gsub_file! , file , "__" , verbose : false , force : true ) { |match | match * 2 } ) . to be_empty
412+ end
413+ end
414+ end
415+ end
416+
319417 describe "#gsub_file" do
320418 context "with invoke behavior" do
321419 it "replaces the content in the file" do
@@ -341,6 +439,11 @@ def file
341439 it "does not log status if required" do
342440 expect ( action ( :gsub_file , file , "__" , verbose : false ) { |match | match * 2 } ) . to be_empty
343441 end
442+
443+ it "does not care if the file contents did not change" do
444+ action :gsub_file , "doc/README" , "___start___" , "START"
445+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
446+ end
344447 end
345448
346449 context "with revoke behavior" do
0 commit comments