Stop relying on non-optional artifacts in tests

Once we don't rely on directory listings anymore, most JARs will be
marked as optional. Also, attributes have to be set beforehand to get
artifacts with specific types, as available artifacts cannot be listed
beforehand.
This commit is contained in:
Alexandre Archambault 2018-09-19 15:04:52 +02:00
parent 2a5804caec
commit 2775ef396b
4 changed files with 91 additions and 200 deletions

View File

@ -9,62 +9,35 @@ object DirectoryListingTests extends TestSuite {
val user = "user"
val password = "pass"
val withListingRepo = MavenRepository(
val repo = MavenRepository(
"http://localhost:8080",
authentication = Some(Authentication(user, password))
)
val withoutListingRepo = MavenRepository(
"http://localhost:8081",
authentication = Some(Authentication(user, password))
)
val module = Module("com.abc", "test")
val version = "0.1"
val tests = Tests {
'withListing - {
'jar - CentralTests.withArtifacts(
module,
version,
"jar",
extraRepos = Seq(withListingRepo)
) {
artifacts =>
assert(artifacts.length == 1)
}
'jarFoo - CentralTests.withArtifacts(
module,
version,
"jar-foo",
extraRepos = Seq(withListingRepo)
) {
artifacts =>
assert(artifacts.length == 1)
}
'jar - CentralTests.withArtifacts(
module,
version,
attributes = Attributes("jar"),
extraRepos = Seq(repo)
) {
artifacts =>
assert(artifacts.length == 1)
assert(artifacts.headOption.exists(_.url.endsWith(".jar")))
}
'withoutListing - {
'jar - CentralTests.withArtifacts(
module,
version,
"jar",
extraRepos = Seq(withoutListingRepo)
) {
artifacts =>
assert(artifacts.length == 1)
}
'jarFoo - CentralTests.withArtifacts(
module,
version,
"jar-foo",
extraRepos = Seq(withoutListingRepo)
) {
artifacts =>
assert(artifacts.length == 0)
}
'jarFoo - CentralTests.withArtifacts(
module,
version,
attributes = Attributes("jar-foo"),
extraRepos = Seq(repo)
) {
artifacts =>
assert(artifacts.length == 1)
assert(artifacts.headOption.exists(_.url.endsWith(".jar-foo")))
}
}

View File

