From f1d543c892d6f2fc2451bbb2d0bf17bb5b04f09d Mon Sep 17 00:00:00 2001 From: ragaskar Date: Fri, 21 Oct 2011 08:01:32 -0700 Subject: [PATCH] Simple Rubygems backwards compatibility --- Rakefile | 13 +-- .../templates/jasmine-example/SpecRunner.html | 4 +- .../javascripts/support/jasmine_runner.rb | 6 +- .../javascripts/support/jasmine_runner.rb | 6 +- lib/jasmine.rb | 3 +- lib/jasmine/base.rb | 14 ---- lib/jasmine/dependencies.rb | 30 +++++++ lib/jasmine/spec_builder.rb | 2 +- lib/jasmine/tasks/jasmine.rake | 4 +- spec/dependencies_spec.rb | 84 +++++++++++++++++++ spec/jasmine_rails2_spec.rb | 2 +- spec/jasmine_rails3_spec.rb | 2 +- spec/jasmine_self_test_spec.rb | 2 +- spec/spec_helper.rb | 20 +---- 14 files changed, 137 insertions(+), 55 deletions(-) create mode 100644 lib/jasmine/dependencies.rb create mode 100644 spec/dependencies_spec.rb diff --git a/Rakefile b/Rakefile index c316025e..2fadf22d 100644 --- a/Rakefile +++ b/Rakefile @@ -4,15 +4,8 @@ require "bundler" Bundler.setup Bundler::GemHelper.install_tasks -def rspec2? - Gem.available? "rspec", ">= 2.0" -end - -def rails3? - Gem.available? "rails", ">= 3.0" -end - -if rspec2? +require "jasmine" +if Jasmine::Dependencies.rspec2? require 'rspec' require 'rspec/core/rake_task' else @@ -22,7 +15,7 @@ end require 'ci/reporter/rake/rspec' desc "Run all examples" -if rspec2? +if Jasmine::Dependencies.rspec2? RSpec::Core::RakeTask.new(:spec) do |t| t.pattern = 'spec/**/*_spec.rb' end diff --git a/generators/jasmine/templates/jasmine-example/SpecRunner.html b/generators/jasmine/templates/jasmine-example/SpecRunner.html index 7658ed9c..ea58503d 100644 --- a/generators/jasmine/templates/jasmine-example/SpecRunner.html +++ b/generators/jasmine/templates/jasmine-example/SpecRunner.html @@ -10,11 +10,11 @@ - + - + diff --git a/generators/jasmine/templates/spec/javascripts/support/jasmine_runner.rb b/generators/jasmine/templates/spec/javascripts/support/jasmine_runner.rb index 13ebce0c..f3944f37 100644 --- a/generators/jasmine/templates/spec/javascripts/support/jasmine_runner.rb +++ b/generators/jasmine/templates/spec/javascripts/support/jasmine_runner.rb @@ -4,7 +4,7 @@ require 'jasmine' jasmine_config_overrides = File.expand_path(File.join(File.dirname(__FILE__), 'jasmine_config.rb')) require jasmine_config_overrides if File.exist?(jasmine_config_overrides) -if Jasmine::rspec2? +if Jasmine::Dependencies.rspec2? require 'rspec' else require 'spec' @@ -15,7 +15,7 @@ should_stop = false -if Jasmine::rspec2? +if Jasmine::Dependencies.rspec2? RSpec.configuration.after(:suite) do spec_builder.stop if should_stop end @@ -29,4 +29,4 @@ spec_builder.start should_stop = true -spec_builder.declare_suites \ No newline at end of file +spec_builder.declare_suites diff --git a/lib/generators/jasmine/templates/spec/javascripts/support/jasmine_runner.rb b/lib/generators/jasmine/templates/spec/javascripts/support/jasmine_runner.rb index 13ebce0c..f3944f37 100644 --- a/lib/generators/jasmine/templates/spec/javascripts/support/jasmine_runner.rb +++ b/lib/generators/jasmine/templates/spec/javascripts/support/jasmine_runner.rb @@ -4,7 +4,7 @@ require 'jasmine' jasmine_config_overrides = File.expand_path(File.join(File.dirname(__FILE__), 'jasmine_config.rb')) require jasmine_config_overrides if File.exist?(jasmine_config_overrides) -if Jasmine::rspec2? +if Jasmine::Dependencies.rspec2? require 'rspec' else require 'spec' @@ -15,7 +15,7 @@ should_stop = false -if Jasmine::rspec2? +if Jasmine::Dependencies.rspec2? RSpec.configuration.after(:suite) do spec_builder.stop if should_stop end @@ -29,4 +29,4 @@ spec_builder.start should_stop = true -spec_builder.declare_suites \ No newline at end of file +spec_builder.declare_suites diff --git a/lib/jasmine.rb b/lib/jasmine.rb index 213f1672..1e45c77c 100644 --- a/lib/jasmine.rb +++ b/lib/jasmine.rb @@ -1,4 +1,5 @@ jasmine_files = ['base', + 'dependencies', 'config', 'server', 'selenium_driver', @@ -9,6 +10,6 @@ require File.join('jasmine', file) end -require File.join('jasmine', "railtie") if Jasmine.rails3? +require File.join('jasmine', "railtie") if Jasmine::Dependencies.rails3? diff --git a/lib/jasmine/base.rb b/lib/jasmine/base.rb index f5d5efaa..96493f7d 100644 --- a/lib/jasmine/base.rb +++ b/lib/jasmine/base.rb @@ -39,18 +39,4 @@ def self.wait_for_listener(port, name = "required process", seconds_to_wait = 10 end end - def self.rspec2? - Gem::Specification::find_by_name "rspec", ">= 2.0" - rescue - Gem.available? "rspec", ">= 2.0" - end - - def self.rails3? - return Rails.version.split(".").first.to_i == 3 if defined? Rails - begin - Gem::Specification::find_by_name "rails", ">= 3.0" - rescue - Gem.available? "rails", ">= 3.0" - end - end end diff --git a/lib/jasmine/dependencies.rb b/lib/jasmine/dependencies.rb new file mode 100644 index 00000000..972033f4 --- /dev/null +++ b/lib/jasmine/dependencies.rb @@ -0,0 +1,30 @@ +module Jasmine + module Dependencies + + class << self + def rspec2? + safe_gem_check("rspec", ">= 2.0") + end + + def rails2? + safe_gem_check("rails", "~> 2.3") + end + + def rails3? + safe_gem_check("rails", ">= 3.0") + end + + private + def safe_gem_check(gem_name, version_string) + if Gem::Specification.respond_to?(:find_by_name) + Gem::Specification.find_by_name(gem_name, version_string) + elsif Gem.respond_to?(:available?) + Gem.available?(gem_name, version_string) + end + rescue Gem::LoadError + false + end + + end + end +end diff --git a/lib/jasmine/spec_builder.rb b/lib/jasmine/spec_builder.rb index 3c7e7d4b..49c16baa 100644 --- a/lib/jasmine/spec_builder.rb +++ b/lib/jasmine/spec_builder.rb @@ -109,7 +109,7 @@ def declare_spec(parent, spec) example_name = spec["name"] @spec_ids << spec["id"] backtrace = @example_locations[parent.description + " " + example_name] - if Jasmine::rspec2? + if Jasmine::Dependencies.rspec2? parent.it example_name, {} do me.report_spec(spec["id"]) end diff --git a/lib/jasmine/tasks/jasmine.rake b/lib/jasmine/tasks/jasmine.rake index f17d958d..ba2d0492 100644 --- a/lib/jasmine/tasks/jasmine.rake +++ b/lib/jasmine/tasks/jasmine.rake @@ -14,7 +14,7 @@ namespace :jasmine do desc "Run continuous integration tests" task :ci => ["jasmine:require_json", "jasmine:require"] do - if Jasmine::rspec2? + if Jasmine::Dependencies.rspec2? require "rspec" require "rspec/core/rake_task" else @@ -22,7 +22,7 @@ namespace :jasmine do require 'spec/rake/spectask' end - if Jasmine::rspec2? + if Jasmine::Dependencies.rspec2? RSpec::Core::RakeTask.new(:jasmine_continuous_integration_runner) do |t| t.rspec_opts = ["--colour", "--format", ENV['JASMINE_SPEC_FORMAT'] || "progress"] t.verbose = true diff --git a/spec/dependencies_spec.rb b/spec/dependencies_spec.rb new file mode 100644 index 00000000..b4331ec7 --- /dev/null +++ b/spec/dependencies_spec.rb @@ -0,0 +1,84 @@ +require 'spec_helper' + +describe Jasmine::Dependencies do + + context "with ruby gems > 1.8" do + before do + Gem::Specification.should_receive(:respond_to?).with(:find_by_name).and_return(true) + end + + context "and rspec 2" do + before do + Gem::Specification.should_receive(:find_by_name).with("rspec", ">= 2.0").and_raise(Gem::LoadError) + end + it "should return the correct results" do + Jasmine::Dependencies.rspec2?.should be false + end + + it "should not raise an error" do + lambda do + Jasmine::Dependencies.rspec2? + end.should_not raise_error(Gem::LoadError) + end + end + + context "and rails 2" do + before do + Gem::Specification.should_receive(:find_by_name).with("rails", "~> 2.3").and_raise(Gem::LoadError) + end + it "should return the correct results" do + Jasmine::Dependencies.rails2?.should be false + end + + it "should not raise an error" do + lambda do + Jasmine::Dependencies.rails2? + end.should_not raise_error(Gem::LoadError) + end + end + + context "and rails 3" do + before do + Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_raise(Gem::LoadError) + end + it "should return the correct results" do + Jasmine::Dependencies.rails3?.should be false + end + + it "should not raise an error" do + lambda do + Jasmine::Dependencies.rails3? + end.should_not raise_error(Gem::LoadError) + end + end + end + + context "with ruby_gems < 1.8" do + before do + Gem::Specification.should_receive(:respond_to?).with(:find_by_name).and_return(false) + end + context "and rspec 2" do + it "should not break when it is not present" do + Gem.should_receive(:available?).with("rspec", ">= 2.0").and_return false + Jasmine::Dependencies.rspec2?.should be false + end + end + + context "and rails 2" do + it "should not break when it is not present" do + Gem.should_receive(:available?).with("rails", "~> 2.3").and_return false + Jasmine::Dependencies.rails2?.should be false + end + end + + context "and rails 3" do + it "should not break when it is not present" do + Gem.should_receive(:available?).with("rails", ">= 3.0").and_return false + Jasmine::Dependencies.rails3?.should be false + end + end + end + +end + + diff --git a/spec/jasmine_rails2_spec.rb b/spec/jasmine_rails2_spec.rb index 5fcb3a3a..7ecca588 100644 --- a/spec/jasmine_rails2_spec.rb +++ b/spec/jasmine_rails2_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper")) -if rails2? +if Jasmine::Dependencies.rails2? describe "A Rails 2 app" do diff --git a/spec/jasmine_rails3_spec.rb b/spec/jasmine_rails3_spec.rb index 5b4afb9c..ad6c2c26 100644 --- a/spec/jasmine_rails3_spec.rb +++ b/spec/jasmine_rails3_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper")) -if rails3? +if Jasmine::Dependencies.rails3? describe "A Rails 3 app" do diff --git a/spec/jasmine_self_test_spec.rb b/spec/jasmine_self_test_spec.rb index a4fc0f12..9a609bdc 100644 --- a/spec/jasmine_self_test_spec.rb +++ b/spec/jasmine_self_test_spec.rb @@ -6,7 +6,7 @@ should_stop = false -if rspec2? +if Jasmine::Dependencies.rspec2? RSpec.configuration.after(:suite) do spec_builder.stop if should_stop end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 78392002..2adfa217 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,30 +5,18 @@ Bundler.setup(:default, :development) -def rspec2? - Gem.available? "rspec", ">= 2.0" -end - -def rails2? - Gem.available? "rails", "~> 2.3" -end - -def rails3? - Gem.available? "rails", ">= 3.0" -end +$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "../lib"))) +require "jasmine" -if rspec2? +if Jasmine::Dependencies.rspec2? require 'rspec' else require 'spec' end -$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "../lib"))) - -require "jasmine" def create_rails(name) - if rails3? + if Jasmine::Dependencies.rails3? `rails new #{name}` else `rails #{name}`