Skip to content

Commit

Permalink
Updating rspec files.
Browse files Browse the repository at this point in the history
Updated the spec helper to use Minitar to unpack a test repo and link test
paths to it. This is a quick way to seed test data.

Refactored the repo spec for helper changes. All tests pass.
  • Loading branch information
rdblue committed Apr 8, 2012
1 parent 110ca3c commit c76557a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
test/test_repo
.*.swp
73 changes: 51 additions & 22 deletions spec/repo_spec.rb
Expand Up @@ -10,26 +10,44 @@
)
end

it "will complain about already existing repositories" do
path = link_test_repo
begin
expect { Svn::Repo.create(test_repo_path) }.to raise_error(
ArgumentError, /existing repository/
)
ensure
unlink_test_repo( path )
end
end

it "will not overwrite an existing path" do
expect { Svn::Repo.create(TMP_PATH) }.to raise_error(
Svn::DirectoryNotEmptyError, /exists/
)
path = temp_path('existing')
FileUtils.mkdir_p(temp_path('existing', 'content'))
begin
expect { Svn::Repo.create(path) }.to raise_error(
Svn::DirectoryNotEmptyError, /exists/
)
ensure
FileUtils.rm_rf(path)
end
end

it "complains about invalid paths" do
expect {
invalid_path = File.join( TMP_PATH, 'blah', 'blah', 'blah' )
invalid_path = temp_path( 'blah', 'blah', 'blah' )
Svn::Repo.create( invalid_path )
}.to( raise_error( Svn::PathNotFoundError ) )
}.to( raise_error( Svn::PathNotFoundError) )
end

it "can create a new repository" do
path = temp_path('new_repo')
repo = Svn::Repo.create( path )
begin
repo = Svn::Repo.create( test_repo_path )
repo.should be_a(Svn::Repo)
repo.null?.should be_false
ensure
remove_test_repo
FileUtils.rm_rf(path) if File.exists? path
end
end

Expand All @@ -45,35 +63,43 @@

it "complains about invalid paths" do
expect {
Svn::Repo.open( File.join( TMP_PATH, 'blah', 'blah' ) )
Svn::Repo.open( temp_path( 'blah', 'blah' ) )
}.to( raise_error( Svn::PathNotFoundError ) )
end

it "complains about paths inside the repository" do
expect {
Svn::Repo.open( File.join( test_repo_path, 'trunk', 'blah' ) )
}.to( raise_error( Svn::PathNotFoundError ) )
path = link_test_repo
begin
expect {
Svn::Repo.open( File.join( test_repo_path, 'trunk', 'blah' ) )
}.to( raise_error( Svn::PathNotFoundError ) )
ensure
unlink_test_repo(path)
end
end

it "can open an existing repository" do
create_test_repo

repo = Svn::Repo.open(test_repo_path)
repo.should be_a(Svn::Repo)
repo.null?.should be_false
path = link_test_repo
begin
repo = Svn::Repo.open(path)
repo.should be_a(Svn::Repo)
repo.null?.should be_false
ensure
unlink_test_repo( path )
end
end

end

context "#revision" do

before do
create_test_repo
@repo = open_test_repo
@path = link_test_repo
@repo = open_test( @path )
end

after do
remove_test_repo
unlink_test_repo( @path )
end

it "complains about invalid revision numbers" do
Expand All @@ -86,19 +112,22 @@
rev = @repo.revision(0)
rev.should be_a( Svn::Revision )
rev.null?.should be_false
rev = @repo.revision(1)
rev.should be_a( Svn::Revision )
rev.null?.should be_false
end

end

context "#youngest" do

before do
create_test_repo
@repo = open_test_repo
@path = link_test_repo
@repo = open_test( @path )
end

after do
remove_test_repo
unlink_test_repo( @path )
end

it "returns a revision" do
Expand Down
56 changes: 48 additions & 8 deletions spec/spec_helper.rb
Expand Up @@ -17,25 +17,65 @@
# print error codes/classes that are dynamically generated to stderr.
$debug_svn_errors = true

# create and destroy the test repo
TMP_PATH = '/tmp'
TEST_REPO = File.join( TMP_PATH, 'ruby_svn_test_repo' )

# manage the test repo
require 'tempfile'
require 'fileutils'
require 'zlib'
require 'archive/tar/minitar'

class Counter
def initialize
@num = 0
end

def next
@num += 1
@num
end
end

TEMP_PATH_BASE = File.join( Dir.tmpdir, "svn-#{Process.pid}" )
TEST_REPO_TARBALL = File.join( File.dirname(__FILE__), '..', 'test', 'test_repo.tar.gz' )
TEST_COUNTER = Counter.new

ObjectSpace.define_finalizer(TEMP_PATH_BASE) do
FileUtils.rm_rf( File.join( Dir.tmpdir, "svn-#{Process.pid}" ) )
end

def temp_path( *parts )
File.join( TEMP_PATH_BASE, *parts )
end

def test_repo_path
TEST_REPO
temp_path( 'test_repo' )
end

# unpacks a fresh copy of the test repo tarball
def unpack_test_repo
FileUtils.mkdir_p(TEMP_PATH_BASE)
gzip_stream = Zlib::GzipReader.new(File.open(TEST_REPO_TARBALL))
Archive::Tar::Minitar.unpack(gzip_stream, temp_path)
end

def create_test_repo
Svn::Repo.create( test_repo_path )
def link_test_repo
name = "test-#{TEST_COUNTER.next}"
link_path = temp_path(name)
FileUtils.ln_s(test_repo_path, link_path)
link_path
end

def open_test_repo
def unlink_test_repo( path )
FileUtils.rm( path )
end

def open_test( name )
Svn::Repo.open( test_repo_path )
end

def remove_test_repo
# clean up the temporary repository, if it is present
FileUtils.rm_rf test_repo_path if File.exists? test_repo_path
end

# make sure the test repo exists for all specs
unpack_test_repo
Binary file added test/test_repo.tar.gz
Binary file not shown.

0 comments on commit c76557a

Please sign in to comment.