@ -46,8 +46,8 @@ object IvyTests extends TestSuite {
extraRepos = Seq(sbtRepo)
)
* - CentralTests.withArtifact(mod, ver, "jar", extraRepos = Seq(sbtRepo)) { artifact =>
assert(artifact.url == expectedArtifactUrl)
* - CentralTests.withArtifacts(mod, ver, Attributes("jar"), extraRepos = Seq(sbtRepo)) { artifacts =>
assert(artifacts.exists(_.url == expectedArtifactUrl))
}
}
@ -71,11 +71,9 @@ object IvyTests extends TestSuite {
val testJarUrl = repoBase + "com.example/a_2.11/0.1.0-SNAPSHOT/jars/a_2.11-tests.jar"
"no conf or classifier" - CentralTests.withArtifacts(
dep = dep,
artifactType = "jar",
dep = dep.copy(attributes = Attributes("jar")),
extraRepos = Seq(repo),
classifierOpt = None,
optional = true
classifierOpt = None
) {
case Seq(artifact) =>
assert(artifact.url == mainJarUrl)
@ -85,19 +83,12 @@ object IvyTests extends TestSuite {
"test conf" - CentralTests.withArtifacts(
dep = dep.copy(configuration = "test"),
artifactType = "jar",
extraRepos = Seq(repo),
classifierOpt = None,
optional = true
) {
case Seq(artifact1, artifact2) =>
val urls = Set(
artifact1.url,
artifact2.url
)
assert(urls == Set(mainJarUrl, testJarUrl))
case other =>
throw new Exception(s"Unexpected number of artifacts\n${other.mkString("\n")}")
classifierOpt = None
) { artifacts =>
val urls = artifacts.map(_.url).toSet
assert(urls(mainJarUrl))
assert(urls(testJarUrl))
}
"tests classifier" - {
@ -105,27 +96,18 @@ object IvyTests extends TestSuite {
* - CentralTests.withArtifacts(
deps = Set(dep, testsDep),
artifactType = "jar",
extraRepos = Seq(repo),
classifierOpt = None,
optional = true
) {
case Seq(artifact1, artifact2) =>
val urls = Set(
artifact1.url,
artifact2.url
)
assert(urls == Set(mainJarUrl, testJarUrl))
case other =>
throw new Exception(s"Unexpected number of artifacts\n${other.mkString("\n")}")
classifierOpt = None
) { artifacts =>
val urls = artifacts.map(_.url).toSet
assert(urls(mainJarUrl))
assert(urls(testJarUrl))
}
* - CentralTests.withArtifacts(
dep = testsDep,
artifactType = "jar",
extraRepos = Seq(repo),
classifierOpt = None,
optional = true
classifierOpt = None
) {
case Seq(artifact) =>
assert(artifact.url == testJarUrl)

View File

@ -26,11 +26,9 @@ object MavenTests extends TestSuite {
val sourcesJarUrl = repoBase + "com/abc/test-snapshot-special/0.1.0-SNAPSHOT/test-snapshot-special-0.1.0-20170421.034426-82-sources.jar"
* - CentralTests.withArtifacts(
dep = dep,
artifactType = "jar",
dep = dep.copy(attributes = Attributes("jar")),
extraRepos = Seq(repo),
classifierOpt = None,
optional = true
classifierOpt = None
) {
case Seq(artifact) =>
assert(artifact.url == mainJarUrl)
@ -39,11 +37,9 @@ object MavenTests extends TestSuite {
}
* - CentralTests.withArtifacts(
dep = dep,
artifactType = "src",
dep = dep.copy(attributes = Attributes("src")),
extraRepos = Seq(repo),
classifierOpt = Some("sources"),
optional = true
classifierOpt = Some("sources")
) {
case Seq(artifact) =>
assert(artifact.url == sourcesJarUrl)

View File

@ -126,58 +126,33 @@ abstract class CentralTests extends TestSuite {
assert(result == expected)
}
def withArtifact[T](
module: Module,
version: String,
artifactType: String,
attributes: Attributes = Attributes(),
extraRepos: Seq[Repository] = Nil
)(
f: Artifact => T
): Future[T] =
withArtifacts(module, version, artifactType, attributes, extraRepos) {
case Seq(artifact) =>
f(artifact)
case other =>
throw new Exception(
s"Unexpected artifact list size: ${other.size}\n" +
"Artifacts:\n" + other.map(" " + _).mkString("\n")
)
}
def withArtifacts[T](
module: Module,
version: String,
artifactType: String,
attributes: Attributes = Attributes(),
extraRepos: Seq[Repository] = Nil,
classifierOpt: Option[String] = None,
transitive: Boolean = false,
optional: Boolean = true
transitive: Boolean = false
)(
f: Seq[Artifact] => T
): Future[T] = {
val dep = Dependency(module, version, transitive = transitive, attributes = attributes)
withArtifacts(dep, artifactType, extraRepos, classifierOpt, optional)(f)
withArtifacts(dep, extraRepos, classifierOpt)(f)
}
def withArtifacts[T](
dep: Dependency,
artifactType: String,
extraRepos: Seq[Repository],
classifierOpt: Option[String],
optional: Boolean
classifierOpt: Option[String]
)(
f: Seq[Artifact] => T
): Future[T] =
withArtifacts(Set(dep), artifactType, extraRepos, classifierOpt, optional)(f)
withArtifacts(Set(dep), extraRepos, classifierOpt)(f)
def withArtifacts[T](
deps: Set[Dependency],
artifactType: String,
extraRepos: Seq[Repository],
classifierOpt: Option[String],
optional: Boolean
classifierOpt: Option[String]
)(
f: Seq[Artifact] => T
): Future[T] = async {
@ -191,13 +166,8 @@ abstract class CentralTests extends TestSuite {
assert(isDone)
val artifacts = classifierOpt
.fold(res.dependencyArtifacts(withOptional = optional))(c => res.dependencyClassifiersArtifacts(Seq(c)))
.fold(res.dependencyArtifacts(withOptional = true))(c => res.dependencyClassifiersArtifacts(Seq(c)))
.map(_._2)
.filter {
if (artifactType == "*") _ => true
else
_.`type` == artifactType
}
f(artifacts)
}
@ -205,13 +175,12 @@ abstract class CentralTests extends TestSuite {
def ensureHasArtifactWithExtension(
module: Module,
version: String,
artifactType: String,
extension: String,
attributes: Attributes = Attributes(),
extraRepos: Seq[Repository] = Nil
): Future[Unit] =
withArtifact(module, version, artifactType, attributes = attributes, extraRepos = extraRepos) { artifact =>
assert(artifact.url.endsWith("." + extension))
withArtifacts(module, version, attributes = attributes, extraRepos = extraRepos) { artifacts =>
assert(artifacts.exists(_.url.endsWith("." + extension)))
}
val tests = Tests {
@ -305,7 +274,7 @@ abstract class CentralTests extends TestSuite {
mod,
version,
"jar",
"jar",
Attributes("jar"),
extraRepos = Seq(extraRepo)
)
}
@ -409,8 +378,8 @@ abstract class CentralTests extends TestSuite {
* - resolutionCheck(mod, version)
* - withArtifact(mod, version, "jar") { artifact =>
assert(artifact.url == expectedArtifactUrl)
* - withArtifacts(mod, version, Attributes("jar")) { artifacts =>
assert(artifacts.exists(_.url == expectedArtifactUrl))
}
}
@ -438,7 +407,8 @@ abstract class CentralTests extends TestSuite {
Dependency(
Module("org.scala-lang", "scala-compiler"), "2.11.8",
configuration = config,
transitive = false
transitive = false,
attributes = Attributes("jar")
)
withArtifacts(
@ -446,10 +416,8 @@ abstract class CentralTests extends TestSuite {
intransitiveCompiler("default"),
intransitiveCompiler("optional")
),
"jar",
extraRepos = Nil,
classifierOpt = None,
optional = true
classifierOpt = None
) {
case Seq() =>
throw new Exception("Expected one JAR")
@ -471,7 +439,6 @@ abstract class CentralTests extends TestSuite {
module,
version,
tpe,
tpe,
attributes = Attributes(tpe)
)
@ -480,7 +447,6 @@ abstract class CentralTests extends TestSuite {
ensureHasArtifactWithExtension(
module,
version,
tpe,
tpe
)
}
@ -491,7 +457,6 @@ abstract class CentralTests extends TestSuite {
* - ensureHasArtifactWithExtension(
Module("com.google.guava", "guava"),
"17.0",
"bundle",
"jar"
)
@ -500,7 +465,6 @@ abstract class CentralTests extends TestSuite {
* - ensureHasArtifactWithExtension(
Module("com.google.guava", "guava"),
"17.0",
"bundle",
"jar",
attributes = Attributes("jar")
)
@ -511,8 +475,8 @@ abstract class CentralTests extends TestSuite {
ensureHasArtifactWithExtension(
Module("org.bytedeco", "javacpp"),
"1.1",
"maven-plugin",
"jar"
"jar",
Attributes("maven-plugin")
)
}
}
@ -530,7 +494,7 @@ abstract class CentralTests extends TestSuite {
)
)
val res = await(resolve(deps, extraRepos = Seq(extraRepo)))
val filenames: Set[String] = res.artifacts.map(_.url.split("/").last).toSet
val filenames: Set[String] = res.artifacts(withOptional = true).map(_.url.split("/").last).toSet
assert(filenames.contains("avro-1.8.1.jar"))
assert(!filenames.contains("avro-1.8.1-tests.jar"))
}
@ -544,7 +508,7 @@ abstract class CentralTests extends TestSuite {
)
)
val res = await(resolve(deps, extraRepos = Seq(extraRepo)))
val filenames: Set[String] = res.artifacts.map(_.url.split("/").last).toSet
val filenames: Set[String] = res.artifacts(withOptional = true).map(_.url.split("/").last).toSet
assert(!filenames.contains("avro-1.8.1.jar"))
assert(filenames.contains("avro-1.8.1-tests.jar"))
}
@ -561,7 +525,7 @@ abstract class CentralTests extends TestSuite {
)
)
val res = await(resolve(deps, extraRepos = Seq(extraRepo)))
val filenames: Set[String] = res.artifacts.map(_.url.split("/").last).toSet
val filenames: Set[String] = res.artifacts(withOptional = true).map(_.url.split("/").last).toSet
assert(filenames.contains("avro-1.8.1.jar"))
assert(filenames.contains("avro-1.8.1-tests.jar"))
}
@ -589,7 +553,7 @@ abstract class CentralTests extends TestSuite {
assert(conflicts.isEmpty)
assert(isDone)
val artifacts = res.artifacts
val artifacts = res.artifacts(withOptional = true)
val map = artifacts.groupBy(a => a)
@ -640,7 +604,6 @@ abstract class CentralTests extends TestSuite {
val zookeeperTestArtifact = zookeeperTestArtifacts.head
assert(!isActualCentral || !zookeeperTestArtifact.isOptional)
assert(zookeeperTestArtifact.attributes.`type` == "test-jar")
assert(zookeeperTestArtifact.attributes.classifier == "tests")
zookeeperTestArtifact.url.endsWith("-tests.jar")
@ -703,30 +666,21 @@ abstract class CentralTests extends TestSuite {
* - resolutionCheck(mod, version)
val mainTarGzUrl = s"$centralBase/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.tar.gz"
val expectedTarGzArtifactUrls = Set(
mainTarGzUrl,
s"$centralBase/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3-bin.tar.gz"
)
val mainZipUrl = s"$centralBase/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip"
val expectedZipArtifactUrls = Set(
mainZipUrl,
s"$centralBase/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3-bin.zip"
)
'tarGz - {
* - {
if (isActualCentral)
withArtifacts(mod, version, "tar.gz", classifierOpt = Some("bin"), transitive = true) { artifacts =>
assert(artifacts.length == 2)
withArtifacts(mod, version, attributes = Attributes("tar.gz", "bin"), transitive = true) { artifacts =>
assert(artifacts.nonEmpty)
val urls = artifacts.map(_.url).toSet
assert(urls == expectedTarGzArtifactUrls)
assert(urls.contains(mainTarGzUrl))
}
else
Future.successful(())
}
* - {
withArtifacts(mod, version, "tar.gz", attributes = Attributes("tar.gz", "bin"), classifierOpt = Some("bin"), transitive = true) { artifacts =>
withArtifacts(mod, version, attributes = Attributes("tar.gz", "bin"), classifierOpt = Some("bin"), transitive = true) { artifacts =>
assert(artifacts.nonEmpty)
val urls = artifacts.map(_.url).toSet
assert(urls.contains(mainTarGzUrl))
@ -737,16 +691,16 @@ abstract class CentralTests extends TestSuite {
'zip - {
* - {
if (isActualCentral)
withArtifacts(mod, version, "zip", classifierOpt = Some("bin"), transitive = true) { artifacts =>
assert(artifacts.length == 2)
withArtifacts(mod, version, attributes = Attributes("zip", "bin"), transitive = true) { artifacts =>
assert(artifacts.nonEmpty)
val urls = artifacts.map(_.url).toSet
assert(urls == expectedZipArtifactUrls)
assert(urls.contains(mainZipUrl))
}
else
Future.successful(())
}
* - {
withArtifacts(mod, version, "zip", attributes = Attributes("zip", "bin"), classifierOpt = Some("bin"), transitive = true) { artifacts =>
withArtifacts(mod, version, attributes = Attributes("zip", "bin"), classifierOpt = Some("bin"), transitive = true) { artifacts =>
assert(artifacts.nonEmpty)
val urls = artifacts.map(_.url).toSet
assert(urls.contains(mainZipUrl))
@ -776,9 +730,8 @@ abstract class CentralTests extends TestSuite {
* - resolutionCheck(mod, ver)
* - withArtifacts(mod, ver, "jar", transitive = true) { artifacts =>
assert(artifacts.length == 1)
assert(artifacts.head.url == expectedUrl)
* - withArtifacts(mod, ver, transitive = true) { artifacts =>
assert(artifacts.exists(_.url == expectedUrl))
}
}
}
@ -812,33 +765,32 @@ abstract class CentralTests extends TestSuite {
def hasSha1(a: Artifact) = a.checksumUrls.contains("SHA-1")
def hasMd5(a: Artifact) = a.checksumUrls.contains("MD5")
def hasSig(a: Artifact) = a.extra.contains("sig")
def sigHasSig(a: Artifact) = a.extra.get("sig").exists(hasSig)
* - resolutionCheck(mod, ver)
* - withArtifacts(mod, ver, "*") { artifacts =>
* - withArtifacts(mod, ver, Attributes("bundle")) { artifacts =>
val jarOpt = artifacts.find(_.`type` == "bundle").orElse(artifacts.find(_.`type` == "jar"))
val pomOpt = artifacts.find(_.`type` == "pom")
assert(jarOpt.nonEmpty)
assert(jarOpt.forall(hasSha1))
assert(jarOpt.forall(hasMd5))
assert(jarOpt.forall(hasSig))
}
if (isActualCentral) {
if (artifacts.length != 2 || jarOpt.isEmpty || pomOpt.isEmpty)
artifacts.foreach(println)
* - {
if (isActualCentral)
withArtifacts(mod, ver, Attributes("pom")) { artifacts =>
assert(jarOpt.forall(_.`type` == "bundle"))
assert(artifacts.length == 2)
assert(pomOpt.nonEmpty)
assert(pomOpt.forall(hasSha1))
assert(pomOpt.forall(hasMd5))
assert(pomOpt.forall(hasSig))
assert(jarOpt.forall(sigHasSig))
assert(pomOpt.forall(sigHasSig))
}
val pomOpt = artifacts.find(_.`type` == "pom")
assert(pomOpt.nonEmpty)
assert(pomOpt.forall(hasSha1))
assert(pomOpt.forall(hasMd5))
assert(pomOpt.forall(hasSig))
}
else
Future.successful(())
}
}
@ -877,40 +829,28 @@ abstract class CentralTests extends TestSuite {
val mod = Module("io.monix", "monix_2.12")
val ver = "2.3.0"
val mainUrl = "https://repo1.maven.org/maven2/io/monix/monix_2.12/2.3.0/monix_2.12-2.3.0.jar"
val mainUrl = s"$centralBase/io/monix/monix_2.12/2.3.0/monix_2.12-2.3.0.jar"
* - resolutionCheck(mod, ver)
* - {
if (isActualCentral)
withArtifacts(mod, ver, "jar") { artifacts =>
val mainArtifactOpt = artifacts.find(_.url == mainUrl)
assert(mainArtifactOpt.nonEmpty)
assert(mainArtifactOpt.forall(_.isOptional))
}
else
Future.successful(())
}
* - withArtifacts(mod, ver, "jar", optional = false) { artifacts =>
* - withArtifacts(mod, ver, Attributes("jar")) { artifacts =>
val mainArtifactOpt = artifacts.find(_.url == mainUrl)
assert(mainArtifactOpt.isEmpty)
assert(mainArtifactOpt.nonEmpty)
assert(mainArtifactOpt.forall(_.isOptional))
}
* - {
if (isActualCentral)
withArtifacts(Module("com.lihaoyi", "scalatags_2.12"), "0.6.2", "jar", transitive = true, optional = false) { artifacts =>
assert(artifacts.forall(!_.isOptional))
withArtifacts(Module("com.lihaoyi", "scalatags_2.12"), "0.6.2", Attributes("jar"), transitive = true) { artifacts =>
val urls = artifacts.map(_.url).toSet
val expectedUrls = Set(
val expectedUrls = Seq(
"https://repo1.maven.org/maven2/org/scala-lang/scala-library/2.12.0/scala-library-2.12.0.jar",
"https://repo1.maven.org/maven2/com/lihaoyi/sourcecode_2.12/0.1.3/sourcecode_2.12-0.1.3.jar",
"https://repo1.maven.org/maven2/com/lihaoyi/scalatags_2.12/0.6.2/scalatags_2.12-0.6.2.jar"
)
assert(urls == expectedUrls)
assert(expectedUrls.forall(urls))
}
else
Future.successful(())
@ -925,7 +865,7 @@ abstract class CentralTests extends TestSuite {
* - resolutionCheck(mod, ver, extraRepos = Seq(extraRepo))
* - withArtifacts(mod, ver, "*", extraRepos = Seq(extraRepo), transitive = true) { artifacts =>
* - withArtifacts(mod, ver, Attributes("aar"), extraRepos = Seq(extraRepo), transitive = true) { artifacts =>
val urls = artifacts.map(_.url).toSet
val expectedUrls = Set(
"https://maven.google.com/com/android/support/support-fragment/25.3.1/support-fragment-25.3.1.aar",
@ -964,7 +904,7 @@ abstract class CentralTests extends TestSuite {
* - {
if (isActualCentral)
withArtifacts(mod, ver, "*", extraRepos = extraRepos, transitive = true) { artifacts =>
withArtifacts(mod, ver, Attributes("jar"), extraRepos = extraRepos, transitive = true) { artifacts =>
val urls = artifacts.map(_.url).toSet
val expectedUrls = Set(
"https://artifacts-oss.talend.com/nexus/content/repositories/TalendOpenSourceRelease/com/cedarsoftware/json-io/4.9.9-TALEND/json-io-4.9.9-TALEND.jar",