Permalink
Browse files

Merge branch 'master' of git://github.com/thoughtworks/cruisecontrol.rb

  • Loading branch information...
mrduncan committed Sep 18, 2009
2 parents 0d09fc2 + 5f695bd commit 46bce7f506689967c256729b6f058d2ea7a3ee91
View
@@ -9,9 +9,25 @@ class << self
attr_accessor :current_project
def all(dir=CRUISE_DATA_ROOT + "/projects")
- Projects.new(dir).load_all
+ load_all(dir).map do |project_dir|
+ load_project project_dir
+ end
end
-
+
+ def create(project_name, scm, dir=CRUISE_DATA_ROOT + "/projects")
+ returning(Project.new(project_name, scm)) do |project|
+ raise "Project named #{project.name.inspect} already exists in #{dir}" if Project.all(dir).include?(project)
+ begin
+ save_project(project, dir)
+ checkout_local_copy(project)
+ write_config_example(project)
+ rescue
+ FileUtils.rm_rf "#{dir}/#{project.name}"
+ raise
+ end
+ end
+ end
+
def plugin(plugin_name)
self.plugin_names << plugin_name unless RAILS_ENV == 'test' or self.plugin_names.include? plugin_name
end
@@ -42,6 +58,32 @@ def load_project(dir)
project.path = dir
end
end
+
+ private
+
+ def load_all(dir)
+ Dir["#{dir}/*"].find_all {|child| File.directory?(child)}.sort
+ end
+
+ def save_project(project, dir)
+ project.path = File.join(dir, project.name)
+ FileUtils.mkdir_p project.path
+ end
+
+ def checkout_local_copy(project)
+ work_dir = File.join(project.path, 'work')
+ FileUtils.mkdir_p work_dir
+ project.source_control.checkout
+ end
+
+ def write_config_example(project)
+ config_example = File.join(RAILS_ROOT, 'config', 'cruise_config.rb.example')
+ config_in_subversion = File.join(project.path, 'work', 'cruise_config.rb')
+ cruise_config = File.join(project.path, 'cruise_config.rb')
+ if File.exists?(config_example) and not File.exists?(config_in_subversion)
+ FileUtils.cp(config_example, cruise_config)
+ end
+ end
end
def initialize(name, scm = nil)
View
@@ -1,57 +0,0 @@
-# Projects represents a list of Project objects. It is used internally by Cruise to keep track of
-# and instantiate all projects associated with this CC.rb instance.
-class Projects
-
- # Create a new project list with the given CRUISE_DATA_ROOT, /projects by default.
- def initialize(dir = CRUISE_DATA_ROOT + "/projects")
- @dir = dir
- @list = []
- end
-
- # Load all projects associated with this CC.rb instance by iterating through
- def load_all
- @list = Dir["#{@dir}/*"].find_all {|child| File.directory?(child)}.sort.
- collect {|child| Project.load_project(child)}
- self
- end
-
- def <<(project)
- raise "Project named #{project.name.inspect} already exists in #@dir" if @list.include?(project)
- begin
- @list << project
- save_project(project)
- checkout_local_copy(project)
- write_config_example(project)
- self
- rescue
- FileUtils.rm_rf "#{@dir}/#{project.name}"
- raise
- end
- end
-
- def save_project(project)
- project.path = File.join(@dir, project.name)
- FileUtils.mkdir_p project.path
- end
-
- def checkout_local_copy(project)
- work_dir = File.join(project.path, 'work')
- FileUtils.mkdir_p work_dir
- project.source_control.checkout
- end
-
- def write_config_example(project)
- config_example = File.join(RAILS_ROOT, 'config', 'cruise_config.rb.example')
- config_in_subversion = File.join(project.path, 'work', 'cruise_config.rb')
- cruise_config = File.join(project.path, 'cruise_config.rb')
- if File.exists?(config_example) and not File.exists?(config_in_subversion)
- FileUtils.cp(config_example, cruise_config)
- end
- end
-
- # delegate everything else to the underlying @list
- def method_missing(method, *args, &block)
- @list.send(method, *args, &block)
- end
-
-end
View
@@ -70,7 +70,7 @@ begin
source_control = SourceControl.create(scm_options)
- Project.all << Project.new(project_name, source_control)
+ Project.create(project_name, source_control)
if source_control.repository =~ /^svn\+ssh\:\/\//
puts "IMPORTANT!!! - It looks like you are connecting to your repository with an svn+ssh connection. " +
@@ -185,7 +185,7 @@ def test_build_should_request_build
project = create_project_stub('two')
Project.expects(:find).with('two').returns(project)
project.expects(:request_build)
- Project.stubs(:load_all).returns [ project ]
+ Project.stubs(:all).returns [ project ]
post :build, :id => "two"
assert_response :success
assert_equal 'two', assigns(:project).name
View
@@ -2,6 +2,7 @@
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
require 'mocha'
+require 'ostruct'
$LOAD_PATH << File.dirname(__FILE__)
require 'file_sandbox'
@@ -1,6 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
-require 'builds_controller'
-require "build"
class ApplicationHelperTest < Test::Unit::TestCase
@@ -1,5 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
-require 'build_status'
class BuildStatusTest < Test::Unit::TestCase
include FileSandbox
@@ -1,6 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
-require 'builder_starter'
-require 'project'
class BuilderStarterTest < Test::Unit::TestCase
include FileSandbox
@@ -1,5 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
-require 'fileutils'
class CommandLineTest < Test::Unit::TestCase
include FileSandbox
@@ -1,5 +1,5 @@
require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
-require 'build_reaper'
+
class BuildReaperTest < Test::Unit::TestCase
include FileSandbox
include BuildFactory
@@ -23,7 +23,7 @@ def setup
@notifier.emails = ["jeremystellsmith@gmail.com", "jeremy@thoughtworks.com"]
@notifier.from = 'cruisecontrol@thoughtworks.com'
- @project.add_plugin(@notifier)
+ @project.add_plugin(@notifier, :test_email_notifier)
end
def teardown
@@ -1,4 +1,3 @@
-require 'date'
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
class PollingSchedulerTest < Test::Unit::TestCase
@@ -1,4 +1,3 @@
-require 'date'
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
class ProjectConfigTrackerTest < Test::Unit::TestCase
View
@@ -1,8 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
-require 'date'
-require 'ostruct'
-require 'email_notifier'
-require 'fileutils'
class ProjectTest < ActiveSupport::TestCase
include FileSandbox
@@ -434,21 +430,19 @@ def test_build_requested
assert @project.build_requested?
end
- def test_build_should_generate_new_label_if_same_name_label_exists
+ def test_build_should_generate_new_label_if_same_name_label_exists
existing_build1 = stub_build('20')
existing_build2 = stub_build('20.1')
new_build = stub_build('20.2')
new_build_with_interesting_number = stub_build('2')
-
- builder_status = Object.new
- builder_status.stubs(:build_initiated).returns(true)
- BuilderStatus.expects(:new).returns(builder_status)
+
+ BuilderStatus.expects(:new).returns stub(:build_initiated => true)
project = Project.new('project1', @svn)
@svn.stubs(:update)
- project.stubs(:log_changeset)
+ project.stubs(:log_changeset)
project.stubs(:builds).returns([existing_build1, existing_build2])
- project.stubs(:last_build).returns(nil)
+ project.stubs(:last_build).returns(nil)
project.stubs(:new_revisions).returns(nil)
Build.expects(:new).with(project, '20.2').returns(new_build)
@@ -733,7 +727,7 @@ def test_project_all_should_always_reload_project_objects
end
end
- test "Projects.load_project should load the project in the given directory" do
+ test "Project.load_project should load the project in the given directory" do
in_sandbox do |sandbox|
sandbox.new :file => 'one/cruise_config.rb', :with_content => ''
@@ -744,7 +738,7 @@ def test_project_all_should_always_reload_project_objects
end
end
- test "Projects.load_project should load a project without any configuration" do
+ test "Project.load_project should load a project without any configuration" do
in_sandbox do |sandbox|
sandbox.new :directory => "myproject/work/.svn"
sandbox.new :directory => "myproject/builds-1"
@@ -757,6 +751,45 @@ def test_project_all_should_always_reload_project_objects
end
end
+ test "Project.create should add a new project" do
+ in_sandbox do |sandbox|
+ Project.create "one", @svn, sandbox.root
+ Project.create "two", @svn, sandbox.root
+ assert_equal %w(one two), Project.all(sandbox.root).map(&:name)
+ end
+ end
+
+ test "Project.create should check out an existing project" do
+ in_sandbox do |sandbox|
+ Project.create "one", @svn, sandbox.root
+ assert SandboxFile.new('one/work').exists?
+ assert SandboxFile.new('one/work/README').exists?
+ end
+ end
+
+ test "Project.create should clean up after itself if the source control throws an exception" do
+ in_sandbox do |sandbox|
+ @svn.expects(:checkout).raises("svn error")
+
+ assert_raise RuntimeError, 'svn error' do
+ Project.create "one", @svn, sandbox.root
+ end
+
+ assert_false SandboxFile.new('one/work').exists?
+ assert_false SandboxFile.new('one').exists?
+ end
+ end
+
+ test "Project.create should not allow you to add the same project twice" do
+ in_sandbox do |sandbox|
+ project = Project.create "one", @svn, sandbox.root
+ assert_raise RuntimeError, "Project named \"one\" already exists in #{sandbox.root}" do
+ Project.create "one", @svn, sandbox.root
+ end
+ assert File.directory?(project.path), "Project directory does not exist."
+ end
+ end
+
private
def stub_build(label)
View
@@ -1,73 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
-
-class ProjectsTest < ActiveSupport::TestCase
- include FileSandbox
-
- def setup
- @svn = FakeSourceControl.new("bob")
- @one = Project.new("one", @svn)
- @two = Project.new("two", @svn)
- end
-
- test "Projects#<< should add a new project" do
- in_sandbox do |sandbox|
- projects = Projects.new(sandbox.root)
- projects << @one << @two
-
- projects = Projects.new(sandbox.root)
- projects.load_all
-
- assert_equal %w(one two), projects.map(&:name)
- end
- end
-
- test "Projects#<< should check out an existing project" do
- in_sandbox do |sandbox|
- projects = Projects.new(sandbox.root)
-
- projects << @one
-
- assert SandboxFile.new('one/work').exists?
- assert SandboxFile.new('one/work/README').exists?
- end
- end
-
- test "Projects#<< should clean up after itself if the source control throws an exception" do
- in_sandbox do |sandbox|
- projects = Projects.new(sandbox.root)
- @svn.expects(:checkout).raises("svn error")
-
- assert_raise_with_message(RuntimeError, 'svn error') do
- projects << @one
- end
-
- assert_false SandboxFile.new('one/work').exists?
- assert_false SandboxFile.new('one').exists?
- end
- end
-
- test "Projects#<< should not allow you to add the same project twice" do
- in_sandbox do |sandbox|
- projects = Projects.new(sandbox.root)
- projects << @one
- assert_raise_with_message(RuntimeError, "Project named \"one\" already exists in #{sandbox.root}") do
- projects << @one
- end
- assert File.directory?(@one.path), "Project directory does not exist."
- end
- end
-
- test "Projects#each should allow enumeration over its project list" do
- in_sandbox do |sandbox|
- projects = Projects.new(sandbox.root)
- projects << @one << @two
-
- out = ""
- projects.each do |project|
- out << project.name
- end
-
- assert_equal("onetwo", out)
- end
- end
-end

0 comments on commit 46bce7f

Please sign in to comment.