Skip to content

Commit

Permalink
All tested Projects functionality now in Project. Only (untested) loa…
Browse files Browse the repository at this point in the history
…d_all remains.
  • Loading branch information
bguthrie committed Aug 3, 2009
1 parent 8cdae2c commit 285bb89
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 109 deletions.
38 changes: 37 additions & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,21 @@ class << self
def all(dir=CRUISE_DATA_ROOT + "/projects")
Projects.new(dir).load_all
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
Expand Down Expand Up @@ -42,6 +56,28 @@ def load_project(dir)
project.path = dir
end
end

private

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)
Expand Down
34 changes: 0 additions & 34 deletions app/models/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,6 @@ def load_all
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)
Expand Down
2 changes: 1 addition & 1 deletion script/add_project
Original file line number Diff line number Diff line change
Expand Up @@ -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. " +
Expand Down
39 changes: 39 additions & 0 deletions test/unit/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,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_raises('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_raises("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)
Expand Down
73 changes: 0 additions & 73 deletions test/unit/projects_test.rb

This file was deleted.

0 comments on commit 285bb89

Please sign in to comment.