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

Commit

Permalink
Added a test for the installation process
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jan 19, 2010
1 parent bf486bb commit 7df97d7
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@ tags
.yardoc .yardoc
doc doc
pkg pkg
*.gemspec
32 changes: 28 additions & 4 deletions Rakefile
Expand Up @@ -2,9 +2,10 @@ require 'rake'
require 'rake/testtask' require 'rake/testtask'
require 'rake/rdoctask' require 'rake/rdoctask'
require 'rake/gempackagetask' require 'rake/gempackagetask'
require 'cucumber/rake/task'


desc 'Default: run unit tests.' desc 'Default: run unit tests.'
task :default => :test task :default => [:test, :cucumber]


desc 'Test the hoptoad_notifier gem.' desc 'Test the hoptoad_notifier gem.'
Rake::TestTask.new(:test) do |t| Rake::TestTask.new(:test) do |t|
Expand All @@ -29,8 +30,8 @@ begin
rescue LoadError rescue LoadError
end end


PLUGIN_ROOT = File.dirname(__FILE__).freeze GEM_ROOT = File.dirname(__FILE__).freeze
VERSION_FILE = File.join(PLUGIN_ROOT, 'lib', 'hoptoad_notifier', 'version') VERSION_FILE = File.join(GEM_ROOT, 'lib', 'hoptoad_notifier', 'version')


require VERSION_FILE require VERSION_FILE


Expand All @@ -40,7 +41,7 @@ gemspec = Gem::Specification.new do |s|
s.summary = %q{Send your application errors to our hosted service and reclaim your inbox.} s.summary = %q{Send your application errors to our hosted service and reclaim your inbox.}


s.files = FileList['[A-Z]*', 'generators/**/*.*', 'lib/**/*.rb', s.files = FileList['[A-Z]*', 'generators/**/*.*', 'lib/**/*.rb',
'test/**/*.rb', 'rails/**.*.rb', 'recipes/**/*.rb', 'test/**/*.rb', 'rails/**/*.rb', 'recipes/**/*.rb',
'tasks/**/*.rake'] 'tasks/**/*.rake']
s.require_path = 'lib' s.require_path = 'lib'
s.test_files = Dir[*['test/**/*_test.rb']] s.test_files = Dir[*['test/**/*_test.rb']]
Expand Down Expand Up @@ -71,3 +72,26 @@ task :gemspec do
end end
end end


LOCAL_GEM_ROOT = File.join(GEM_ROOT, 'tmp', 'local_gems').freeze
LOCAL_GEMS = %w(rails sham_rack)

task :vendor_test_gems do
LOCAL_GEMS.each do |gem_name|
pattern = File.join(LOCAL_GEM_ROOT, 'gems', "#{gem_name}-*")
existing = Dir.glob(pattern).first
unless existing
command = "gem install -i #{LOCAL_GEM_ROOT} --no-ri --no-rdoc #{gem_name}"
puts "Vendoring #{gem_name}..."
unless system(command)
$stderr.puts "Command failed: #{command}"
end
end
end
end

Cucumber::Rake::Task.new(:cucumber) do |t|
t.fork = true
t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
end

task :cucumber => [:gemspec, :vendor_test_gems]
12 changes: 12 additions & 0 deletions features/rails.feature
@@ -0,0 +1,12 @@
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
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 "rake hoptoad:test --trace"
Then I should receive a Hoptoad notification
39 changes: 39 additions & 0 deletions features/step_definitions/rails_application_steps.rb
@@ -0,0 +1,39 @@
When /^I generate a new Rails application$/ do
@terminal.cd(TEMP_DIR)
@terminal.run("rails rails_root")
end

Given /^I have installed the "([^\"]*)" gem$/ do |gem_name|
@terminal.install_gem(gem_name)
end

Given /^I have built and installed the "([^\"]*)" gem$/ do |gem_name|
@terminal.build_and_install_gem(File.join(PROJECT_ROOT, "#{gem_name}.gemspec"))
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) }
else
raise "Couldn't find #{run.inspect} in #{path}"
end
end

When /^I run "([^\"]*)"$/ do |command|
@terminal.cd(RAILS_ROOT)
@terminal.run(command)
end

Then /^I should receive a Hoptoad notification$/ do
@terminal.output.should include("[Hoptoad] Success: Net::HTTPOK")
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)
end
12 changes: 12 additions & 0 deletions features/support/env.rb
@@ -0,0 +1,12 @@
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..')).freeze
TEMP_DIR = File.join(PROJECT_ROOT, 'tmp').freeze
RAILS_ROOT = File.join(TEMP_DIR, 'rails_root').freeze
BUILT_GEM_ROOT = File.join(TEMP_DIR, 'built_gems').freeze
LOCAL_GEM_ROOT = File.join(TEMP_DIR, 'local_gems').freeze

