@@ -118,11 +118,11 @@ def when_no_thorfiles_exist
118118 end
119119
120120 describe "commands" do
121+ let ( :location ) { "#{ File . dirname ( __FILE__ ) } /fixtures/command.thor" }
121122 before do
122- @location = "#{ File . dirname ( __FILE__ ) } /fixtures/command.thor"
123123 @original_yaml = {
124124 "random" => {
125- :location => @ location,
125+ :location => location ,
126126 :filename => "4a33b894ffce85d7b412fc1b36f88fe0" ,
127127 :namespaces => %w( amazing )
128128 }
@@ -214,31 +214,53 @@ def when_no_thorfiles_exist
214214 end
215215
216216 describe "install/update" do
217- before do
218- allow ( FileUtils ) . to receive ( :mkdir_p )
219- allow ( FileUtils ) . to receive ( :touch )
220- allow ( Thor ::LineEditor ) . to receive ( :readline ) . and_return ( "Y" )
217+ context "with local thor files" do
218+ before do
219+ allow ( FileUtils ) . to receive ( :mkdir_p )
220+ allow ( FileUtils ) . to receive ( :touch )
221+ allow ( Thor ::LineEditor ) . to receive ( :readline ) . and_return ( "Y" )
222+
223+ path = File . join ( Thor ::Util . thor_root , Digest ::MD5 . hexdigest ( location + "random" ) )
224+ expect ( File ) . to receive ( :open ) . with ( path , "w" )
225+ end
221226
222- path = File . join ( Thor ::Util . thor_root , Digest ::MD5 . hexdigest ( @location + "random" ) )
223- expect ( File ) . to receive ( :open ) . with ( path , "w" )
224- end
225227
226- it "updates existing thor files" do
227- path = File . join ( Thor ::Util . thor_root , @original_yaml [ "random" ] [ :filename ] )
228- if File . directory? path
229- expect ( FileUtils ) . to receive ( :rm_rf ) . with ( path )
230- else
231- expect ( File ) . to receive ( :delete ) . with ( path )
228+ it "updates existing thor files" do
229+ path = File . join ( Thor ::Util . thor_root , @original_yaml [ "random" ] [ :filename ] )
230+ if File . directory? path
231+ expect ( FileUtils ) . to receive ( :rm_rf ) . with ( path )
232+ else
233+ expect ( File ) . to receive ( :delete ) . with ( path )
234+ end
235+ silence_warnings do
236+ silence ( :stdout ) { Thor ::Runner . start ( %w( update random ) ) }
237+ end
232238 end
233- silence_warnings do
234- silence ( :stdout ) { Thor ::Runner . start ( %w( update random ) ) }
239+
240+ it "installs thor files" do
241+ ARGV . replace %W( install #{ location } )
242+ silence_warnings do
243+ silence ( :stdout ) { Thor ::Runner . start }
244+ end
235245 end
236246 end
237247
238- it "installs thor files" do
239- ARGV . replace %W( install #{ @location } )
240- silence_warnings do
241- silence ( :stdout ) { Thor ::Runner . start }
248+ context "with remote thor files" do
249+ let ( :location ) { "https://example.com/Thorfile" }
250+
251+ it "installs thor files" do
252+ allow ( Thor ::LineEditor ) . to receive ( :readline ) . and_return ( "Y" , "random" )
253+ stub_request ( :get , location ) . to_return ( :body => "class Foo < Thor; end" )
254+ path = File . join ( Thor ::Util . thor_root , Digest ::MD5 . hexdigest ( location + "random" ) )
255+ expect ( File ) . to receive ( :open ) . with ( path , "w" )
256+ expect { silence ( :stdout ) { Thor ::Runner . start ( %W( install #{ location } ) ) } } . not_to raise_error
257+ end
258+
259+ it "shows proper errors" do
260+ expect ( Thor ::Runner ) . to receive :exit
261+ expect ( URI ) . to receive ( :open ) . with ( location ) . and_raise ( OpenURI ::HTTPError . new ( "foo" , StringIO . new ) )
262+ content = capture ( :stderr ) { Thor ::Runner . start ( %W( install #{ location } ) ) }
263+ expect ( content ) . to include ( "Error opening URI '#{ location } '" )
242264 end
243265 end
244266 end
0 commit comments