Permalink
Browse files

Added integration coverage and fixes for older versions of Rails

  • Loading branch information...
1 parent 2be23d7 commit 1f878913c34c9f3bfe7ee2b48ef530cf7ff67891 @jferris jferris committed Jan 19, 2010
View
@@ -218,18 +218,10 @@ errors are not reported while running tests.
end
end
-== Supported rails versions
-
-the notifier currently supports the following versions of Rails:
-
-* 1.2.6
-* 2.0.2
-* 2.1.0
-* 2.1.2
-* 2.2.2
-* 2.3.2
-* 2.3.3
-* 2.3.4
+== Supported Rails versions
+
+See SUPPORTED_RAILS_VERSIONS for a list of official supported versions of
+Rails.
Please open up a support ticket on Tender ( http://help.hoptoadapp.com ) if
you're using a version of Rails that is not listed above and the notifier is
View
@@ -72,15 +72,19 @@ task :gemspec do
end
LOCAL_GEM_ROOT = File.join(GEM_ROOT, 'tmp', 'local_gems').freeze
-LOCAL_GEMS = %w(rails sham_rack capistrano)
+RAILS_VERSIONS = IO.read('SUPPORTED_RAILS_VERSIONS').strip.split("\n")
+LOCAL_GEMS = [['sham_rack', nil], ['capistrano', nil], ['sqlite3-ruby', nil]] +
+ RAILS_VERSIONS.collect { |version| ['rails', version] }
task :vendor_test_gems do
- LOCAL_GEMS.each do |gem_name|
- pattern = File.join(LOCAL_GEM_ROOT, 'gems', "#{gem_name}-*")
+ LOCAL_GEMS.each do |gem_name, version|
+ gem_file_pattern = [gem_name, version || '*'].compact.join('-')
+ version_option = version ? "-v #{version}" : ''
+ pattern = File.join(LOCAL_GEM_ROOT, 'gems', "#{gem_file_pattern}")
existing = Dir.glob(pattern).first
unless existing
- command = "gem install -i #{LOCAL_GEM_ROOT} --no-ri --no-rdoc #{gem_name}"
- puts "Vendoring #{gem_name}..."
+ command = "gem install -i #{LOCAL_GEM_ROOT} --no-ri --no-rdoc #{version_option} #{gem_name}"
+ puts "Vendoring #{gem_file_pattern}..."
unless system(command)
$stderr.puts "Command failed: #{command}"
end
@@ -94,3 +98,20 @@ Cucumber::Rake::Task.new(:cucumber) do |t|
end
task :cucumber => [:gemspec, :vendor_test_gems]
+
+OLD_RAILS_VERSIONS = RAILS_VERSIONS[0...-1]
+
+namespace :cucumber do
+ namespace :rails do
+ OLD_RAILS_VERSIONS.each do |version|
+ desc "Test integration of the gem with Rails #{version}"
+ task version do
+ ENV['RAILS_VERSION'] = version
+ system("cucumber --format progress features/rails.feature")
+ end
+ end
+
+ desc "Test integration of the gem with all Rails versions"
+ task :all => [:cucumber, *OLD_RAILS_VERSIONS]
+ end
+end
@@ -0,0 +1,8 @@
+1.2.6
+2.0.2
+2.1.0
+2.1.2
+2.2.2
+2.3.2
+2.3.3
+2.3.4
@@ -3,7 +3,7 @@ Feature: Install the Gem in a Rails application
Background:
Given I have built and installed the "hoptoad_notifier" gem
- Scenario: Use config.gem without vendoring the gem in a Rails application
+ Scenario: Use the gem without vendoring the gem in a Rails application
When I generate a new Rails application
And I configure the Hoptoad shim
And I configure my application to require the "hoptoad_notifier" gem
@@ -14,10 +14,11 @@ Feature: Install the Gem in a Rails application
When I generate a new Rails application
And I configure the Hoptoad shim
And I configure my application to require the "hoptoad_notifier" gem
- And I run "rake gems:unpack"
- And I uninstall the "hoptoad_notifier" gem
+ And I unpack the "hoptoad_notifier" gem
And I run "script/generate hoptoad -k myapikey"
- Then I should receive a Hoptoad notification
+ And I uninstall the "hoptoad_notifier" gem
+ And I run "rake hoptoad:test"
+ Then I should receive two Hoptoad notifications
Scenario: Configure the notifier by hand
When I generate a new Rails application
@@ -1,6 +1,8 @@
When /^I generate a new Rails application$/ do
@terminal.cd(TEMP_DIR)
- @terminal.run("rails rails_root")
+ version_string = ENV['RAILS_VERSION'] ? "_#{ENV['RAILS_VERSION']}_" : ''
+ @terminal.run("rails #{version_string} rails_root")
+ @terminal.echo("Generated a Rails #{rails_version} application")
end
Given /^I have installed the "([^\"]*)" gem$/ do |gem_name|
@@ -12,14 +14,25 @@
end
When /^I configure my application to require the "([^\"]*)" gem$/ do |gem_name|
- path = File.join(RAILS_ROOT, 'config', 'environment.rb')
- run = "Rails::Initializer.run do |config|"
- insert = " config.gem '#{gem_name}'"
- content = File.read(path)
- if content.sub!(run, "#{run}\n#{insert}")
- File.open(path, 'wb') { |file| file.write(content) }
+ if rails_manages_gems?
+ run = "Rails::Initializer.run do |config|"
+ insert = " config.gem '#{gem_name}'"
+ content = File.read(environment_path)
+ if content.sub!(run, "#{run}\n#{insert}")
+ File.open(environment_path, 'wb') { |file| file.write(content) }
+ else
+ raise "Couldn't find #{run.inspect} in #{environment_path}"
+ end
else
- raise "Couldn't find #{run.inspect} in #{path}"
+ File.open(environment_path, 'a') do |file|
+ file.puts
+ file.puts("require 'hoptoad_notifier'")
+ file.puts("require 'hoptoad_notifier/rails'")
+ end
+
+ unless rails_finds_generators_in_gems?
+ FileUtils.cp_r(File.join(PROJECT_ROOT, 'generators'), File.join(RAILS_ROOT, 'lib'))
+ end
end
end
@@ -32,20 +45,45 @@
@terminal.output.should include("[Hoptoad] Success: Net::HTTPOK")
end
+Then /^I should receive two Hoptoad notifications$/ do
+ @terminal.output.scan(/\[Hoptoad\] Success: Net::HTTPOK/).size.should == 2
+end
+
When /^I configure the Hoptoad shim$/ do
shim_file = File.join(PROJECT_ROOT, 'features', 'support', 'hoptoad_shim.rb.template')
- target = File.join(RAILS_ROOT, 'config', 'initializers', 'hoptoad_shim.rb')
- FileUtils.cp(shim_file, target)
+ if rails_supports_initializers?
+ target = File.join(RAILS_ROOT, 'config', 'initializers', 'hoptoad_shim.rb')
+ FileUtils.cp(shim_file, target)
+ else
+ File.open(environment_path, 'a') do |file|
+ file.puts
+ file.write IO.read(shim_file)
+ end
+ end
end
When /^I configure the notifier to use "([^\"]*)" as an API key$/ do |api_key|
config_file = File.join(RAILS_ROOT, 'config', 'initializers', 'hoptoad.rb')
- File.open(config_file, 'w') do |file|
- file.write(<<-EOF)
- HoptoadNotifier.configure do |config|
- config.api_key = #{api_key.inspect}
- end
- EOF
+ if rails_manages_gems?
+ requires = ''
+ else
+ requires = "require 'hoptoad_notifier'"
+ end
+
+ initializer_code = <<-EOF
+ #{requires}
+ HoptoadNotifier.configure do |config|
+ config.api_key = #{api_key.inspect}
+ end
+ EOF
+
+ if rails_supports_initializers?
+ File.open(config_file, 'w') { |file| file.write(initializer_code) }
+ else
+ File.open(environment_path, 'a') do |file|
+ file.puts
+ file.puts initializer_code
+ end
end
end
@@ -56,3 +94,21 @@
When /^I uninstall the "([^\"]*)" gem$/ do |gem_name|
@terminal.uninstall_gem(gem_name)
end
+
+When /^I unpack the "([^\"]*)" gem$/ do |gem_name|
+ if rails_manages_gems?
+ @terminal.cd(RAILS_ROOT)
+ @terminal.run("rake gems:unpack GEM=#{gem_name}")
+ else
+ vendor_dir = File.join(RAILS_ROOT, 'vendor', 'gems')
+ FileUtils.mkdir_p(vendor_dir)
+ @terminal.cd(vendor_dir)
+ @terminal.run("gem unpack #{gem_name}")
+ gem_path =
+ Dir.glob(File.join(RAILS_ROOT, 'vendor', 'gems', "#{gem_name}-*", 'lib')).first
+ File.open(environment_path, 'a') do |file|
+ file.puts
+ file.puts("$: << #{gem_path.inspect}")
+ end
+ end
+end
@@ -0,0 +1,24 @@
+module RailsHelpers
+ def rails_version
+ environment_file = File.join(RAILS_ROOT, 'config', 'environment.rb')
+ @rails_version ||= `grep RAILS_GEM_VERSION #{environment_file}`.match(/[\d.]+/)[0]
+ end
+
+ def rails_manages_gems?
+ rails_version =~ /^2\.[123]/
+ end
+
+ def rails_supports_initializers?
+ rails_version =~ /^2\./
+ end
+
+ def rails_finds_generators_in_gems?
+ rails_version =~ /^2\./
+ end
+
+ def environment_path
+ File.join(RAILS_ROOT, 'config', 'environment.rb')
+ end
+end
+
+World(RailsHelpers)
@@ -27,6 +27,10 @@ def run(command)
@status = $?
end
+ def echo(string)
+ logger.debug(string)
+ end
+
def build_and_install_gem(gemspec)
pkg_dir = File.join(TEMP_DIR, 'pkg')
FileUtils.mkdir_p(pkg_dir)
@@ -7,7 +7,7 @@ def add_options!(opt)
end
def manifest
- unless File.exists?('config/initializers/hoptoad.rb') || options[:api_key]
+ if !api_key_configured? && !options[:api_key]
puts "Must pass --api-key or create config/initializers/hoptoad.rb"
exit
end
@@ -17,11 +17,26 @@ def manifest
if File.exists?('config/deploy.rb')
m.append_to 'config/deploy.rb', "require 'hoptoad_notifier/capistrano'"
end
- unless options[:api_key].nil?
- m.template 'initializer.rb', 'config/initializers/hoptoad.rb',
- :assigns => {:api_key => options[:api_key]}
+ if options[:api_key]
+ if use_initializer?
+ m.template 'initializer.rb', 'config/initializers/hoptoad.rb',
+ :assigns => {:api_key => options[:api_key]}
+ else
+ m.template 'initializer.rb', 'config/hoptoad.rb',
+ :assigns => {:api_key => options[:api_key]}
+ m.append_to 'config/environment.rb', "require 'config/hoptoad'"
+ end
end
m.rake "hoptoad:test", :generate_only => true
end
end
+
+ def use_initializer?
+ Rails::VERSION::MAJOR > 1
+ end
+
+ def api_key_configured?
+ File.exists?('config/initializers/hoptoad.rb') ||
+ system("grep HoptoadNotifier config/environment.rb")
+ end
end
@@ -1,3 +1,6 @@
+<% if Rails::VERSION::MINOR < 2 -%>
+require 'hoptoad_notifier/rails'
+<% end -%>
HoptoadNotifier.configure do |config|
config.api_key = '<%= api_key %>'
end

0 comments on commit 1f87891

Please sign in to comment.