@@ -293,6 +293,104 @@ def file
293293 end
294294 end
295295
296+ describe "#gsub_file!" do
297+ context "with invoke behavior" do
298+ it "replaces the content in the file" do
299+ action :gsub_file! , "doc/README" , "__start__" , "START"
300+ expect ( File . binread ( file ) ) . to eq ( "START\n README\n __end__\n " )
301+ end
302+
303+ it "does not replace if pretending" do
304+ runner ( pretend : true )
305+ action :gsub_file! , "doc/README" , "__start__" , "START"
306+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
307+ end
308+
309+ it "accepts a block" do
310+ action ( :gsub_file! , "doc/README" , "__start__" ) { |match | match . gsub ( "__" , "" ) . upcase }
311+ expect ( File . binread ( file ) ) . to eq ( "START\n README\n __end__\n " )
312+ end
313+
314+ it "logs status" do
315+ expect ( action ( :gsub_file! , "doc/README" , "__start__" , "START" ) ) . to eq ( " gsub doc/README\n " )
316+ end
317+
318+ it "does not log status if required" do
319+ expect ( action ( :gsub_file! , file , "__" , verbose : false ) { |match | match * 2 } ) . to be_empty
320+ end
321+
322+ it "cares if the file contents did not change" do
323+ expect do
324+ action :gsub_file! , "doc/README" , "___start___" , "START"
325+ end . to raise_error ( Thor ::Error )
326+
327+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
328+ end
329+ end
330+
331+ context "with revoke behavior" do
332+ context "and no force option" do
333+ it "does not replace the content in the file" do
334+ runner ( { } , :revoke )
335+ action :gsub_file! , "doc/README" , "__start__" , "START"
336+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
337+ end
338+
339+ it "does not replace if pretending" do
340+ runner ( { pretend : true } , :revoke )
341+ action :gsub_file! , "doc/README" , "__start__" , "START"
342+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
343+ end
344+
345+ it "does not replace the content in the file when given a block" do
346+ runner ( { } , :revoke )
347+ action ( :gsub_file! , "doc/README" , "__start__" ) { |match | match . gsub ( "__" , "" ) . upcase }
348+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
349+ end
350+
351+ it "does not log status" do
352+ runner ( { } , :revoke )
353+ expect ( action ( :gsub_file! , "doc/README" , "__start__" , "START" ) ) . to be_empty
354+ end
355+
356+ it "does not log status if required" do
357+ runner ( { } , :revoke )
358+ expect ( action ( :gsub_file! , file , "__" , verbose : false ) { |match | match * 2 } ) . to be_empty
359+ end
360+ end
361+
362+ context "and force option" do
363+ it "replaces the content in the file" do
364+ runner ( { } , :revoke )
365+ action :gsub_file! , "doc/README" , "__start__" , "START" , force : true
366+ expect ( File . binread ( file ) ) . to eq ( "START\n README\n __end__\n " )
367+ end
368+
369+ it "does not replace if pretending" do
370+ runner ( { pretend : true } , :revoke )
371+ action :gsub_file! , "doc/README" , "__start__" , "START" , force : true
372+ expect ( File . binread ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
373+ end
374+
375+ it "replaces the content in the file when given a block" do
376+ runner ( { } , :revoke )
377+ action ( :gsub_file! , "doc/README" , "__start__" , force : true ) { |match | match . gsub ( "__" , "" ) . upcase }
378+ expect ( File . binread ( file ) ) . to eq ( "START\n README\n __end__\n " )
379+ end
380+
381+ it "logs status" do
382+ runner ( { } , :revoke )
383+ expect ( action ( :gsub_file! , "doc/README" , "__start__" , "START" , force : true ) ) . to eq ( " gsub doc/README\n " )
384+ end
385+
386+ it "does not log status if required" do
387+ runner ( { } , :revoke )
388+ expect ( action ( :gsub_file! , file , "__" , verbose : false , force : true ) { |match | match * 2 } ) . to be_empty
389+ end
390+ end
391+ end
392+ end
393+
296394 describe "#gsub_file" do
297395 context "with invoke behavior" do
298396 it "replaces the content in the file" do
0 commit comments