Permalink
Browse files

Specs.

Use Bundler.
  • Loading branch information...
1 parent 3d220c6 commit c76e53f80e2fd9d11bc8bd2501d17d603c61bf19 @justinko justinko committed Oct 3, 2010
View
3 .gitignore
@@ -1,4 +1,5 @@
.bundle
.DS_Store
.rvmrc
-*.gem
+*.gem
+tmp
View
2 Gemfile.lock
@@ -41,7 +41,7 @@ PLATFORMS
DEPENDENCIES
archive-tar-minitar (~> 0.5.2)
- aruba (~> 0.2.1)
+ aruba (~> 0.2.2)
bundler (~> 1.0.0)
cucumber (~> 0.8.5)
rake (~> 0.8.7)
View
7 bin/relish
@@ -1,5 +1,8 @@
#!/usr/bin/env ruby
$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
-require 'relish/cli/runner'
-Relish::Cli::Runner.run(ARGV, $stderr, $stdout)
+args = ARGV.dup
+ARGV.clear
+command = args.shift.strip rescue 'help'
+
+Relish::Cli::Runner.run(command, $stderr, $stdout)
View
15 lib/relish/cli/options.rb
@@ -4,17 +4,20 @@
module Relish
module Cli
class OptionsParser < Trollop::Parser
+
COMMANDS = { 'push' => Commands::Push }
def initialize(error_stream, out_stream)
super
- banner "This is the relish gem. Valid commands are: #{valid_commands.join(",")}"
- opt :help, "Show help information"
- opt :host, "Host to connect to", :default => "relishapp.com"
- opt :account, "Account to connect to", :type => String, :required => true
- opt :project, "Project to connect to", :type => String, :required => true
- opt :version, "Version to connect to", :type => String
+ banner "This is the relish gem. Valid commands are: #{valid_commands.join(",")}"
+
+ opt :help, "Show help information"
+ opt :host, "Host to connect to", :short => '-h', :type => String
+ opt :account, "Account to connect to", :short => '-a', :type => String
+ opt :project, "Project to connect to", :short => '-p', :type => String
+ opt :version, "Version to connect to", :short => '-v', :type => String
+
stop_on valid_commands
end
View
31 lib/relish/commands/base.rb
@@ -1,8 +1,39 @@
+require 'trollop'
+
module Relish
module Command
class Base
include Relish::Helpers
+ DEFAULT_HOST = 'relishapp.com'
+ LOCAL_OPTIONS_FILE = '.relish'
+
+ def initialize(global_options = {})
+ @options = global_options
+ end
+
+ [:account, :project].each do |meth|
+ define_method meth do
+ @options[meth] || parse_options_file[meth]
+ end
+ end
+
+ def host
+ @options[:host] || DEFAULT_HOST
+ end
+
+ def parse_options_file
+ @parsed_options_file ||= begin
+ if File.exist?(LOCAL_OPTIONS_FILE)
+ parser = Trollop::Parser.new
+ parser.opt :account, "", :short => '-a', :type => String
+ parser.opt :project, "", :short => '-p', :type => String
+ parser.opt :version, "", :short => '-v', :type => String
+ parser.parse(File.read(LOCAL_OPTIONS_FILE).split)
+ else {} end
+ end
+ end
+
def api_token
File.read("#{home_directory}/.relish/api_token")
end
View
34 lib/relish/commands/push.rb
@@ -7,16 +7,10 @@ module Relish
module Command
class Push < Base
- def initialize(global_options)
- @options = global_options
- end
-
def run
- post features_as_tar_gz
+ post files_as_tar_gz
end
-
- private
-
+
def post(tar_gz_data)
resource = RestClient::Resource.new(url)
resource.post(tar_gz_data, :content_type => 'application/x-gzip')
@@ -30,30 +24,30 @@ def post(tar_gz_data)
end
def url
- host = @options[:host]
- account = @options[:account]
- project = @options[:project]
- version = @options[:version]
-
- "http://#{host}/pushes?account_id=#{account}&project_id=#{project}&api_token=#{api_token}".tap do |str|
- str << "&version_id=#{version}" if version
+ "".tap do |str|
+ str << "http://#{host}/pushes?"
+ str << "account_id=#{account}&"
+ str << "project_id=#{project}&"
+ str << "version_id=#{version}&" if version
+ str << "api_token=#{api_token}"
end
end
- def features_as_tar_gz
+ def version
+ @options[:version]
+ end
+
+ def files_as_tar_gz
stream = StringIO.new
-
begin
tgz = Zlib::GzipWriter.new(stream)
tar = Archive::Tar::Minitar::Output.new(tgz)
files.each do |entry|
Archive::Tar::Minitar.pack_file(entry, tar)
end
ensure
- # Closes both tar and sgz.
- tar.close if tar
+ tar.close if tar # Closes both tar and tgz.
end
-
stream.string
end
View
2 relish.gemspec
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
'trollop' => '~> 1.16.2',
'rspec' => '~> 2.0.0.beta.22',
'cucumber' => '~> 0.8.5',
- 'aruba' => '~> 0.2.1'
+ 'aruba' => '~> 0.2.2'
}.each do |lib, version|
s.add_development_dependency lib, version
end
View
73 spec/relish/commands/base_spec.rb
@@ -4,6 +4,79 @@ module Relish
module Command
describe Base do
+ {:account => 'rspec', :project => 'rspec-core'}.each do |meth, name|
+ describe "##{meth}" do
+ context 'passed in command line' do
+ let(:base) { described_class.new({meth => name}) }
+
+ it 'returns the value' do
+ base.send(meth).should eq(name)
+ end
+ end
+
+ context 'not passed in command line' do
+ let(:base) { described_class.new }
+
+ context 'and options file does not exist' do
+ it 'returns nil' do
+ base.send(meth).should be_nil
+ end
+ end
+ end
+ end
+ end
+
+ describe '#host' do
+ context 'passed in command line' do
+ let(:base) { described_class.new({:host => 'test.com'}) }
+
+ it 'returns test.com' do
+ base.host.should eq('test.com')
+ end
+ end
+
+ context 'not passed in command line' do
+ let(:base) { described_class.new }
+
+ it 'returns the default host' do
+ base.host.should eq(Base::DEFAULT_HOST)
+ end
+ end
+ end
+
+ describe '#parse_options_file' do
+ let(:base) { described_class.new }
+
+ context 'with options file that exists' do
+ let(:options) do
+ '--account rspec --project rspec-core'
+ end
+
+ before do
+ File.stub(:exist?).and_return(true)
+ File.stub(:read).and_return(options)
+ end
+
+ it 'parses the account' do
+ base.parse_options_file[:account].should eq('rspec')
+ end
+
+ it 'parses the project' do
+ base.parse_options_file[:project].should eq('rspec-core')
+ end
+ end
+
+ context 'with options file that does not exist' do
+ before do
+ File.stub(:exist?).and_return(false)
+ end
+
+ it 'returns an empty hash' do
+ base.parse_options_file.should eq({})
+ end
+ end
+ end
+
describe '#api_token' do
let(:base) { described_class.new }
View
54 spec/relish/commands/push_spec.rb
@@ -3,6 +3,60 @@
module Relish
module Command
describe Push do
+
+ describe '#url' do
+ before do
+ push.should_receive(:account).and_return('rspec')
+ push.should_receive(:project).and_return('rspec')
+ push.should_receive(:api_token).and_return('abc')
+ end
+
+ context 'without version' do
+ let(:push) { described_class.new }
+
+ specify do
+ push.url.should eq(
+ "http://relishapp.com/pushes?account_id=rspec&project_id=rspec&api_token=abc"
+ )
+ end
+ end
+
+ context 'with version' do
+ let(:push) { described_class.new(:version => 'one') }
+
+ specify do
+ push.url.should eq(
+ "http://relishapp.com/pushes?" \
+ "account_id=rspec&project_id=rspec&version_id=one&api_token=abc"
+ )
+ end
+ end
+ end
+
+ describe '#version' do
+ context 'with :version in @options' do
+ let(:push) { described_class.new(:version => 'one') }
+ specify { push.version.should eq('one') }
+ end
+
+ context 'with :version not in @options' do
+ let(:push) { described_class.new }
+ specify { push.version.should be_nil }
+ end
+ end
+
+ describe '#files_as_tar_gz' do
+ let(:push) { described_class.new }
+ specify { expect { push.files_as_tar_gz }.to_not raise_exception }
+ specify { push.files_as_tar_gz.should be_a(String) }
+ end
+
+ describe '#files' do
+ let(:push) { described_class.new }
+ specify { expect { push.files }.to_not raise_exception }
+ specify { push.files.should be_a(Array) }
+ end
+
end
end
end

0 comments on commit c76e53f

Please sign in to comment.