Before do
FileUtils.mkdir_p(TEMP_DIR)
FileUtils.rm_rf(BUILT_GEM_ROOT)
FileUtils.rm_rf(RAILS_ROOT)
FileUtils.mkdir_p(BUILT_GEM_ROOT)
end
5 changes: 5 additions & 0 deletions features/support/hoptoad_shim.rb.template
@@ -0,0 +1,5 @@
require 'sham_rack'

ShamRack.at("hoptoadapp.com") do |env|
["200 OK", { "Content-type" => "text/xml" }, "<notice/>"]
end
51 changes: 51 additions & 0 deletions features/support/terminal.rb
@@ -0,0 +1,51 @@
Before do
@terminal = Terminal.new
end

class Terminal

attr_reader :output, :status

def initialize
@cwd = FileUtils.pwd
@output = ""
@status = 0
end

def cd(directory)
@cwd = directory
end

def run(command)
output << "#{command}\n"
FileUtils.cd(@cwd) do
result = `#{environment_settings} #{command} 2>&1`
output << result
end
@status = $?
end

def build_and_install_gem(gemspec)
pkg_dir = File.join(TEMP_DIR, 'pkg')
FileUtils.mkdir_p(pkg_dir)
`gem build #{gemspec} 2>&1`
gem_file = Dir.glob("*.gem").first
target = File.join(pkg_dir, gem_file)
FileUtils.mv(gem_file, target)
install_gem_to(BUILT_GEM_ROOT, target)
end

def install_gem(gem)
install_gem_to(LOCAL_GEM_ROOT, gem)
end

private

def install_gem_to(root, gem)
`gem install -i #{root} --no-ri --no-rdoc #{gem}`
end

def environment_settings
"GEM_HOME=#{LOCAL_GEM_ROOT} GEM_PATH=#{LOCAL_GEM_ROOT}:#{BUILT_GEM_ROOT}"
end
end
8 changes: 4 additions & 4 deletions generators/hoptoad/hoptoad_generator.rb
Expand Up @@ -14,14 +14,14 @@ def manifest
record do |m| record do |m|
m.directory 'lib/tasks' m.directory 'lib/tasks'
m.file 'hoptoad_notifier_tasks.rake', 'lib/tasks/hoptoad_notifier_tasks.rake' m.file 'hoptoad_notifier_tasks.rake', 'lib/tasks/hoptoad_notifier_tasks.rake'
if File.exists?('config/deploy.rb') # if File.exists?('config/deploy.rb')
m.insert_into 'config/deploy.rb', "require 'hoptoad_notifier/recipes/hoptoad'" # m.insert_into 'config/deploy.rb', "require 'hoptoad_notifier/recipes/hoptoad'"
end # end
unless options[:api_key].nil? unless options[:api_key].nil?
m.template 'initializer.rb', 'config/initializers/hoptoad.rb', m.template 'initializer.rb', 'config/initializers/hoptoad.rb',
:assigns => {:api_key => options[:api_key]} :assigns => {:api_key => options[:api_key]}
end end
m.rake "hoptoad:test", :generate_only => true # m.rake "hoptoad:test", :generate_only => true
end end
end end
end end
1 change: 0 additions & 1 deletion init.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/hoptoad_notifier.rb
Expand Up @@ -2,6 +2,7 @@
require 'net/https' require 'net/https'
require 'rubygems' require 'rubygems'
require 'active_support' require 'active_support'
require 'hoptoad_notifier/version'
require 'hoptoad_notifier/configuration' require 'hoptoad_notifier/configuration'
require 'hoptoad_notifier/notice' require 'hoptoad_notifier/notice'
require 'hoptoad_notifier/sender' require 'hoptoad_notifier/sender'
Expand Down
11 changes: 11 additions & 0 deletions lib/hoptoad_notifier/rails.rb
@@ -0,0 +1,11 @@
if defined?(ActionController::Base) && !ActionController::Base.include?(HoptoadNotifier::Catcher)
ActionController::Base.send(:include, HoptoadNotifier::Catcher)
end

require 'hoptoad_notifier/rails_initializer'
HoptoadNotifier::RailsInitializer.initialize

HoptoadNotifier.configure(true) do |config|
config.environment_name = RAILS_ENV
config.project_root = RAILS_ROOT
end
12 changes: 1 addition & 11 deletions rails/init.rb
@@ -1,11 +1 @@
if defined?(ActionController::Base) && !ActionController::Base.include?(HoptoadNotifier::Catcher) require 'hoptoad_notifier/rails'
ActionController::Base.send(:include, HoptoadNotifier::Catcher)
end

require File.join(File.dirname(__FILE__), '..', 'lib', 'hoptoad_notifier', 'rails_initializer')
HoptoadNotifier::RailsInitializer.initialize

HoptoadNotifier.configure(true) do |config|
config.environment_name = RAILS_ENV
config.project_root = RAILS_ROOT
end

0 comments on commit 7df97d7

Please sign in to comment.