Skip to content

Commit d0cd377

Browse files
committed
Tweak unpackRepo(), allow specifying an explicit Path for the resource
The `unpackRepo()` method is very useful for setting up example repo fixtures for testing - it unzips zipped git archives on the resource classpath, and returns us the JGit repository. However, loading resources from the classpath (while convenient, meaning we don't need to worry about the absolute location on the filesystem of the zip file) can also be temperamental, with the resource not being found if it's not within the same classloader as the class of `com.madgag.git.test`. It seems the most reliable way to be able to find our test resources is to specify _which_ class we want to search the classpath of when we search for the resource. From there, we can get an unambiguous path for the resource that can be used by `unpackRepo()`.
1 parent 3077901 commit d0cd377

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

scala-git-test/src/main/scala/com/madgag/git/test/package.scala

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,36 @@
1717
package com.madgag.git
1818

1919
import net.lingala.zip4j.ZipFile
20-
21-
import java.io.File
22-
import java.io.File.separatorChar
23-
import java.net.URL
2420
import org.eclipse.jgit.internal.storage.file.FileRepository
2521
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
2622

27-
import java.nio.file.Files.createTempDirectory
23+
import java.io.File
24+
import java.net.URL
25+
import java.nio.file.{Files, Path}
2826

2927
package object test {
30-
def unpackRepo(fileName: String): FileRepository = {
31-
val resolvedGitDir = unpackRepoAndGetGitDir(fileName)
32-
require(resolvedGitDir.exists)
33-
FileRepositoryBuilder.create(resolvedGitDir).asInstanceOf[FileRepository]
34-
}
28+
def unpackRepo(zippedRepoResource: String): FileRepository = unpackRepo(pathForResource(zippedRepoResource))
29+
30+
def unpackRepo(zippedRepo: Path): FileRepository = fileRepoFor(unpackRepoAndGetGitDir(zippedRepo))
3531

36-
def unpackRepoAndGetGitDir(fileName: String): File = {
37-
val resource: URL = getClass.getResource(fileName)
32+
def pathForResource(fileName: String, clazz: Class[_] = getClass): Path = {
33+
val resource: URL = clazz.getResource(fileName)
3834
assert(resource != null, s"Resource for $fileName is null.")
35+
new File(resource.toURI).toPath
36+
}
3937

40-
val file = new File(resource.toURI)
41-
assert(file.exists(), s"File $file does not exist.")
38+
private def unpackRepoAndGetGitDir(zippedRepo: Path): File = {
39+
assert(Files.exists(zippedRepo), s"File $zippedRepo does not exist.")
4240

43-
val repoParentFolder = new File(createTempDirectory("test").toFile, fileName.replace(separatorChar, '_') + "-unpacked")
44-
repoParentFolder.mkdir()
41+
val repoParentFolder = Files.createTempDirectory(s"test-${zippedRepo.getFileName}-unpacked").toFile
4542

46-
new ZipFile(file.getAbsolutePath).extractAll(repoParentFolder.getAbsolutePath)
43+
new ZipFile(zippedRepo.toFile.getAbsolutePath).extractAll(repoParentFolder.getAbsolutePath)
4744

4845
repoParentFolder
4946
}
47+
48+
private def fileRepoFor(resolvedGitDir: File): FileRepository = {
49+
require(resolvedGitDir.exists)
50+
FileRepositoryBuilder.create(resolvedGitDir).asInstanceOf[FileRepository]
51+
}
5052
}

0 commit comments

Comments
 (0)