原文链接:https://www.scala-sbt.org/1.x/docs/Mapping-Files.html
原文中有6段代码,其中第1,2,6段代码在windows上是跑不对的。现在将错误原因和修改代码一并给出:
第1,2段代码出错原因一样,是目录分隔符的原因:
- file(),relativeTo(),以及rebase(File)方法会将目录分隔符(/)转换成windows特有的分隔符(\),因此mappings的值变成String输出是:Vector((\a\b\C.scala,b\C.scala))
- 但是,expected是一个Seq[File,String],其String部分写死了为b/C.scala, 所以expected的String输出是List((\a\b\C.scala,b/C.scala))
- ==比较的是内容,不考虑集合的类型,所以比较对象(mappings和expected)的类型是List还是Vector无所谓,但是内容不同就不同了。
第6段代码出错原因除了目录分割符问题之外,还因为在expected的定义里多了两个括号。
修改后能跑的6段代码集合如下:
import Path.relativeTo val files: Seq[File] = file("/a/b/C.scala") :: Nil val baseDirectories: Seq[File] = file("/a") :: Nil val mappings: Seq[(File,String)] = files pair relativeTo(baseDirectories) val expected = (file("/a/b/C.scala") -> "b\\C.scala") :: Nil assert( mappings == expected ) import Path.rebase val files2: Seq[File] = file("/a/b/C.scala") :: Nil val baseDirectories2: File = file("/a") val mappings2: Seq[(File,String)] = files2 pair rebase(baseDirectories2, "pre/") val expected2 = (file("/a/b/C.scala") -> "pre/b\\C.scala") :: Nil assert( mappings2 == expected2 ) val files3: Seq[File] = file("/a/b/C.scala") :: Nil val baseDirectories3: Seq[File] = file("/a") :: Nil val newBase3: File = file("/new/base") val mappings3: Seq[(File,File)] = files3 pair rebase(baseDirectories3, newBase3) val expected3 = (file("/a/b/C.scala") -> file("/new/base/b/C.scala") ) :: Nil assert( mappings3 == expected3 ) import Path.flat val files4: Seq[File] = file("/a/b/C.scala") :: Nil val mappings4: Seq[(File,String)] = files4 pair flat val expected4 = (file("/a/b/C.scala") -> "C.scala" ) :: Nil assert( mappings4 == expected4 ) val files5: Seq[File] = file("/a/b/C.scala") :: Nil val newBase5: File = file("/new/base") val mappings5: Seq[(File,File)] = files5 pair flat(newBase5) val expected5 = (file("/a/b/C.scala") -> file("/new/base/C.scala") ) :: Nil assert( mappings5 == expected5 ) val files6: Seq[File] = file("/a/b/C.scala") :: file("/zzz/D.scala") :: Nil val baseDirectories6: Seq[File] = file("/a") :: Nil val mappings6: Seq[(File,String)] = files6 pair ( relativeTo(baseDirectories6) | flat ) val expected6 = (file("/a/b/C.scala") -> "b\\C.scala") :: (file("/zzz/D.scala") -> "D.scala") :: Nil assert( mappings6 == expected6 )