Add scripted tests for resources

In sbt 1.3.0-RC1, when the dependency layers are used, the dependencies
cannot see the project resources. This can be problematic when the
library requires certain files like logback.xml to work. This introduces
a scripted test for this case.

The next commit should fix this test.
This commit is contained in:
Ethan Atkins 2019-05-25 10:22:50 -07:00
parent a128ddf4a6
commit 2268d91b47
24 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,5 @@
resolvers += "Local Maven" at (baseDirectory.value / "libraries" / "foo" / "ivy").toURI.toURL.toString
libraryDependencies += "sbt" %% "foo-lib" % "0.1.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"

View File

@ -0,0 +1,9 @@
package scripted
import org.scalatest.FlatSpec
class ResourceTest extends FlatSpec {
"test resources" should "load" in {
Main.main(Array("bar.txt", "updated-test"))
}
}

View File

@ -0,0 +1 @@
updated-main

View File

@ -0,0 +1 @@
updated-test

View File

@ -0,0 +1,11 @@
name := "foo-lib"
organization := "sbt"
publishTo := Some(Resolver.file("test-resolver", file("").getCanonicalFile / "ivy"))
version := "0.1.0"
classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.Dependencies
Test / classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.Dependencies

View File

@ -0,0 +1 @@
11081018ca6824226893b153fe439d3a94e23286

View File

@ -0,0 +1 @@
e4e1b37d77060a77ab20c508df90cdae6125f3bc

View File

@ -0,0 +1 @@
f25267dfe760e734ea4f8a00b8dc6ace477589d6

View File

@ -0,0 +1,20 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>sbt</groupId>
<artifactId>foo-lib_2.12</artifactId>
<packaging>jar</packaging>
<description>foo-lib</description>
<version>0.1.0</version>
<name>foo-lib</name>
<organization>
<name>sbt</name>
</organization>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.8</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1 @@
6b2c0b4d69ed8e10e0221db841e0f1fdeef12780

View File

@ -0,0 +1 @@
sbt.version=1.3.0-RC1

View File

@ -0,0 +1,19 @@
package resource
import java.net.URL
import java.nio.file._
import java.util.Collections
object Resource {
def readFile(url: URL): String = {
val uri = url.toURI
val fileSystem =
if (uri.getScheme != "jar") None
else Some(FileSystems.newFileSystem(uri, Collections.emptyMap[String, Object]))
try new String(Files.readAllBytes(Paths.get(uri)))
finally fileSystem.foreach(_.close())
}
def getStringResource(name: String): String = {
readFile(Resource.getClass.getClassLoader.getResource(name))
}
}

View File

@ -0,0 +1,10 @@
package scripted
import resource.Resource
object Main {
def main(args: Array[String]): Unit = {
val Array(resource, expected) = args
assert(Resource.getStringResource(resource) == expected)
}
}

View File

@ -0,0 +1,9 @@
package scripted
import org.scalatest.FlatSpec
class ResourceTest extends FlatSpec {
"test resources" should "load" in {
Main.main(Array("bar.txt", "test"))
}
}

View File

@ -0,0 +1,15 @@
> run foo.txt main
$ copy-file changes/updated-main.txt src/main/resources/foo.txt
> run foo.txt updated-main
> test
$ copy-file changes/updated-test.txt src/test/resources/bar.txt
-> test
$ copy-file changes/UpdatedResourceTest.scala src/test/scala/scripted/ResourceTest.scala
> test