Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Re-organized capistrano task to allow for testing

  • Loading branch information...
commit ee3ea1b7c020b3877523b20b32f88e3d419dd90b 1 parent ca0ee34
David Czarnecki authored September 03, 2011
1  airbrake.gemspec
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24 24
   s.add_development_dependency("rspec",         "~> 2.6.0")
25 25
   s.add_development_dependency("sham_rack",     "~> 1.3.0")
26 26
   s.add_development_dependency("shoulda",       "~> 2.11.3")
  27
+  s.add_development_dependency("capistrano",    "~> 2.8.0")
27 28
 
28 29
   s.authors = ["thoughtbot, inc"]
29 30
   s.email   = %q{support@airbrakeapp.com}
41  lib/airbrake/capistrano.rb
... ...
@@ -1,21 +1,32 @@
1 1
 # Defines deploy:notify_airbrake which will send information about the deploy to Airbrake.
  2
+require 'capistrano'
2 3
 
3  
-Capistrano::Configuration.instance(:must_exist).load do
4  
-  after "deploy",            "deploy:notify_airbrake"
5  
-  after "deploy:migrations", "deploy:notify_airbrake"
  4
+module Airbrake
  5
+  module Capistrano
  6
+    def self.load_into(configuration)
  7
+      configuration.load do
  8
+        after "deploy",            "airbrake:notify"
  9
+        after "deploy:migrations", "airbrake:notify"
6 10
 
7  
-  namespace :deploy do
8  
-    desc "Notify Airbrake of the deployment"
9  
-    task :notify_airbrake, :except => { :no_release => true } do
10  
-      rails_env = fetch(:airbrake_env, fetch(:rails_env, "production"))
11  
-      local_user = ENV['USER'] || ENV['USERNAME']
12  
-      executable = RUBY_PLATFORM.downcase.include?('mswin') ? fetch(:rake, 'rake.bat') : fetch(:rake, 'rake')
13  
-      notify_command = "#{executable} airbrake:deploy TO=#{rails_env} REVISION=#{current_revision} REPO=#{repository} USER=#{local_user}"
14  
-      notify_command << " DRY_RUN=true" if dry_run
15  
-      notify_command << " API_KEY=#{ENV['API_KEY']}" if ENV['API_KEY']
16  
-      logger.info "Notifying Airbrake of Deploy (#{notify_command})"
17  
-      `#{notify_command}`
18  
-      logger.info "Airbrake Notification Complete."
  11
+        namespace :airbrake do
  12
+          desc "Notify Airbrake of the deployment"
  13
+          task :notify, :except => { :no_release => true } do
  14
+            rails_env = fetch(:airbrake_env, fetch(:rails_env, "production"))
  15
+            local_user = ENV['USER'] || ENV['USERNAME']
  16
+            executable = RUBY_PLATFORM.downcase.include?('mswin') ? fetch(:rake, 'rake.bat') : fetch(:rake, 'rake')
  17
+            notify_command = "#{executable} airbrake:deploy TO=#{rails_env} REVISION=#{current_revision} REPO=#{repository} USER=#{local_user}"
  18
+            notify_command << " DRY_RUN=true" if dry_run
  19
+            notify_command << " API_KEY=#{ENV['API_KEY']}" if ENV['API_KEY']
  20
+            logger.info "Notifying Airbrake of Deploy (#{notify_command})"
  21
+            `#{notify_command}` if !configuration.dry_run
  22
+            logger.info "Airbrake Notification Complete."
  23
+          end
  24
+        end
  25
+      end
19 26
     end
20 27
   end
21 28
 end
  29
+
  30
+if Capistrano::Configuration.instance
  31
+  Airbrake::Capistrano.load_into(Capistrano::Configuration.instance)
  32
+end
33  test/capistrano_test.rb
... ...
@@ -0,0 +1,33 @@
  1
+require File.dirname(__FILE__) + '/helper'
  2
+
  3
+require 'capistrano/configuration'
  4
+require 'airbrake/capistrano'
  5
+
  6
+class CapistranoTest < Test::Unit::TestCase
  7
+  def setup
  8
+    super
  9
+    reset_config
  10
+    
  11
+    @configuration = Capistrano::Configuration.new
  12
+    Airbrake::Capistrano.load_into(@configuration)
  13
+    @configuration.dry_run = true
  14
+  end
  15
+  
  16
+  should "define deploy:notify_airbrake task" do
  17
+    assert_not_nil @configuration.find_task('airbrake:notify')
  18
+  end
  19
+  
  20
+  should "log when calling deploy:notify_airbrake task" do
  21
+    @configuration.set(:current_revision, '084505b1c0e0bcf1526e673bb6ac99fbcb18aecc')
  22
+    @configuration.set(:repository, 'repository')
  23
+    io = StringIO.new
  24
+    logger = Capistrano::Logger.new(:output => io)
  25
+    logger.level = Capistrano::Logger::MAX_LEVEL
  26
+    
  27
+    @configuration.logger = logger
  28
+    @configuration.find_and_execute_task('airbrake:notify')
  29
+    
  30
+    assert io.string.include?('** Notifying Airbrake of Deploy')
  31
+    assert io.string.include?('** Airbrake Notification Complete')
  32
+  end
  33
+end

0 notes on commit ee3ea1b

Please sign in to comment.
Something went wrong with that request. Please try again.