Thanks for the info Aslak, I have rewritten my code like you suggest
but have one issue with raising an exception. When I try the code in
the console an exception is raised correctly but in my test code I get
a message "command did not return expected result"

# test
describe Ssh, "Remote" do

  before(:each) do
    @ssh = mock(Net::SSH)
    @response = mock(Net::SSH)
    @connection = Ssh::Remote.new
    @connection.should_receive(:start).with('server', 'root').and_return(@ssh)
    @connection.should_receive(:close)
    @ssh.should_receive(:send_command).with('ls /').and_return(@response)
    @response.should_receive(:stdout).and_return('a list')
  end

  it "should raise exception if response does not match expected" do
    @connection.remote_command('server', 'root', [{:command => 'ls /',
:expects => /blah/}]).
      should raise_error(Ssh::CommandError, 'command did not return
expected result')
  end

end

# code exception.rb
module Ssh
  class CommandError < Exception
  end
end

# ssh.rb
require 'net/ssh'
require 'ssh/exception'

module Ssh
  class Remote

    def remote_command(server, user, commands=[])
      shell = start(server, user)
      commands.each do |command|
        out = shell.send_command command[:command]
        unless out.stdout =~ command[:expects] or command[:expects].blank?
          close
          raise(CommandError.new, 'command did not return expected result')
        end
      end
      close
      return true
    end

    def start(server, user)
      @session = Net::SSH.start(server, user)
      @session.shell.sync
    end

    def close()
      @session.close
    end

  end
end

Thanks
Jamie
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to