Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Gradle's ConsoleRenderer #54

Merged
merged 1 commit into from
Jun 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/main/kotlin/com/jaredsburrows/spoon/ConsoleRenderer.kt
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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()
}