Skip to content

Commit

Permalink
Merge pull request #54 from jaredsburrows/pr/jaredsburrows/ConsoleRen…
Browse files Browse the repository at this point in the history
…derer

Use Gradle's ConsoleRenderer
  • Loading branch information
jaredsburrows committed Jun 1, 2019
2 parents 422c69a + 6399fc0 commit b344c28
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
70 changes: 70 additions & 0 deletions src/main/kotlin/com/jaredsburrows/spoon/ConsoleRenderer.kt
@@ -0,0 +1,70 @@
package com.jaredsburrows.spoon

import org.gradle.api.UncheckedIOException
import java.io.File
import java.io.IOException
import java.net.URI
import java.net.URISyntaxException

/**
* Renders information in a format suitable for logging to the console.
*
* Taken from: https://github.com/gradle/gradle/blob/master/subprojects/logging/src/main/java/org/gradle/internal/logging/ConsoleRenderer.java
*/
class ConsoleRenderer {
/**
* Renders a path name as a file URL that is likely recognized by consoles.
*/
fun asClickableFileUrl(path: File): String {
// File.toURI().toString() leads to an URL like this on Mac: file:/reports/index.html
// This URL is not recognized by the Mac console (too few leading slashes). We solve
// this be creating an URI with an empty authority.
try {
return URI("file", "", path.toURI().path, null, null).toString()
} catch (e: URISyntaxException) {
throw UncheckedException.throwAsUncheckedException(e)
}
}
}

/**
* Wraps a checked exception. Carries no other context.
*
* Taken from: https://github.com/gradle/gradle/blob/master/subprojects/base-services/src/main/java/org/gradle/internal/UncheckedException.java
*/
class UncheckedException : RuntimeException {
constructor(cause: Throwable) : super(cause)
constructor(message: String, cause: Throwable) : super(message, cause)

companion object {
/**
* Note: always throws the failure in some form. The return value is to keep the compiler happy.
*/
@JvmOverloads fun throwAsUncheckedException(
t: Throwable,
preserveMessage: Boolean = false
): RuntimeException {
if (t is RuntimeException) {
throw t
}

if (t is Error) {
throw t
}

if (t is IOException) {
if (preserveMessage) {
throw UncheckedIOException(t.message.orEmpty(), t)
} else {
throw UncheckedIOException(t)
}
}

if (preserveMessage) {
throw UncheckedException(t.message.orEmpty(), t)
} else {
throw UncheckedException(t)
}
}
}
}
5 changes: 1 addition & 4 deletions src/main/kotlin/com/jaredsburrows/spoon/SpoonTask.kt
Expand Up @@ -6,7 +6,6 @@ import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.net.URI
import java.time.Duration

open class SpoonTask : DefaultTask() {
Expand Down Expand Up @@ -109,9 +108,7 @@ open class SpoonTask : DefaultTask() {

val success = if (testing) testValue else builder.build().run()
if (!success && !extension.ignoreFailures) {
throw GradleException("Tests failed! See ${getClickableFileUrl(outputDir, "index.html")}")
throw GradleException("Tests failed! See ${ConsoleRenderer().asClickableFileUrl(File(outputDir, "index.html"))}")
}
}

private fun getClickableFileUrl(path: File, fileName: String): String = URI("file", "", File(path.toURI().path, fileName).path, null, null).toString()
}

0 comments on commit b344c28

Please sign in to comment.