Skip to content

Commit

Permalink
refactor setup/teardown of test repos
Browse files Browse the repository at this point in the history
also fixed occasionally failing tmpdir test
  • Loading branch information
wuputah committed Feb 16, 2011
1 parent 2919d02 commit 53c1d44
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 135 deletions.
9 changes: 1 addition & 8 deletions test/integration/autobuild_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
require "integration_test_helper"

class AutobuildTest < ActionController::IntegrationTest
def setup
super
`cd test/files; mkdir repo; cd repo; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

def teardown
FileUtils.rm_rf("test/files/repo")
super
end
include WithTestRepo

test "if hook name is not found, 404 status is returned with appropriate description" do
post "/hooks/build/not_found_halp"
Expand Down
1 change: 0 additions & 1 deletion test/integration/bitbucket_autobuild_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def setup

def teardown
FileUtils.rm_rf("test/files/repo")
FileUtils.rm_rf("builds/*")
super
end

Expand Down
9 changes: 1 addition & 8 deletions test/integration/builds_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
require "integration_test_helper"

class BuildsTest < ActionController::IntegrationTest
def setup
super
@output = `cd test/files; mkdir repo; cd repo; git init; echo "my file" > file; git add file; git commit -m "my file added"; git log --pretty=format:%H --max-count=1`
end

def teardown
FileUtils.rm_rf("test/files/repo")
super
end
include WithTestRepo

test "one can delete build" do
project = project_with_steps({:name => "myproject", :vcs_source => "test/files/repo", :vcs_type => "git"}, "ls -al file")
Expand Down
9 changes: 1 addition & 8 deletions test/integration/hooks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ class Hooks::NoConfig
end

class HooksTest < ActionController::IntegrationTest
def setup
super
`cd test/files; mkdir repo; cd repo; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

def teardown
FileUtils.rm_rf("test/files/repo")
super
end
include WithTestRepo

