diff --git a/robolectric-extension/src/main/kotlin/tech/apter/junit/jupiter/robolectric/internal/FakeMavenManifestFactory.kt b/robolectric-extension/src/main/kotlin/tech/apter/junit/jupiter/robolectric/internal/FakeMavenManifestFactory.kt new file mode 100644 index 0000000..729904e --- /dev/null +++ b/robolectric-extension/src/main/kotlin/tech/apter/junit/jupiter/robolectric/internal/FakeMavenManifestFactory.kt @@ -0,0 +1,37 @@ +package tech.apter.junit.jupiter.robolectric.internal + +import org.robolectric.annotation.Config +import org.robolectric.internal.ManifestIdentifier +import org.robolectric.internal.MavenManifestFactory +import org.robolectric.res.Fs +import tech.apter.junit.jupiter.robolectric.internal.extensions.createLogger +import java.nio.file.Path +import java.nio.file.Paths + +internal class FakeMavenManifestFactory : MavenManifestFactory() { + + private inline val logger get() = createLogger() + + override fun identify(config: Config): ManifestIdentifier { + val manifestPath = config.manifest + if (manifestPath == Config.NONE) { + return ManifestIdentifier(null as String?, null, null, null, null) + } + + // Try to locate the manifest file as a classpath resource; fallback to using the base dir. + val manifestFile: Path + val resourceName = if (manifestPath.startsWith("/")) manifestPath else ("/$manifestPath") + val resourceUrl = javaClass.getResource(resourceName) + if (resourceUrl != null && "file" == resourceUrl.protocol) { + // Construct a path to the manifest file relative to the current working directory. + val workingDirectory = Paths.get(System.getProperty("user.dir")) + val absolutePath = Fs.fromUrl(resourceUrl) + logger.warn { "user.dir=$workingDirectory" } + logger.warn { "resourceUrl=$resourceUrl" } + logger.warn { "absolutePath=$absolutePath" } + + manifestFile = workingDirectory.relativize(absolutePath) + } + return super.identify(config) + } +} diff --git a/robolectric-extension/src/main/kotlin/tech/apter/junit/jupiter/robolectric/internal/JUnit5RobolectricTestRunner.kt b/robolectric-extension/src/main/kotlin/tech/apter/junit/jupiter/robolectric/internal/JUnit5RobolectricTestRunner.kt index 19075db..5b075a5 100644 --- a/robolectric-extension/src/main/kotlin/tech/apter/junit/jupiter/robolectric/internal/JUnit5RobolectricTestRunner.kt +++ b/robolectric-extension/src/main/kotlin/tech/apter/junit/jupiter/robolectric/internal/JUnit5RobolectricTestRunner.kt @@ -3,6 +3,7 @@ package tech.apter.junit.jupiter.robolectric.internal import org.junit.runners.model.FrameworkMethod import org.robolectric.RobolectricTestRunner import org.robolectric.internal.AndroidSandbox +import org.robolectric.internal.ManifestFactory import org.robolectric.internal.SandboxManager import org.robolectric.internal.SandboxManager.SandboxBuilder import org.robolectric.internal.SandboxTestRunner @@ -119,5 +120,6 @@ internal class JUnit5RobolectricTestRunner( defaultInjector().bind(SandboxBuilder::class.java, JUnit5RobolectricSandboxBuilder::class.java) .bind(DependencyResolver::class.java, JUnit5MavenDependencyResolver::class.java) .bind(SandboxManager::class.java, JUnit5RobolectricSandboxManager::class.java) + .bind(ManifestFactory::class.java, FakeMavenManifestFactory::class.java) } }