Skip to content
This repository has been archived by the owner on Nov 11, 2017. It is now read-only.

Commit

Permalink
Starting to add features for Rails 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Morrison authored and jferris committed Mar 23, 2010
1 parent 4ec9869 commit 912a441
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 23 deletions.
1 change: 1 addition & 0 deletions SUPPORTED_RAILS_VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
2.3.2
2.3.4
2.3.5
3.0.0.beta
19 changes: 10 additions & 9 deletions features/rails.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ Feature: Install the Gem in a Rails application
Background:
Given I have built and installed the "hoptoad_notifier" gem

@wip
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
And I run "script/generate hoptoad -k myapikey"
And I run the hoptoad generator with "-k myapikey"
Then I should receive a Hoptoad notification
And I should see the Rails version

Expand All @@ -16,7 +17,7 @@ Feature: Install the Gem in a Rails application
And I configure the Hoptoad shim
And I configure my application to require the "hoptoad_notifier" gem
And I unpack the "hoptoad_notifier" gem
And I run "script/generate hoptoad -k myapikey"
And I run the hoptoad generator with "-k myapikey"
And I uninstall the "hoptoad_notifier" gem
And I run "rake hoptoad:test"
Then I should receive two Hoptoad notifications
Expand All @@ -26,21 +27,21 @@ Feature: Install the Gem in a Rails application
And I configure the Hoptoad shim
And I configure the notifier to use "myapikey" as an API key
And I configure my application to require the "hoptoad_notifier" gem
And I run "script/generate hoptoad"
And I run the hoptoad generator with ""
Then I should receive a Hoptoad notification

Scenario: Try to install without an api key
When I generate a new Rails application
And I configure my application to require the "hoptoad_notifier" gem
And I run "script/generate hoptoad"
And I run the hoptoad generator with ""
Then I should see "Must pass --api-key or create config/initializers/hoptoad.rb"

Scenario: Configure and deploy using only installed gem
When I generate a new Rails application
And I run "capify ."
And I configure the Hoptoad shim
And I configure my application to require the "hoptoad_notifier" gem
And I run "script/generate hoptoad -k myapikey"
And I run the hoptoad generator with "-k myapikey"
And I run "cap -T"
Then I should see "deploy:notify_hoptoad"

Expand All @@ -50,7 +51,7 @@ Feature: Install the Gem in a Rails application
And I configure the Hoptoad shim
And I configure my application to require the "hoptoad_notifier" gem
And I unpack the "hoptoad_notifier" gem
And I run "script/generate hoptoad -k myapikey"
And I run the hoptoad generator with "-k myapikey"
And I uninstall the "hoptoad_notifier" gem
And I run "cap -T"
Then I should see "deploy:notify_hoptoad"
Expand All @@ -61,14 +62,14 @@ Feature: Install the Gem in a Rails application
And I configure the Hoptoad shim
And I configure the notifier to use "myapikey" as an API key
And I configure my application to require the "hoptoad_notifier" gem
And I run "script/generate hoptoad"
And I run the hoptoad generator with ""
Then I should see "You must first remove the hoptoad_notifier plugin. Please run: script/plugin remove hoptoad_notifier"

Scenario: Rescue an exception in a controller
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 "script/generate hoptoad -k myapikey"
And I run the hoptoad generator with "-k myapikey"
And I define a response for "TestController#index":
"""
session[:value] = "test"
Expand All @@ -88,7 +89,7 @@ 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 "script/generate hoptoad -k myapikey"
And I run the hoptoad generator with "-k myapikey"
And I run "rake gems"
Then I should see that "hoptoad_notifier" is not considered a framework gem

Expand Down
41 changes: 31 additions & 10 deletions features/step_definitions/rails_application_steps.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
When /^I generate a new Rails application$/ do
@terminal.cd(TEMP_DIR)
version_string = ENV['RAILS_VERSION'] ? "_#{ENV['RAILS_VERSION']}_" : ''
@terminal.run("rails #{version_string} rails_root")
version_string = ENV['RAILS_VERSION']

rails3 = version_string =~ /^3/

if rails3
rails_binary_gem = 'railties'
else
rails_binary_gem = 'rails'
end

load_rails = <<-RUBY
gem '#{rails_binary_gem}', '#{version_string}'; \
load Gem.bin_path('#{rails_binary_gem}', 'rails', '#{version_string}')
RUBY

@terminal.run(%{ruby -rubygems -e "#{load_rails.strip!}" rails_root})
if rails_root_exists?
@terminal.echo("Generated a Rails #{rails_version} application")
else
raise "Unable to generate a Rails application:\n#{@terminal.output}"
end
end

When /^I run the hoptoad generator with "([^\"]*)"$/ do |generator_args|
if rails3?
When %{I run "script/rails generate hoptoad #{generator_args}"}
else
When %{I run "script/generate hoptoad #{generator_args}"}
end
end

Given /^I have installed the "([^\"]*)" gem$/ do |gem_name|
@terminal.install_gem(gem_name)
end
Expand All @@ -19,14 +41,9 @@

When /^I configure my application to require the "([^\"]*)" gem$/ do |gem_name|
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
config_gem(gem_name)
elsif bundler_manages_gems?
bundle_gem(gem_name)
else
File.open(environment_path, 'a') do |file|
file.puts
Expand Down Expand Up @@ -54,6 +71,10 @@
end

When /^I configure the Hoptoad shim$/ do
if bundler_manages_gems?
bundle_gem("sham_rack")
end

shim_file = File.join(PROJECT_ROOT, 'features', 'support', 'hoptoad_shim.rb.template')
if rails_supports_initializers?
target = File.join(RAILS_ROOT, 'config', 'initializers', 'hoptoad_shim.rb')
Expand Down
43 changes: 39 additions & 4 deletions features/support/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,61 @@ def rails_root_exists?
File.exists?(environment_path)
end

def rails3?
rails_version =~ /^3/
end

def rails_version
environment_file = File.join(RAILS_ROOT, 'config', 'environment.rb')
@rails_version ||= `grep RAILS_GEM_VERSION #{environment_file}`.match(/[\d.]+/)[0]
@rails_version ||= begin
if bundler_manages_gems?
rails_version = open(gemfile_path).read.match(/gem.*rails".*"(.+)"/)[1]
else
environment_file = File.join(RAILS_ROOT, 'config', 'environment.rb')
rails_version = `grep RAILS_GEM_VERSION #{environment_file}`.match(/[\d.]+/)[0]
end
end
end

def bundler_manages_gems?
File.exists?(gemfile_path)
end

def gemfile_path
gemfile = File.join(RAILS_ROOT, 'Gemfile')
end

def rails_manages_gems?
rails_version =~ /^2\.[123]/
end

def rails_supports_initializers?
rails_version =~ /^2\./
rails3? || rails_version =~ /^2\./
end

def rails_finds_generators_in_gems?
rails_version =~ /^2\./
rails3? || rails_version =~ /^2\./
end

def environment_path
File.join(RAILS_ROOT, 'config', 'environment.rb')
end

def bundle_gem(gem_name)
File.open(gemfile_path, 'a') do |file|
file.puts("gem '#{gem_name}'")
end
end

def config_gem(gem_name)
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
end
end

World(RailsHelpers)

0 comments on commit 912a441

Please sign in to comment.