Permalink
Browse files

Fix issue #3 - Supporting bundler

- When have gemfile -> run with 'bundle exec' command
- If not have gemfile -> run normally

w00t =]
  • Loading branch information...
1 parent 57c83c4 commit 7e84a7ffa15fac069caf52a538aad4967e0c8b09 Tomas D'Stefano committed Oct 14, 2010
View
@@ -0,0 +1,22 @@
+The MIT License
+
+Copyright (c) 2010 Tomás D'Stefano
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -106,7 +106,7 @@ def have_gemfile?
end
def gemfile
- File.join(File.dirname(__FILE__), 'Gemfile')
+ File.join(Dir.pwd, 'Gemfile')
end
# Contruct all the commands for the test framework
@@ -26,6 +26,8 @@ module ClassMethods
# Set the binary to search in the RVM binary folder
#
+ # binary :bundle
+ #
def binary(binary_name, options={})
method_sufix = options[:name] || binary_name
eval <<-EVAL
@@ -1,11 +1,61 @@
module InfinityTest
class TestFramework
- attr_accessor :message, :test_directory_pattern, :rubies, :test_pattern
+ include BinaryPath
+
+ binary :bundle
+
+ attr_accessor :application, :message, :test_directory_pattern, :rubies, :test_pattern
def initialize(options={})
+ @application = InfinityTest.application
@rubies = options[:rubies] || []
end
+ # Run in context of each Ruby Environment, and the Ruby Version
+ #
+ def environments(&block)
+ raise unless block_given?
+ RVM.environments(rubies).each do |environment|
+ ruby_version = environment.environment_name
+ block.call(environment, ruby_version)
+ end
+ end
+
+ #
+ # if application.have_gemfile?
+ # run_with_bundler!
+ # else
+ # run_without_bundler!
+ # end
+ #
+ def construct_command(options)
+ binary_name, ruby_version, command, file, environment = resolve_options(options)
+ unless have_binary?(binary_name) || options[:skip_binary?]
+ print_message(binary_name, ruby_version)
+ else
+ command = "ruby #{command} #{decide_files(file)}"
+ rvm_ruby_version = "rvm #{ruby_version}"
+ if application.have_gemfile?
+ run_with_bundler!(rvm_ruby_version, command, environment)
+ else
+ run_without_bundler!(rvm_ruby_version, command)
+ end
+ end
+ end
+
+ def run_with_bundler!(rvm_ruby_version, command, environment)
+ bundle_binary = search_bundle(environment)
+ %{#{rvm_ruby_version} #{bundle_binary} exec #{command}}
+ end
+
+ def run_without_bundler!(rvm_ruby_version, command)
+ %{#{rvm_ruby_version} #{command}}
+ end
+
+ # Contruct all the Commands for each ruby instance variable
+ # If don't want to run with many rubies, add the current ruby to the rubies instance
+ # and create the command with current ruby
+ #
def construct_commands(file=nil)
@rubies << RVM::Environment.current.environment_name if @rubies.empty?
construct_rubies_commands(file)
@@ -93,5 +143,16 @@ def test_message(output, patterns)
lines.select { |line| line =~ patterns.values.first }.first
end
+ private
+ def resolve_options(options)
+ ruby_version = options[:for]
+ binary_name = options[:skip_binary?] ? '' : options[:binary]
+ load_path = %{-I"#{options[:load_path]}"} if options[:load_path]
+ environment = options[:environment]
+ file = options[:file]
+ command = [ binary_name, load_path].compact.join(' ')
+ [binary_name, ruby_version, command, file, environment]
+ end
+
end
end
@@ -6,55 +6,33 @@ class Bacon < TestFramework
binary :bacon
parse_results :specifications => /(\d+) specifications/, :requirements => /(\d+) requirements/, :failures => /(\d+) failure/, :errors => /(\d+) errors/
+ # Bacon Framework
#
- # construct_commands(:bacon) do |environment, ruby_version|
- #
- # command = "rvm #{ruby_version} ruby"
- # bacon_binary = search_bacon(environment)
- # unless have_binary?(bacon_binary)
- # print_message('bacon', ruby_version)
- # else
- # results[ruby_version] = "rvm #{ruby_version} ruby #{bacon_binary} #{decide_files(file)}"
- # end
- # end
+ # For more information about the Bacon see: http://github.com/chneukirchen/bacon
#
# bacon = InfinityTest::Bacon.new(:rubies => '1.9.1,1.9.2')
# bacon.rubies # => '1.9.1,1.9.2'
- #
+ # bacon.test_directory_pattern # => "^spec/*/(.*)_spec.rb"
+ # bacon.test_pattern # => 'spec/**/*_spec.rb'
+ #
def initialize(options={})
super(options)
@test_directory_pattern = "^spec/*/(.*)_spec.rb"
@test_pattern = options[:test_pattern] || 'spec/**/*_spec.rb'
end
- # environments do |environment, ruby_version|
- # bacon_binary = search_bacon(environment)
- # create_command(:ruby_version => ruby_version, :binary => bacon_binary)
- # end
- #
- # def create_command(options)
- # ruby_version = options[:ruby_version]
- # binary_name = options[:binary]
- # if application.have_gemfile?
- # run_with_bundler!
- # else
- # run_without_bundler!
- # end
- # end
+ # Construct all the commands for each ruby
+ # First, try to find the bacon binary, and raise/puts an Error if don't find it.
+ # After that, verifying if the user have a Gemfile, and if has, run with "bundle exec" command, else will run normally
#
-
def construct_rubies_commands(file=nil)
- results = Hash.new
- RVM.environments(@rubies) do |environment|
- ruby_version = environment.environment_name
+ commands = {}
+ environments do |environment, ruby_version|
bacon_binary = search_bacon(environment)
- unless have_binary?(bacon_binary)
- print_message('bacon', ruby_version)
- else
- results[ruby_version] = "rvm #{ruby_version} ruby #{bacon_binary} -Ilib -d #{decide_files(file)}"
- end
+ command = construct_command(:for => ruby_version, :binary => bacon_binary, :load_path => 'lib:spec', :file => file, :environment => environment) || next
+ commands[ruby_version] = command
end
- results
+ commands
end
def sucess?
@@ -66,6 +44,10 @@ def failure?
@failures > 0
end
+ def pending?
+ false # Don't have pending in Bacon
+ end
+
end
end
end
@@ -1,8 +1,6 @@
module InfinityTest
module TestLibrary
class Rspec < TestFramework
- include BinaryPath
-
binary :rspec, :name => :rspec_two
binary :spec, :name => :rspec_one
@@ -20,19 +18,18 @@ def initialize(options={})
@test_pattern = 'spec/**/*_spec.rb'
end
+ # Construct all the commands for each ruby
+ # First, try to find the rspec one binary, and if dont have installed try to find rspec two, and raise/puts an Error if don't find it.
+ # After that, verifying if the user have a Gemfile, and if has, run with "bundle exec" command, else will run normally
+ #
def construct_rubies_commands(file=nil)
- results = Hash.new
- RVM.environments(@rubies) do |environment|
- ruby_version = environment.environment_name
+ commands = {}
+ environments do |environment, ruby_version|
rspec_binary = search_rspec_two(environment)
- rspec_binary = search_rspec_one(environment) unless File.exist?(rspec_binary)
- unless have_binary?(rspec_binary)
- print_message('rspec', ruby_version)
- else
- results[ruby_version] = "rvm #{ruby_version} ruby #{rspec_binary} #{decide_files(file)}"
- end
+ rspec_binary = search_rspec_one(environment) unless have_binary?(rspec_binary)
+ commands[ruby_version] = construct_command(:for => ruby_version, :binary => rspec_binary, :file => file, :environment => environment)
end
- results
+ commands
end
def sucess?
@@ -10,12 +10,11 @@ def initialize(options={})
end
def construct_rubies_commands(file=nil)
- results = Hash.new
- RVM.environments(@rubies) do |environment|
- ruby_version = environment.environment_name
- results[ruby_version] = "rvm #{ruby_version} ruby -I'lib:test' #{decide_files(file)}"
+ command = {}
+ environments do |environment, ruby_version|
+ command[ruby_version] = construct_command :for => ruby_version, :load_path => 'lib:test', :file => file, :environment => environment, :skip_binary? => true
end
- results
+ command
end
def test_files
@@ -15,20 +15,24 @@ class Example
describe '.binary' do
+ before do
+ @example = Example.new
+ end
+
it "should create the binary for bacon framework" do
- lambda { Example.new.search_bacon(current_env) }.should_not raise_exception
+ lambda { @example.search_bacon(current_env) }.should_not raise_exception
end
it "should create the binary for cucumber framework" do
- lambda { Example.new.search_cucumber(current_env) }.should_not raise_exception
+ lambda { @example.search_cucumber(current_env) }.should_not raise_exception
end
it "should create the binary for rspec two with sufix of rspec_two" do
- lambda { Example.new.search_rspec_two(current_env) }.should_not raise_exception
+ lambda { @example.search_rspec_two(current_env) }.should_not raise_exception
end
it "should create the binary for rspec one with sufix of rspec_one" do
- lambda { Example.new.search_rspec_one(current_env) }.should_not raise_exception
+ lambda { @example.search_rspec_one(current_env) }.should_not raise_exception
end
end
@@ -16,6 +16,27 @@ class OtherFramework < TestFramework
let(:some_framework) { SomeFramework.new }
let(:other_framework) { OtherFramework.new }
+
+ describe '#environments' do
+
+ it "should run in the scope of RVM environment" do
+ lambda {
+ some_framework.environments do |environment, ruby_version|
+ environment.should be_instance_of(RVM::Environment)
+ end
+ }.should_not raise_exception
+ end
+
+ it "should raise an error when not have block" do
+ lambda {some_framework.environments}.should raise_exception
+ end
+
+ end
+
+ describe '#application' do
+ it { some_framework.application.should equal InfinityTest.application}
+ it { other_framework.application.should equal InfinityTest.application}
+ end
describe '#parse_results' do
@@ -33,6 +33,10 @@ module TestLibrary
Bacon.new(:test_pattern => 'spec/**/spec_*.rb').test_pattern.should == 'spec/**/spec_*.rb'
end
+ it "should return false for #pending method" do
+ Bacon.new.pending?.should be_false
+ end
+
describe '#test_files' do
let(:bacon) { Bacon.new }
@@ -148,6 +152,63 @@ module TestLibrary
end
+ describe '#construct_rubies_commands' do
+
+ before do
+ @bacon = Bacon.new
+ @bacon.stub(:have_binary?).and_return(true)
+ @bacon.application.stub(:have_gemfile?).and_return(false)
+ end
+
+ it "should return a Hash" do
+ @bacon.construct_rubies_commands.should be_instance_of(Hash)
+ end
+
+ it "should return the keys of the rubies" do
+ bacon = Bacon.new(:rubies => '1.9.2')
+ bacon.should_receive(:have_binary?).and_return(true)
+ bacon.construct_rubies_commands.keys.should == ["1.9.2"]
+ end
+
+ it "should return the rvm with the environment name" do
+ first_element(@bacon.construct_commands).should match /^rvm #{environment_name}/
+ end
+
+ it "should include ruby command" do
+ first_element(@bacon.construct_commands).should =~ / ruby /
+ end
+
+ it "should include bacon(nhame nhame) in the command" do
+ first_element(@bacon.construct_commands).should =~ /\/bacon /
+ end
+
+ it "should include the load path with lib and spec directory" do
+ first_element(@bacon.construct_commands).should match /-I"lib:spec"/
+ end
+
+ it "should include the files to test" do
+ first_element(@bacon.construct_commands).should match /spec.rb/
+ end
+
+ it "should not include bundle exec when Gemfile is not present" do
+ application_without_gemfile(@bacon.application)
+ first_element(@bacon.construct_commands).should_not =~ /bundle exec /
+ end
+
+ it "should include bundle exec when Gemfile is present" do
+ application_with_gemfile(@bacon.application)
+ first_element(@bacon.construct_commands).should =~ /\/bundle exec /
+ end
+
+ def first_element(hash, hash_size=1)
+ hash.should have(hash_size).item
+ command = hash.each { |ruby_version, command_to_run|
+ return command_to_run
+ }
+ end
+
+ end
+
end
end
end
Oops, something went wrong.

0 comments on commit 7e84a7f

Please sign in to comment.