From 1d44420c914789a863864360e4784a5392ae5efd Mon Sep 17 00:00:00 2001 From: Martin Duhem Date: Mon, 11 Apr 2016 09:48:42 +0200 Subject: [PATCH] Comply to Ivy's specification in `FakeResolver` --- ivy/src/main/scala/sbt/FakeResolver.scala | 17 +++++++++-------- .../scala/FakeResolverSpecification.scala | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/ivy/src/main/scala/sbt/FakeResolver.scala b/ivy/src/main/scala/sbt/FakeResolver.scala index 6ef728bdc..d9706fd53 100644 --- a/ivy/src/main/scala/sbt/FakeResolver.scala +++ b/ivy/src/main/scala/sbt/FakeResolver.scala @@ -59,14 +59,15 @@ private[sbt] class FakeResolver(private var name: String, cacheDir: File, module override def download(artifact: ArtifactOrigin, options: DownloadOptions): ArtifactDownloadReport = { val report = new ArtifactDownloadReport(artifact.getArtifact) val path = new URL(artifact.getLocation).toURI.getPath - assert(path.nonEmpty, "Path to local artifact is empty.") - val localFile = new File(path) - assert(localFile.exists, "Local file doesn't exist.") - report.setLocalFile(localFile) - report.setDownloadStatus(DownloadStatus.SUCCESSFUL) - report.setSize(localFile.length) + if (path.nonEmpty && localFile.exists) { + report.setLocalFile(localFile) + report.setDownloadStatus(DownloadStatus.SUCCESSFUL) + report.setSize(localFile.length) + } else { + report.setDownloadStatus(DownloadStatus.FAILED) + } report } @@ -112,7 +113,7 @@ private[sbt] class FakeResolver(private var name: String, cacheDir: File, module new ResolvedModuleRevision(this, this, moduleDescriptor, metadataReport) } - artifact getOrElse (throw new Exception(s"Could not find module $organisation % $name % $revision")) + artifact.orNull } @@ -161,7 +162,7 @@ private[sbt] class FakeResolver(private var name: String, cacheDir: File, module artifact <- artifacts find (a => a.name == art.getName && a.tpe == art.getType && a.ext == art.getExt) } yield new ArtifactOrigin(art, /* isLocal = */ true, artifact.file.toURI.toURL.toString) - artifact getOrElse (throw new IllegalStateException(s"Asking for non-existing module: $moduleOrganisation % $moduleName % $moduleRevision")) + artifact.orNull } diff --git a/ivy/src/test/scala/FakeResolverSpecification.scala b/ivy/src/test/scala/FakeResolverSpecification.scala index baed9cc28..fc1d9f300 100644 --- a/ivy/src/test/scala/FakeResolverSpecification.scala +++ b/ivy/src/test/scala/FakeResolverSpecification.scala @@ -13,10 +13,14 @@ class FakeResolverSpecification extends BaseIvySpecification { The FakeResolver should find modules with only one artifact $singleArtifact find modules with more than one artifact $multipleArtifacts + fail gracefully when asked for unknown modules $nonExistingModule + fail gracefully when some artifacts cannot be found $existingAndNonExistingArtifacts """ val myModule = ModuleID("org.example", "my-module", "0.0.1-SNAPSHOT", Some("compile")) val example = ModuleID("com.example", "example", "1.0.0", Some("compile")) + val anotherExample = ModuleID("com.example", "another-example", "1.0.0", Some("compile")) + val nonExisting = ModuleID("com.example", "does-not-exist", "1.2.3", Some("compile")) def singleArtifact = { val m = getModule(myModule) @@ -40,6 +44,16 @@ class FakeResolverSpecification extends BaseIvySpecification { allFiles map (_.getName) should beEqualTo(Set("artifact1-1.0.0.jar", "artifact2-1.0.0.txt")) } + def nonExistingModule = { + val m = getModule(nonExisting) + ivyUpdate(m) should throwA[ResolveException] + } + + def existingAndNonExistingArtifacts = { + val m = getModule(anotherExample) + ivyUpdate(m) should throwA[ResolveException]("download failed: com.example#another-example;1.0.0!non-existing.txt") + } + private def artifact1 = new File(getClass.getResource("/artifact1.jar").toURI.getPath) private def artifact2 = new File(getClass.getResource("/artifact2.txt").toURI.getPath) @@ -51,6 +65,11 @@ class FakeResolverSpecification extends BaseIvySpecification { ("com.example", "example", "1.0.0") -> List( FakeArtifact("artifact1", "jar", "jar", artifact1), FakeArtifact("artifact2", "txt", "txt", artifact2) + ), + + ("com.example", "another-example", "1.0.0") -> List( + FakeArtifact("artifact1", "jar", "jar", artifact1), + FakeArtifact("non-existing", "txt", "txt", new File("non-existing-file")) ) )