File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 1
1
require "thor/actions/empty_directory"
2
+ require "pry"
2
3
3
4
class Thor
4
5
module Actions
@@ -56,7 +57,11 @@ def invoke!
56
57
replacement + '\0'
57
58
end
58
59
59
- replace! ( /#{ flag } / , content , config [ :force ] )
60
+ if File . exist? ( destination )
61
+ replace! ( /#{ flag } / , content , config [ :force ] )
62
+ else
63
+ raise MalformattedArgumentError , "The file #{ destination } does not appear to exist"
64
+ end
60
65
end
61
66
62
67
def revoke!
Original file line number Diff line number Diff line change @@ -70,6 +70,21 @@ def file
70
70
expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n __end__\n " )
71
71
end
72
72
73
+ it "does not attempt to change the file if it doesn't exist" do
74
+ expect_any_instance_of ( Thor ::Actions ::InjectIntoFile ) . not_to receive ( :replace! )
75
+ invoker . inject_into_file "idontexist" , :before => "something" do
76
+ "any content"
77
+ end rescue nil
78
+ end
79
+
80
+ it "raises a malformatted argument error including filename if file doesn't exist" do
81
+ expect do
82
+ invoker . inject_into_file "idontexist" , :before => "something" do
83
+ "any content"
84
+ end
85
+ end . to raise_error ( Thor ::MalformattedArgumentError , /does not appear to exist/ )
86
+ end
87
+
73
88
it "does change the file if already includes content and :force is true" do
74
89
invoke! "doc/README" , :before => "__end__" do
75
90
"more content\n "
You can’t perform that action at this time.
0 commit comments