Skip to content

Commit 92e0569

Browse files
authored
Merge pull request #505 from vcavallo/feature/dont-replace-nonexistant-files
Feature/dont replace nonexistant files
2 parents cb4a1d9 + 764bcda commit 92e0569

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/thor/actions/inject_into_file.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ def invoke!
5353
replacement + '\0'
5454
end
5555

56-
replace!(/#{flag}/, content, config[:force])
56+
if File.exist?(destination)
57+
replace!(/#{flag}/, content, config[:force])
58+
else
59+
raise Thor::Error, "The file #{ destination } does not appear to exist"
60+
end
5761
end
5862

5963
def revoke!

spec/actions/inject_into_file_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ def file
7171
expect(File.read(file)).to eq("__start__\nREADME\nmore content\n__end__\n")
7272
end
7373

74+
it "does not attempt to change the file if it doesn't exist - instead raises Thor::Error" do
75+
expect do
76+
invoke! "idontexist", :before => "something" do
77+
"any content"
78+
end
79+
end.to raise_error(Thor::Error, /does not appear to exist/)
80+
expect(File.exist?("idontexist")).to be_falsey
81+
end
82+
7483
it "does change the file if already includes content and :force is true" do
7584
invoke! "doc/README", :before => "__end__" do
7685
"more content\n"

0 commit comments

Comments
 (0)