Move configuration handling code out into its own class to clean up the Command logic

Taking some of my own advice regarding good RSpec practice too, lazy developer is lazy
This commit is contained in:
R. Tyler Croy 2012-12-17 22:29:43 -08:00
parent 85bfa1e64d
commit 00352ab19b
4 changed files with 140 additions and 57 deletions

View File

@ -3,7 +3,8 @@ require 'heroku/command/base'
require 'httparty'
require 'json'
#require 'sauce'
require 'yaml'
require 'sauce/heroku/config'
module Heroku
module Command
@ -96,21 +97,6 @@ access_key: #{apikey}
private
def configured?
return @config if @config
if File.exists?('ondemand.yml')
@config = YAML.load_file('ondemand.yml')
return @config
end
if File.exists?(File.expand_path('~/.sauce/ondemand.yml'))
@config = YAML.load_file(File.expand_path('~/.sauce/ondemand.yml'))
return @config
end
return false
end
def scoutup!
unless configured?

View File

@ -0,0 +1,37 @@
require 'yaml'
module Sauce
module Heroku
class Config
CONFIG_FILE = 'ondemand.yml'
attr_accessor :config
def filepath
[
File.join(Dir.pwd, CONFIG_FILE),
File.expand_path("~/.sauce/#{CONFIG_FILE}")
].each do |path|
if File.exists?(path)
return path
end
end
return nil
end
def load!
return config unless config.nil?
if filepath.nil?
return nil
end
config = YAML.load_file(filepath)
end
def configured?
return !(config.nil?)
end
end
end
end

100
spec/config_spec.rb Normal file
View File

@ -0,0 +1,100 @@
require 'spec_helper'
describe Sauce::Heroku::Config do
let(:config) { described_class.new }
describe '#config' do
subject { config.config }
context 'by default' do
it { should be_nil }
end
end
describe '#configured?' do
subject { config.configured? }
context 'by default' do
it { should_not be_true }
end
context 'when a config has been loaded' do
before :each do
config.stub(:config => {})
end
it { should be_true }
end
end
describe '#load!' do
subject { config.load! }
context 'when no config files are available' do
before :each do
config.should_receive(:filepath)
end
it { should be_nil }
end
context 'when a config file is available' do
let(:fakepath) { '//fake/path/ondemand.yml' }
let(:contents) { {'config' => true} }
before :each do
config.stub(:filepath => fakepath)
YAML.should_receive(:load_file).with(fakepath).and_return(contents)
end
it { should eql(contents) }
end
context 'when a config has already been loaded' do
let(:contents) { {'config' => true} }
before :each do
config.stub(:config => contents)
config.should_receive(:filepath).never
YAML.should_receive(:load_file).never
end
it { should eql(contents) }
end
end
describe '#filepath' do
subject { config.filepath }
context 'when no config files exist' do
before :each do
File.stub(:exists?).and_return(false)
end
it { should be_nil }
end
context 'when a local config file exists' do
let(:expected_path) { File.join(Dir.pwd, Sauce::Heroku::Config::CONFIG_FILE) }
before :each do
File.should_receive(:exists?).with(expected_path).and_return(true)
end
it { should eql(expected_path) }
end
context 'when a user config file exists' do
let(:expected_path) do
File.expand_path("~/.sauce/#{Sauce::Heroku::Config::CONFIG_FILE}")
end
before :each do
local_path = File.join(Dir.pwd, Sauce::Heroku::Config::CONFIG_FILE)
File.should_receive(:exists?).with(local_path).and_return(false)
File.should_receive(:exists?).with(expected_path).and_return(true)
end
it { should eql(expected_path) }
end
end
end

View File

@ -4,47 +4,6 @@ describe Heroku::Command::Sauce do
let(:command) { subject }
it { should be_instance_of Heroku::Command::Sauce }
describe '#configured?' do
let(:exists) { false }
context 'by default' do
before :each do
File.stub(:exists?).with(
'ondemand.yml').and_return(false)
File.stub(:exists?).with(
File.expand_path('~/.sauce/ondemand.yml')).and_return(false)
end
it 'should return false' do
command.send(:configured?).should_not be_true
end
end
context 'configured with a local ondemand.yml' do
let(:config) do
{
'username' => 'niceguy',
'access_key' => 'sekret'
}
end
before :each do
File.should_receive(:exists?).with('ondemand.yml').and_return(true)
YAML.should_receive(:load_file).with('ondemand.yml').and_return(config)
end
it { command.should be_configured }
it 'should return the configuration' do
result = command.send(:configured?)
result.should be_instance_of(Hash)
expect(result['username']).to eql(config['username'])
expect(result['access_key']).to eql(config['access_key'])
end
end
end
describe '#scoutup!' do
context 'when configured' do
let(:scouturl) { 'http://fakeurl' }
@ -53,6 +12,7 @@ describe Heroku::Command::Sauce do
end
it 'should post to Sauce Labs' do
pending
HTTParty.should_receive(:post).with(scouturl,
hash_including(:headers => {'Content-Type' => 'application/json'}))
command.send(:scoutup!)