Permalink
Browse files

Move Campfire to be it's own class, no need to do wonky class level m…

…ethods, it causes issues with multiple joes.
  • Loading branch information...
1 parent 0350689 commit 18a40fa2057153729407c3cc6c825e97b59cc343 @queso queso committed Feb 19, 2011
Showing with 74 additions and 26 deletions.
  1. +4 −2 lib/cijoe.rb
  2. +22 −22 lib/cijoe/campfire.rb
  3. +0 −2 lib/cijoe/server.rb
  4. +48 −0 test/test_campfire.rb
View
@@ -21,14 +21,16 @@
require 'cijoe/server'
class CIJoe
- attr_reader :user, :project, :url, :current_build, :last_build
+ attr_reader :user, :project, :url, :current_build, :last_build, :campfire
def initialize(project_path)
@project_path = File.expand_path(project_path)
@user, @project = git_user_and_project
@url = "http://github.com/#{@user}/#{@project}"
+ @campfire = CIJoe::Campfire.new(project_path)
+
@last_build = nil
@current_build = nil
@@ -77,7 +79,7 @@ def finish_build(status, output)
@current_build = nil
write_build 'current', @current_build
write_build 'last', @last_build
- @last_build.notify if @last_build.respond_to? :notify
+ @campfire.notify if @campfire.valid?
# another build waits
if !repo_config.buildallfile.to_s.empty? && File.exist?(repo_config.buildallfile.to_s)
View
@@ -1,53 +1,53 @@
class CIJoe
- module Campfire
- def self.activate(project_path)
+ class Campfire
+ attr_reader :project_path
+
+ def initialize(project_path)
@project_path = project_path
-
- if valid_config?
+ if valid?
require 'tinder'
-
- CIJoe::Build.class_eval do
- include CIJoe::Campfire
- end
-
- puts "Loaded Campfire notifier"
+ puts "Loaded Campfire notifier for #{project_path}."
elsif ENV['RACK_ENV'] != 'test'
- puts "Can't load Campfire notifier."
+ puts "Can't load Campfire notifier for #{project_path}."
puts "Please add the following to your project's .git/config:"
puts "[campfire]"
- puts "\ttoken = your_api_token"
+ puts "\ttoken = abcd1234"
puts "\tsubdomain = whatever"
puts "\troom = Awesomeness"
puts "\tssl = false"
end
end
-
- def self.config
- campfire_config = Config.new('campfire', @project_path)
- @config ||= {
+
+ def campfire_config
+ campfire_config = Config.new('campfire', project_path)
+ @config = {
:subdomain => campfire_config.subdomain.to_s,
:token => campfire_config.token.to_s,
:room => campfire_config.room.to_s,
:ssl => campfire_config.ssl.to_s.strip == 'true'
}
end
- def self.valid_config?
+ def valid?
%w( subdomain token room ).all? do |key|
- !config[key.intern].empty?
+ !campfire_config[key.intern].empty?
end
end
def notify
- room.speak "#{short_message}. #{commit.url}"
- room.paste full_message if failed?
- room.leave
+ begin
+ room.speak "#{short_message}. #{commit.url}"
+ room.paste full_message if failed?
+ room.leave
+ rescue
+ puts "Please check your campfire config for #{project_path}."
+ end
end
private
def room
@room ||= begin
- config = Campfire.config
+ config = campfire_config
campfire = Tinder::Campfire.new(config[:subdomain],
:token => config[:token],
:ssl => config[:ssl] || false)
View
@@ -80,8 +80,6 @@ def initialize(*args)
super
check_project
@joe = CIJoe.new(options.project_path)
-
- CIJoe::Campfire.activate(options.project_path)
end
def self.start(host, port, project_path)
View
@@ -0,0 +1,48 @@
+require "helper"
+require "cijoe"
+require "fakefs/safe"
+
+
+class TestCampfire < Test::Unit::TestCase
+
+ class ::CIJoe
+ attr_writer :current_build, :last_build
+ end
+
+ attr_accessor :app
+
+ def setup
+ @app = CIJoe::Server.new
+ joe = @app.joe
+
+ # make Build#restore a no-op so we don't overwrite our current/last
+ # build attributes set from tests.
+ def joe.restore
+ end
+
+ # make CIJoe#build! and CIJoe#git_update a no-op so we don't overwrite our local changes
+ # or local commits nor should we run tests.
+ def joe.build!
+ end
+ end
+
+ def teardown
+ teardown_git_info
+ end
+
+ def test_campfire_pulls_campfire_config_from_git_config
+ setup_git_info(:config => {"campfire.subdomain" => "github", "remote.origin.url" => "https://github.com/defunkt/cijoe.git"})
+ cf = CIJoe::Campfire.new(tmp_dir)
+ assert_equal "github", cf.campfire_config[:subdomain]
+ end
+
+ def test_campfire_pulls_campfire_config_from_its_own_git_config
+ setup_git_info(:config => {"campfire.subdomain" => "github"})
+ setup_git_info(:config => {"campfire.subdomain" => "37signals"}, :tmp_dir => "/tmp/cijoe_test_37signals")
+ cf1 = CIJoe::Campfire.new(tmp_dir)
+ cf2 = CIJoe::Campfire.new("/tmp/cijoe_test_37signals")
+ assert_equal "github", cf1.campfire_config[:subdomain]
+ assert_equal "37signals", cf2.campfire_config[:subdomain]
+ end
+
+end

0 comments on commit 18a40fa

Please sign in to comment.