Skip to content

Commit

Permalink
Simple Rubygems backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ragaskar committed Oct 21, 2011
1 parent fd3f292 commit f1d543c
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 55 deletions.
13 changes: 3 additions & 10 deletions Rakefile
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions generators/jasmine/templates/jasmine-example/SpecRunner.html
Expand Up @@ -10,11 +10,11 @@
<script type="text/javascript" src="lib/jasmine-1.1.0.rc1/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-1.1.0.rc1/jasmine-html.js"></script>

<!-- include source files here... -->
<!-- include spec files here... -->
<script type="text/javascript" src="spec/SpecHelper.js"></script>
<script type="text/javascript" src="spec/PlayerSpec.js"></script>

<!-- include spec files here... -->
<!-- include source files here... -->
<script type="text/javascript" src="src/Player.js"></script>
<script type="text/javascript" src="src/Song.js"></script>

Expand Down
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -29,4 +29,4 @@

spec_builder.start
should_stop = true
spec_builder.declare_suites
spec_builder.declare_suites
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -29,4 +29,4 @@

spec_builder.start
should_stop = true
spec_builder.declare_suites
spec_builder.declare_suites
3 changes: 2 additions & 1 deletion lib/jasmine.rb
@@ -1,4 +1,5 @@
jasmine_files = ['base',
'dependencies',
'config',
'server',
'selenium_driver',
Expand All @@ -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?


14 changes: 0 additions & 14 deletions lib/jasmine/base.rb
Expand Up @@ -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
30 changes: 30 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion lib/jasmine/spec_builder.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/jasmine/tasks/jasmine.rake
Expand Up @@ -14,15 +14,15 @@ 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
require "spec"
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
Expand Down
84 changes: 84 additions & 0 deletions 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


2 changes: 1 addition & 1 deletion 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

Expand Down
2 changes: 1 addition & 1 deletion 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

Expand Down
2 changes: 1 addition & 1 deletion spec/jasmine_self_test_spec.rb
Expand Up @@ -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
Expand Down
20 changes: 4 additions & 16 deletions spec/spec_helper.rb
Expand Up @@ -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}`
Expand Down

0 comments on commit f1d543c

Please sign in to comment.