git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 4/4] Automatically clean up any repositories created by the test cases

From
Robin Rosenberg <robin.rosenberg@dewire.com>
Date
Nov 27, 2008, 21:15 UTC
Message-ID
<1227820535-9785-5-git-send-email-robin.rosenberg@dewire.com>
In-Reply-To
<1227820535-9785-4-git-send-email-robin.rosenberg@dewire.com>
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../org/spearce/jgit/lib/RepositoryTestCase.java   |   21 +++++++++++++++++++-
 1 files changed, 20 insertions(+), 1 deletions(-)
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
index ef4fd1b..cab65a0 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
@@ -45,6 +45,7 @@
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
+import java.util.ArrayList;
 
 import junit.framework.TestCase;
 import org.spearce.jgit.util.JGitTestUtil;
@@ -145,6 +146,8 @@ protected static void checkFile(File f, final String checkData)
 	private static int testcount;
 	private static Thread shutdownhook;
 
+	private ArrayList<Repository> repositoriesToClose = new ArrayList<Repository>();
+
 	public void setUp() throws Exception {
 		super.setUp();
 		System.gc();
@@ -192,6 +195,20 @@ copyFile(JGitTestUtil.getTestResourceFile(packs[k] + ".idx"), new File(packDir,
 
 	protected void tearDown() throws Exception {
 		db.close();
+		for (Repository r : repositoriesToClose) {
+			r.close();
+		}
+		// Since memory mapping is controlled by the GC we need to
+		// tell it this is a good time to clean up and unlock
+		// mmemory mapped files.
+		System.gc();
+
+		recursiveDelete(trash, false, getName());
+		for (Repository r : repositoriesToClose) {
+			recursiveDelete(r.getWorkDir(), false, getName());
+		}
+		repositoriesToClose.clear();
+
 		super.tearDown();
 	}
 
@@ -209,12 +226,14 @@ protected Repository createNewEmptyRepo() throws IOException {
 		assertTrue(unusedDir.mkdirs());
 		final Repository newRepo = new Repository(newTestRepo);
 		newRepo.create();
+		final String name = getClass().getName() + "." + getName();
 		Runtime.getRuntime().addShutdownHook(new Thread() {
 			@Override
 			public void run() {
-				recursiveDelete(newTestRepo);
+				recursiveDelete(newTestRepo, false, name);
 			}
 		});
+		repositoriesToClose.add(newRepo);
 		return newRepo;
 	}
 
-- 
1.6.0.3.640.g6331a
Previous: Robin Rosenberg
Message 5 of 5 in “RepositoryTestCase cleanups”
  1. 0/4 RepositoryTestCase cleanupsRobin Rosenberg, Nov 27, 2008
  2. 1/4 Make the cleanup less verbose when it fails to delete temporary stuff.Robin Rosenberg, Nov 27, 2008
  3. 2/4 Add shutdown hooks to try to clean up after unit tests anywayRobin Rosenberg, Nov 27, 2008
  4. 3/4 Cleanup malformed test casesRobin Rosenberg, Nov 27, 2008
  5. 4/4 Automatically clean up any repositories created by the test casesRobin Rosenberg, Nov 27, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.