自定义NIO文件系统无法通过SBT的测试任务加载 [英] Custom NIO filesystem doesn't load through SBT's test task
问题描述
为了进行测试,我使用内存中的NIO FileSystem
实现( memoryfs ).我之前已经利用了它,并且似乎在例如Maven.
For testing, I'm using an in-memory NIO FileSystem
implementaion ( memoryfs ). I've taken advantage of it before, and it seems to run fine through e.g. Maven.
但是,现在,在SBT项目中,不可能初始化新的FileSystem
.
However, now, in an SBT project, it's impossible to initialize a new FileSystem
.
这里是用于重现该问题的最小SBT配置:
Here's a minimal SBT configuration to reproduce the problem:
import sbt._
import Keys._
name := "testfs"
organization := "com.example
version := "0.1-SNAPSHOT"
scalaVersion := "2.11.6"
libraryDependencies ++= {
val scalaTestVersion = "2.2.5"
Seq(
"org.scalatest" %% "scalatest" % scalaTestVersion % "test",
"org.mockito" % "mockito-core" % "1.10.19" % "test",
"de.pfabulist.lindwurm" % "memoryfs" % "0.28.3" % "test"
)}
这是一个测试:
import de.pfabulist.lindwurm.memory.MemoryFSBuilder
import org.scalatest.{FlatSpec, MustMatchers}
class FsDummySpec extends FlatSpec with MustMatchers {
it must "init the FS" in {
new MemoryFSBuilder().watchService(true).name("testFs").build() //init here
}
}
运行sbt test
将导致:
[info] FsDummySpec:
[info] - must init the FS *** FAILED ***
[info] java.nio.file.ProviderNotFoundException: Provider "memoryfs" not found
[info] at java.nio.file.FileSystems.getFileSystem(FileSystems.java:224)
[info] at de.pfabulist.kleinod.paths.Pathss.getOrCreate(Pathss.java:76)
这是东西:它应该可以正常运行.我的问题是:为什么以及如何解决?
Here's the thing: this should run without any problems. My question is: why, and how to fix it?
浏览自定义FS提供程序文档看起来SBT以某种方式使类路径混乱,但是很难说出原因.
Glancing over the custom FS provider docs it looks like SBT borks the classpath somehow, but its hard to say why.
注意:有趣的是,IntelliJ IDEA的测试运行程序似乎可以顺利运行,问题仅出在命令行中(在"SBT适当"中).
Note: interestingly enough, IntelliJ IDEA's test runner seems to work without a hitch, the problem is only on the command line (in "SBT proper").
推荐答案
openCage的评论提示了解决方案.
The comment by openCage hinted at the solution.
事实证明,自定义文件系统确实需要附加元素,即位于META-INF/services
中的服务提供商定义文件.
It turns out custom file systems do require an additional element, i.e. a service provider definition file located in META-INF/services
.
如果您使用自定义的NIO文件系统,则需要使该提供程序定义文件在测试类路径中可用.
最简单的方法可能只是派生测试VM,即将以下内容添加到您的build.sbt
:
The simplest way is probably just to fork the test VM, i.e. add the following to your build.sbt
:
fork in Test := true
这篇关于自定义NIO文件系统无法通过SBT的测试任务加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!