operable / cog-rb

Simple, opinionated library for building Cog bundles in Ruby
Other
6 stars 2 forks source link

Suggestions for improvement #20

Open vanstee opened 8 years ago

vanstee commented 8 years ago

Most of these improvements are to make testing simpler as most data can be more easily mocked out. Warning untested sudo-y code below, but notice there aren't any crazy mocks and stubs other than just a client being stubbed out.

class CogCmd::EC2::Find
  attr_reader :client

  def initialize(request, client = ::EC2::Client.new)
    super(request)
    @client = client
  end

  def run_command
    raise(Cog::Error, "Region option required") unless region
    render "find", client.instances(region)
  end

  def region
    options[:region]
  end
end

let(:client) { double(:client, instances: []) }

it "lists ec2 instances" do
  request = CogCmd::Request.new({
    "COG_OPT_REGION" => "us-east"
  })

  response = CogCmd::EC2.Find.new(request, client).run_command

  expect(response.body).to eq([])
end

it "fails if region is not specified" do
  request = CogCmd::Request.new({})

  expect { CogCmd::EC2.Find.new(request, client).run_command }.to raise(Cog:Error, "Region option required")
end
christophermaier commented 8 years ago

I wonder if commands should receive a list of args, a map of options, and the input... things we've already extracted from ENV and STDIN. That might also make testing easier, since you wouldn't need to manually reproduce all the various COG_X variables.

vanstee commented 8 years ago

Yeah, that's essentially what's happening since those are bundled up in a request object. I'd be ok with breaking those out though. I'm pretty much a fan of anything that helps us remove random access to ENV from inside an instance.