spec/hobo/error_handlers/friendly_spec.rb in hobo-inviqa-0.0.7 vs spec/hobo/error_handlers/friendly_spec.rb in hobo-inviqa-0.0.8

- old
+ new

@@ -1,7 +1,6 @@ -require 'hobo/error_handlers/friendly' -require 'hobo/ui' +require 'spec_helper' describe Hobo::ErrorHandlers::Friendly do before do Hobo.ui = double(Hobo::Ui.new).as_null_object FakeFS.activate! @@ -11,71 +10,89 @@ after do FakeFS::FileSystem.clear FakeFS.deactivate! end + def faked_exception(error_template) + error = nil + begin + raise error_template + rescue Exception => error + end + + return error + end + describe "handle" do it "should display specialized error for Interrupt" do - error = Interrupt.new + error = faked_exception(Interrupt.new) Hobo.ui.should_receive(:warning).with(/Caught Interrupt/) Hobo::ErrorHandlers::Friendly.new.handle(error) end it "should display specialized error for an external command error" do File.write("temp_log", "command output") output = Struct.new(:path).new output.path = "temp_log" - error = Hobo::ExternalCommandError.new("command", 128, output) + + error = faked_exception Hobo::ExternalCommandError.new("command", 128, output) + Hobo.ui.should_receive(:error).with(/The following external command appears to have failed \(exit status 128\)/) Hobo::ErrorHandlers::Friendly.new.handle(error) end it "should write command output to /tmp/hobo_error.log for external command error" do File.write("temp_log", "command output") output = Struct.new(:path).new output.path = "temp_log" - error = Hobo::ExternalCommandError.new("command", 128, output) + + error = faked_exception Hobo::ExternalCommandError.new("command", 128, output) + Hobo::ErrorHandlers::Friendly.new.handle(error) File.read(File.join(Dir.tmpdir, 'hobo_error.log')).should match "command output" end it "should display specialized error for invalid command or opt error" do - error = Hobo::InvalidCommandOrOpt.new("command") + error = faked_exception Hobo::InvalidCommandOrOpt.new("command") Hobo.ui.should_receive(:error).with(/Invalid command or option specified: 'command'/) Hobo::ErrorHandlers::Friendly.new.handle(error) end it "should display specialized error for missing argument error" do - error = Hobo::MissingArgumentsError.new("command", ["arg1"]) + error = faked_exception Hobo::MissingArgumentsError.new("command", ["arg1"]) Hobo.ui.should_receive(:error).with(/Not enough arguments for command/) Hobo::ErrorHandlers::Friendly.new.handle(error) end it "should display specialized error for user error" do - error = Hobo::UserError.new("user error") + error = faked_exception Hobo::UserError.new("user error") Hobo.ui.should_receive(:error).with(/user error/) Hobo::ErrorHandlers::Friendly.new.handle(error) end it "should display generic error for other exception" do - error = nil - begin - raise Exception.new("general error") - rescue Exception => error - end - + error = faked_exception Exception.new("general error") Hobo.ui.should_receive(:error).with(/An unexpected error has occured/) Hobo::ErrorHandlers::Friendly.new.handle(error) end it "should write error backtrace to /tmp/hobo_error.log for other exception" do - error = nil - begin - raise Exception.new("general error") - rescue Exception => error - end - + error = faked_exception Exception.new("general error") Hobo::ErrorHandlers::Friendly.new.handle(error) File.read(File.join(Dir.tmpdir, 'hobo_error.log')).should match /\(Exception\) general error/ end + + it "should return exit code according to exit_code_map" do + File.write("temp_log", "command output") + output = Struct.new(:path).new + output.path = "temp_log" + + Hobo::ErrorHandlers::Friendly.new.handle(faked_exception Interrupt.new).should eq 1 + Hobo::ErrorHandlers::Friendly.new.handle(faked_exception Hobo::ExternalCommandError.new("command", 128, output)).should eq 3 + Hobo::ErrorHandlers::Friendly.new.handle(faked_exception Hobo::InvalidCommandOrOpt.new("command")).should eq 4 + Hobo::ErrorHandlers::Friendly.new.handle(faked_exception Hobo::MissingArgumentsError.new("command", ["arg1"])).should eq 5 + Hobo::ErrorHandlers::Friendly.new.handle(faked_exception Hobo::UserError.new("user error")).should eq 6 + Hobo::ErrorHandlers::Friendly.new.handle(faked_exception Hobo::ProjectOnlyError.new).should eq 7 + Hobo::ErrorHandlers::Friendly.new.handle(faked_exception Exception.new "general").should eq 128 + end end -end \ No newline at end of file +end