Make sure test repositories are closed

Some repositories created during tests are not added to the 'toClose'
list in LocalDiskRepositoryTestCase. Therefore when the tests end
we may have open FileHandles and on Windows this may cause the
tests to fail because we can't delete those files.

This is fixed by adding the possibility to explicitly add
repositories to the list of repos which are closed automatically.

Change-Id: I1261baeef4c7d9aaedd7c34b546393bfa005bbcc
Signed-off-by: Christian Halstrick <christian.halstrick@sap.com>
This commit is contained in:
Christian Halstrick 2011-05-24 09:36:45 +02:00 committed by Shawn O. Pearce
parent cc2197ed9c
commit c1525e2aa5
5 changed files with 27 additions and 0 deletions

View File

@ -302,6 +302,17 @@ private FileRepository createRepository(boolean bare) throws IOException {
return db;
}
/**
* Adds a repository to the list of repositories which is closed at the end
* of the tests
*
* @param r
* the repository to be closed
*/
public void addRepoToClose(Repository r) {
toClose.add(r);
}
/**
* Creates a new unique directory for a test repository
*

View File

@ -101,6 +101,7 @@ public void testCloneRepository() {
command.setURI("file://"
+ git.getRepository().getWorkTree().getPath());
Git git2 = command.call();
addRepoToClose(git2.getRepository());
assertNotNull(git2);
ObjectId id = git2.getRepository().resolve("tag-for-blob");
assertNotNull(id);
@ -135,6 +136,8 @@ public void testCloneRepositoryWithBranch() {
command.setURI("file://"
+ git.getRepository().getWorkTree().getPath());
Git git2 = command.call();
addRepoToClose(git2.getRepository());
assertNotNull(git2);
assertEquals(git2.getRepository().getFullBranch(),
"refs/heads/master");
@ -152,6 +155,8 @@ public void testCloneRepositoryWithBranch() {
+ git.getRepository().getWorkTree().getPath());
command.setNoCheckout(true);
git2 = command.call();
addRepoToClose(git2.getRepository());
assertNotNull(git2);
assertEquals(git2.getRepository().getFullBranch(),
"refs/heads/master");
@ -169,6 +174,8 @@ public void testCloneRepositoryWithBranch() {
+ git.getRepository().getWorkTree().getPath());
command.setBare(true);
git2 = command.call();
addRepoToClose(git2.getRepository());
assertNotNull(git2);
assertEquals(git2.getRepository().getFullBranch(),
"refs/heads/master");
@ -191,6 +198,7 @@ public void testCloneRepositoryOnlyOneBranch() {
command.setURI("file://"
+ git.getRepository().getWorkTree().getPath());
Git git2 = command.call();
addRepoToClose(git2.getRepository());
assertNotNull(git2);
assertEquals(git2.getRepository().getFullBranch(),
"refs/heads/master");
@ -209,6 +217,7 @@ public void testCloneRepositoryOnlyOneBranch() {
+ git.getRepository().getWorkTree().getPath());
command.setBare(true);
git2 = command.call();
addRepoToClose(git2.getRepository());
assertNotNull(git2);
assertEquals(git2.getRepository().getFullBranch(),
"refs/heads/master");

View File

@ -71,6 +71,7 @@ public void setUp() throws Exception {
.setURI(db.getDirectory().toURI().toString())
.setDirectory(createUniqueTestGitDir(true)).call()
.getRepository();
addRepoToClose(bareRepo);
}
@Test

View File

@ -70,6 +70,7 @@ public void testInitRepository() {
InitCommand command = new InitCommand();
command.setDirectory(directory);
Repository repository = command.call().getRepository();
addRepoToClose(repository);
assertNotNull(repository);
} catch (Exception e) {
fail(e.getMessage());
@ -84,6 +85,7 @@ public void testInitBareRepository() {
command.setDirectory(directory);
command.setBare(true);
Repository repository = command.call().getRepository();
addRepoToClose(repository);
assertNotNull(repository);
assertTrue(repository.isBare());
} catch (Exception e) {

View File

@ -89,6 +89,8 @@ public void testLsRemote() {
+ git.getRepository().getWorkTree().getPath());
command.setCloneAllBranches(true);
Git git2 = command.call();
addRepoToClose(git2.getRepository());
LsRemoteCommand lsRemoteCommand = git2.lsRemote();
Collection<Ref> refs = lsRemoteCommand.call();
@ -109,6 +111,7 @@ public void testLsRemoteWithTags() {
+ git.getRepository().getWorkTree().getPath());
command.setCloneAllBranches(true);
Git git2 = command.call();
addRepoToClose(git2.getRepository());
LsRemoteCommand lsRemoteCommand = git2.lsRemote();
lsRemoteCommand.setTags(true);
@ -130,6 +133,7 @@ public void testLsRemoteWithHeads() {
+ git.getRepository().getWorkTree().getPath());
command.setCloneAllBranches(true);
Git git2 = command.call();
addRepoToClose(git2.getRepository());
LsRemoteCommand lsRemoteCommand = git2.lsRemote();
lsRemoteCommand.setHeads(true);