Skip to content

Commit e3fce16

Browse files
authored
Merge pull request #574 from yhirano55/fix_pretend_handling
Fix bug and refactoring
2 parents df5ba2b + dc8b751 commit e3fce16

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lib/thor/actions/inject_into_file.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ def invoke!
5353
replacement + '\0'
5454
end
5555

56-
if File.exist?(destination)
56+
if exists?
5757
replace!(/#{flag}/, content, config[:force])
5858
else
59-
raise Thor::Error, "The file #{ destination } does not appear to exist"
59+
unless pretend?
60+
raise Thor::Error, "The file #{ destination } does not appear to exist"
61+
end
6062
end
6163
end
6264

@@ -95,7 +97,7 @@ def say_status(behavior)
9597
# Adds the content to the file.
9698
#
9799
def replace!(regexp, string, force)
98-
return if base.options[:pretend]
100+
return if pretend?
99101
content = File.read(destination)
100102
if force || !content.include?(replacement)
101103
content.gsub!(regexp, string)

spec/actions/inject_into_file_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,23 @@ def file
7373

7474
it "does not attempt to change the file if it doesn't exist - instead raises Thor::Error" do
7575
expect do
76-
invoke! "idontexist", :before => "something" do
77-
"any content"
78-
end
76+
invoke! "idontexist", :before => "something" do
77+
"any content"
78+
end
7979
end.to raise_error(Thor::Error, /does not appear to exist/)
8080
expect(File.exist?("idontexist")).to be_falsey
8181
end
8282

83+
it "does not attempt to change the file if it doesn't exist and pretending" do
84+
expect do
85+
invoker :pretend => true
86+
invoke! "idontexist", :before => "something" do
87+
"any content"
88+
end
89+
end.not_to raise_error
90+
expect(File.exist?("idontexist")).to be_falsey
91+
end
92+
8393
it "does change the file if already includes content and :force is true" do
8494
invoke! "doc/README", :before => "__end__" do
8595
"more content\n"

0 commit comments

Comments
 (0)