自定义 NIO 文件系统不会通过 SBT 的测试任务加载

Custom NIO filesystem doesn't load through SBT's test task

为了测试,我正在使用内存中的 NIO FileSystem 实现(memoryfs)。我以前利用过它,它似乎 运行 通过例如专家。

但是,现在在一个SBT项目中,不可能初始化一个新的FileSystem

这是重现问题的最小 SBT 配置:

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)

事情是这样的:这应该 运行 没有任何问题。我的问题是:为什么,以及如何解决它?

扫视 custom FS provider docs 似乎 SBT 以某种方式使类路径变笨,但很难说为什么。

注意: 有趣的是,IntelliJ IDEA 的测试 运行ner 似乎工作顺利,问题只出现在命令行上(在 "SBT proper").

openCage 的评论暗示了解决方案。

事实证明自定义文件系统确实需要一个额外的元素,即位于 META-INF/services.

中的服务提供商定义文件

如果您使用自定义 NIO 文件系统,则需要使该提供程序定义文件在测试类路径中可用。

最简单的方法可能只是分叉测试 VM,即将以下内容添加到您的 build.sbt:

fork in Test := true