test "hooks config renders hook config partial if it's present" do
project = project_with_steps({
Expand Down
9 changes: 1 addition & 8 deletions test/integration/projects_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
require "integration_test_helper"

class ProjectsTest < ActionController::IntegrationTest
def setup
super
`cd test/files; mkdir repo; cd repo; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

def teardown
FileUtils.rm_rf("test/files/repo")
super
end
include WithTestRepo

test "user can add a project" do
visit "/"
Expand Down
30 changes: 30 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,34 @@ def run_delayed_jobs
end
ran_jobs
end

def create_test_repo
command = <<-CMD.gsub("\n", "; ")
mkdir -p test/files/repo
cd test/files/repo
git init
git config user.name git
git config user.email git@example.com
echo "my file" > file
git add file
git commit -m "my file added"
CMD
`#{command}`
end

def destroy_test_repo
FileUtils.rm_rf 'test/files/repo'
end

module WithTestRepo
def setup
super
create_test_repo
end

def teardown
destroy_test_repo
super
end
end
end
43 changes: 18 additions & 25 deletions test/unit/build_test.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
require 'test_helper'

class BuildTest < ActiveSupport::TestCase
def setup
super
`cd test/files; mkdir koss; cd koss; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

def teardown
FileUtils.rm_rf("test/files/koss")
super
end
include WithTestRepo

test "Project.ajax_reload? method with ajax_reload => always" do
BigTuna.stubs(:ajax_reload).returns('always')

build = Build.new

build.status = Build::STATUS_IN_QUEUE
assert(build.ajax_reload?, "Should be true.")

build.status = Build::STATUS_PROGRESS
assert(build.ajax_reload?, "Should be true.")

build.status = Build::STATUS_OK
assert(build.ajax_reload?, "Should be true.")

Expand All @@ -37,15 +30,15 @@ def teardown

test "Project.ajax_reload? method with ajax_reload => building" do
BigTuna.stubs(:ajax_reload).returns('building')

build = Build.new

build.status = Build::STATUS_IN_QUEUE
assert(build.ajax_reload?, "Should be true.")

build.status = Build::STATUS_PROGRESS
assert(build.ajax_reload?, "Should be true.")

build.status = Build::STATUS_OK
assert(!build.ajax_reload?, "Should be false.")

Expand All @@ -58,18 +51,18 @@ def teardown
build.status = Build::STATUS_BUILDER_ERROR
assert(!build.ajax_reload?, "Should be false.")
end

test "Project.ajax_reload? method with ajax_reload => false" do
BigTuna.stubs(:ajax_reload).returns('false')

build = Build.new

build.status = Build::STATUS_IN_QUEUE
assert(!build.ajax_reload?, "Should be false.")

build.status = Build::STATUS_PROGRESS
assert(!build.ajax_reload?, "Should be false.")

build.status = Build::STATUS_OK
assert(!build.ajax_reload?, "Should be false.")

Expand All @@ -85,8 +78,8 @@ def teardown

test "invalid build is marked as invalid and failed count gets updated" do
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:name => "repo",
:vcs_source => "test/files/repo",
:max_builds => 1,
}, "ls /not/existing")
assert_equal 0, project.failed_builds
Expand All @@ -100,8 +93,8 @@ def teardown

test "special variable %build_dir% is available in steps" do
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:name => "repo",
:vcs_source => "test/files/repo",
:max_builds => 1,
}, "ls -al file\nls %build_dir%")
project.build!
Expand All @@ -113,8 +106,8 @@ def teardown

test "special variable %project_dir% is available in steps" do
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:name => "repo",
:vcs_source => "test/files/repo",
:max_builds => 1,
}, "ls -al file\nls %project_dir%")
project.build!
Expand All @@ -126,8 +119,8 @@ def teardown

test "if step produces white output then it should be set to nil" do
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:name => "repo",
:vcs_source => "test/files/repo",
:max_builds => 1,
}, "cd %project_dir%")
project.build!
Expand All @@ -139,8 +132,8 @@ def teardown

test "build #to_param includes build display name and project name" do
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:name => "repo",
:vcs_source => "test/files/repo",
:vcs_type => "git",
:max_builds => 2,
}, "ls .")
Expand Down
7 changes: 2 additions & 5 deletions test/unit/git_vcs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
if BigTuna::VCS::Git.supported?

class GitVCSTest < ActiveSupport::TestCase
def setup
super
`cd test/files; mkdir repo; cd repo; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

include WithTestRepo

def teardown
FileUtils.rm_rf("test/files/repo")
FileUtils.rm_rf("test/files/repo_clone")
super
end
Expand Down
17 changes: 5 additions & 12 deletions test/unit/hooks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@ def build_finished(build, config); raise "build_finished"; end
end

class HooksUnitTest < ActiveSupport::TestCase
def setup
super
`cd test/files; mkdir koss; cd koss; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

def teardown
FileUtils.rm_rf("test/files/koss")
super
end
include WithTestRepo

test "if hook produces error it is handled and marks build as hook failed" do
with_hook_enabled(BigTuna::Hooks::RaisingHook) do
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:name => "repo",
:vcs_source => "test/files/repo",
:max_builds => 2,
:hooks => {"raising_hook" => "raising_hook"},
}, "true")
Expand All @@ -39,8 +32,8 @@ def teardown
test "if hook is not enabled it won't get executed" do
with_hook_enabled(BigTuna::Hooks::RaisingHook) do
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:name => "repo",
:vcs_source => "test/files/repo",
:max_builds => 2,
:hooks => {"raising_hook" => "raising_hook"},
}, "true")
Expand Down
13 changes: 3 additions & 10 deletions test/unit/irc_hook_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
require 'test_helper'

class IrcHookTest < ActiveSupport::TestCase
def setup
super
`cd test/files; mkdir koss; cd koss; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

def teardown
FileUtils.rm_rf("test/files/koss")
super
end
include WithTestRepo

test "IRC message stating that build failed is sent when build failed" do
BigTuna::Hooks::Irc::Job.any_instance.expects(:perform).at_least_once.returns(true)
Expand Down Expand Up @@ -66,8 +59,8 @@ def teardown
private
def irc_project_with_steps(steps)
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:name => "repo",
:vcs_source => "test/files/repo",
:vcs_type => "git",
:max_builds => 2,
:hooks => {"irc" => "irc"},
Expand Down
13 changes: 3 additions & 10 deletions test/unit/mailer_hook_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
require 'test_helper'

class MailerHookTest < ActiveSupport::TestCase
def setup
super
`cd test/files; mkdir koss; cd koss; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

def teardown
FileUtils.rm_rf("test/files/koss")
super
end
include WithTestRepo

test "mail stating that build failed is sent when build failed" do
project = mailing_project_with_steps("ls invalid_file_here")
Expand Down Expand Up @@ -63,8 +56,8 @@ def teardown

def mailing_project_with_steps(steps)
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:name => "repo",
:vcs_source => "test/files/repo",
:max_builds => 2,
:hooks => {"mailer" => "mailer"},
}, steps)
Expand Down
13 changes: 3 additions & 10 deletions test/unit/notifo_hook_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
require 'test_helper'

class NotifoHookTest < ActiveSupport::TestCase
def setup
super
`cd test/files; mkdir koss; cd koss; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

def teardown
FileUtils.rm_rf("test/files/koss")
super
end
include WithTestRepo

test "notifo message stating that build failed is sent when build failed" do
BigTuna::Hooks::Notifo::Job.any_instance.expects(:perform).at_least_once.returns(true)
Expand Down Expand Up @@ -60,8 +53,8 @@ def teardown
private
def notifo_project_with_steps(steps)
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:name => "repo",
:vcs_source => "test/files/repo",
:vcs_type => "git",
:max_builds => 2,
:hooks => {"notifo" => "notifo"},
Expand Down
11 changes: 2 additions & 9 deletions test/unit/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@
require 'tmpdir'

class ProjectTest < ActiveSupport::TestCase
def setup
super
`cd test/files; mkdir repo; cd repo; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

def teardown
FileUtils.rm_rf("test/files/repo")
super
end
include WithTestRepo

def build_and_run_project_with_steps(steps = nil, project_attrs = {})
steps ||= "ls -al file"
Expand Down Expand Up @@ -313,7 +306,7 @@ def build_and_run_project_with_steps(steps = nil, project_attrs = {})
end

def assert_project_lifecycle(project)
assert_match Regexp.new(BigTuna.config[:build_dir]), project.build_dir
assert project.build_dir.include?(BigTuna.config[:build_dir])
assert File.exist?(project.build_dir)
assert_difference("Dir[File.join(BigTuna.build_dir, '*')].size", -1) do
project.destroy
Expand Down
Loading

0 comments on commit 53c1d44

Please sign in to comment.