Skip to content
This repository has been archived by the owner on Sep 29, 2020. It is now read-only.

Commit

Permalink
Initial commit to Bob::Test
Browse files Browse the repository at this point in the history
We're duplicating our SCM and Buildable test helpers
(each with a different API) accross three projects:
Integrity, Bob and Bobette.

I figured it was time to extract.
  • Loading branch information
sr committed Jun 1, 2009
0 parents commit 4c8296e
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/bob/test.rb
@@ -0,0 +1,4 @@
require "bob"
require "bob/test/buildable_stub"
require "bob/test/scm/git"
require "bob/test/scm/svn"
38 changes: 38 additions & 0 deletions lib/bob/test/buildable_stub.rb
@@ -0,0 +1,38 @@
module Bob::Test
BuildableStub = Struct.new(:kind, :uri, :branch, :build_script) do
include Bob::Buildable

attr_reader :repo, :builds, :metadata

def self.from(repo)
kind = repo.class == Bob::Test::GitRepo ? :git : :svn
# same
uri =
if kind == :git
repo.path
else
"file://#{SvnRepo.server_root}/#{repo.name}"
end
# move to repo
branch = (kind == :git) ? "master" : ""
build_script = "./test"

new(kind, uri, branch, build_script)
end

def initialize(*args)
super

@builds = {}
@metadata = {}
end

def start_building(commit_id, commit_info)
@metadata[commit_id] = commit_info
end

def finish_building(commit_id, status, output)
@builds[commit_id] = [status ? :successful : :failed, output]
end
end
end
50 changes: 50 additions & 0 deletions lib/bob/test/scm/abstract.rb
@@ -0,0 +1,50 @@
module Bob::Test
class AbstractSCMRepo
attr_reader :path, :name

def initialize(name, base_dir=Bob.directory)
@name = name
@path = File.join(base_dir, @name.to_s)
end

def add_commit(message, &action)
Dir.chdir(@path) do
yield action
commit(message)
end
end

def add_failing_commit
add_commit "This commit will fail" do
system "echo '#{build_script(false)}' > test"
system "chmod +x test"
add "test &>/dev/null"
end
end

def add_successful_commit
add_commit "This commit will work" do
system "echo '#{build_script(true)}' > test"
system "chmod +x test"
add "test"
end
end

protected
def add(file)
raise NotImplementedError
end

def commit(message)
raise NotImplementedError
end

def build_script(successful=true)
<<-script
#!/bin/sh
echo "Running tests..."
exit #{successful ? 0 : 1}
script
end
end
end
52 changes: 52 additions & 0 deletions lib/bob/test/scm/git.rb
@@ -0,0 +1,52 @@
require File.dirname(__FILE__) + "/abstract"

module Bob::Test
class GitRepo < AbstractSCMRepo
def create
FileUtils.mkdir_p @path

Dir.chdir(@path) do
system 'git init &>/dev/null'
system 'git config user.name "John Doe"'
system 'git config user.email "johndoe@example.org"'
system 'echo "just a test repo" >> README'
add 'README &>/dev/null'
commit "First commit"
end
end

def destroy
FileUtils.rm_rf @path
end

def commits
Dir.chdir(@path) do
commits = `git log --pretty=oneline`.collect { |l| l.split(" ").first }
commits.inject([]) do |commits, sha1|
format = "---%n:message: >-%n %s%n:timestamp: %ci%n" +
":identifier: %H%n:author: %n :name: %an%n :email: %ae%n"
commits << YAML.load(`git show -s --pretty=format:"#{format}" #{sha1}`)
end.reverse
end
end

def head
Dir.chdir(@path) do
`git log --pretty=format:%H | head -1`.chomp
end
end

def short_head
head[0..6]
end

protected
def add(file)
system "git add #{file}"
end

def commit(message)
system %Q{git commit -m "#{message}" &>/dev/null}
end
end
end
76 changes: 76 additions & 0 deletions lib/bob/test/scm/svn.rb
@@ -0,0 +1,76 @@
require File.dirname(__FILE__) + "/abstract"
require "hpricot"

module Bob::Test
class SvnRepo < AbstractSCMRepo
def self.server_root
@root ||= File.join(Bob.directory, "svn")
end

attr_reader :remote

def initialize(name, base_dir=Bob.directory)
super

@path = File.join(base_dir, "svn-#{name}")
@remote = File.join(SvnRepo.server_root, name.to_s)
end

def create
create_remote

system "svn checkout file://#{remote} #{path} &>/dev/null"

add_commit("First commit") do
system "echo 'just a test repo' >> README"
add "README"
end
end

def destroy
FileUtils.rm_r(@remote) if File.directory?(@remote)
FileUtils.rm_r(@path) if File.directory?(@path)
end

def commits
Dir.chdir(path) do
doc = Hpricot::XML(`svn log --xml`)

(doc/:log/:logentry).inject([]) { |commits, commit|
commits << { :identifier => commit["revision"],
:message => commit.at("msg").inner_html,
:committed_at => Time.parse(commit.at("date").inner_html) }
}.reverse
end
end

def head
commits.last[:identifier]
end

alias_method :short_head, :head

protected
def add(file)
system "svn add #{file} &>/dev/null"
end

def commit(message)
system %Q{svn commit -m "#{message}" &>/dev/null}
system "svn up &>/dev/null"
end

private
def create_remote
FileUtils.mkdir_p(SvnRepo.server_root)

system "svnadmin create #{remote} &>/dev/null"

File.open(File.join(remote, "conf", "svnserve.conf"), "w") { |f|
f.puts "[general]"
f.puts "anon-access = write"
f.puts "auth-access = write"
}
end
end
end
67 changes: 67 additions & 0 deletions test/bob_test_test.rb
@@ -0,0 +1,67 @@
require "test/unit"
require "bob/test"

class BobTestTest < Test::Unit::TestCase
include Bob::Test

def assert_scm_repo(repo)
repo.destroy
repo.create

assert_equal 1, repo.commits.size
assert_equal "First commit", repo.commits.first[:message]

repo.add_failing_commit
assert_equal 2, repo.commits.size
assert_equal "This commit will fail", repo.commits.last[:message]
assert_equal repo.commits.last[:identifier], repo.head
assert repo.short_head

repo.add_successful_commit
assert_equal 3, repo.commits.size
assert_equal "This commit will work", repo.commits.last[:message]
assert_equal repo.commits.last[:identifier], repo.head
end

def test_buildable_stub
b = BuildableStub.new(:git, "git://github.com/ry/node", "master", "make")

assert_equal :git, b.kind
assert_equal "git://github.com/ry/node", b.uri
assert_equal "master", b.branch
assert_equal "make", b.build_script
end

def test_scm_repo
assert_scm_repo(GitRepo.new(:my_test_project))
assert_scm_repo(SvnRepo.new(:my_test_project))
end

def test_buildable_git_repo
Bob.directory = "/tmp/bob-git"

repo = GitRepo.new(:test_repo)
repo.destroy
repo.create

b = BuildableStub.from(repo)
assert_equal :git, b.kind
assert_equal "/tmp/bob-git/test_repo", b.uri
assert_equal "master", b.branch
assert_equal "./test", b.build_script
end

def test_buildable_svn_repo
Bob.directory = "/tmp/bob-svn"

repo = SvnRepo.new(:test_repo)
repo.destroy
repo.create

b = BuildableStub.from(repo)
assert_equal :svn, b.kind
assert_equal "", b.branch
assert_equal "./test", b.build_script
assert_equal "file:///tmp/bob-svn/svn/test_repo", b.uri
end
end

0 comments on commit 4c8296e

Please sign in to comment.