Skip to content

Commit

Permalink
Add failure message assertions to Gradle's "expect failure" tests (#4693
Browse files Browse the repository at this point in the history
)
  • Loading branch information
3flex committed Apr 7, 2022
1 parent 8c68b78 commit 6fe3aab
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
Expand Up @@ -101,7 +101,20 @@ class DetektReportMergeSpec {

val gradleRunner = DslGradleRunner(projectLayout, builder.gradleBuildName, mainBuildFileContent)
gradleRunner.setupProject()
gradleRunner.runTasksAndExpectFailure("detekt", "sarifReportMerge", "--continue") { _ ->
gradleRunner.runTasksAndExpectFailure("detekt", "sarifReportMerge", "--continue") { result ->
assertThat(result.output).contains("FAILURE: Build completed with 2 failures.")
assertThat(result.output).containsIgnoringWhitespaces(
"""
Execution failed for task ':child1:detekt'.
> Analysis failed with 2 weighted issues.
"""
)
assertThat(result.output).containsIgnoringWhitespaces(
"""
Execution failed for task ':child2:detekt'.
> Analysis failed with 4 weighted issues.
"""
)
assertThat(projectFile("build/reports/detekt/detekt.sarif")).doesNotExist()
assertThat(projectFile("build/reports/detekt/merge.sarif")).exists()
assertThat(projectFile("build/reports/detekt/merge.sarif").readText())
Expand Down Expand Up @@ -202,7 +215,20 @@ class DetektReportMergeSpec {

val gradleRunner = DslGradleRunner(projectLayout, builder.gradleBuildName, mainBuildFileContent)
gradleRunner.setupProject()
gradleRunner.runTasksAndExpectFailure("detekt", "xmlReportMerge", "--continue") { _ ->
gradleRunner.runTasksAndExpectFailure("detekt", "xmlReportMerge", "--continue") { result ->
assertThat(result.output).contains("FAILURE: Build completed with 2 failures.")
assertThat(result.output).containsIgnoringWhitespaces(
"""
Execution failed for task ':child1:detekt'.
> Analysis failed with 2 weighted issues.
"""
)
assertThat(result.output).containsIgnoringWhitespaces(
"""
Execution failed for task ':child2:detekt'.
> Analysis failed with 4 weighted issues.
"""
)
assertThat(projectFile("build/reports/detekt/detekt.xml")).doesNotExist()
assertThat(projectFile("build/reports/detekt/merge.xml")).exists()
assertThat(projectFile("build/reports/detekt/merge.xml").readText())
Expand Down
Expand Up @@ -47,6 +47,8 @@ class DetektTaskSpec {
.withDetektConfig(config)
.build()

gradleRunner.runDetektTaskAndExpectFailure()
gradleRunner.runDetektTaskAndExpectFailure { result ->
assertThat(result.output).contains("Analysis failed with 15 weighted issues.")
}
}
}
Expand Up @@ -383,7 +383,7 @@ class DetektTaskDslSpec {
@BeforeAll
fun beforeGroup() {
val config = """
|tasks.withType(io.gitlab.arturbosch.detekt.Detekt).configureEach {
|tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
| reports {
| custom {
| destination = file("build/reports/custom.xml")
Expand All @@ -397,7 +397,10 @@ class DetektTaskDslSpec {

@Test
fun `fails the build`() {
gradleRunner.runDetektTaskAndExpectFailure()
gradleRunner.runDetektTaskAndExpectFailure { result ->
assertThat(result.output)
.contains("If a custom report is specified, the reportId must be present")
}
}
}

Expand All @@ -406,7 +409,7 @@ class DetektTaskDslSpec {
@BeforeAll
fun beforeGroup() {
val config = """
|tasks.withType(io.gitlab.arturbosch.detekt.Detekt).configureEach {
|tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
| reports {
| custom {
| reportId = "customJson"
Expand All @@ -420,7 +423,10 @@ class DetektTaskDslSpec {

@Test
fun `fails the build`() {
gradleRunner.runDetektTaskAndExpectFailure()
gradleRunner.runDetektTaskAndExpectFailure { result ->
assertThat(result.output)
.contains("If a custom report is specified, the destination must be present")
}
}
}

Expand All @@ -431,7 +437,7 @@ class DetektTaskDslSpec {
val aDirectory = "\${rootDir}/src"

val config = """
|tasks.withType(io.gitlab.arturbosch.detekt.Detekt).configureEach {
|tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
| reports {
| custom {
| reportId = "foo"
Expand All @@ -446,7 +452,9 @@ class DetektTaskDslSpec {

@Test
fun `fails the build`() {
gradleRunner.runDetektTaskAndExpectFailure()
gradleRunner.runDetektTaskAndExpectFailure { result ->
assertThat(result.output).contains("Cannot write a file to a location pointing at a directory.")
}
}
}

Expand All @@ -456,7 +464,7 @@ class DetektTaskDslSpec {
@EnumSource(DetektReportType::class)
fun `fails the build`(wellKnownType: DetektReportType) {
val config = """
|tasks.withType(io.gitlab.arturbosch.detekt.Detekt).configureEach {
|tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
| reports {
| custom {
| reportId = "${wellKnownType.reportId}"
Expand All @@ -467,7 +475,10 @@ class DetektTaskDslSpec {
"""

gradleRunner = builder.withDetektConfig(config).build()
gradleRunner.runDetektTaskAndExpectFailure()
gradleRunner.runDetektTaskAndExpectFailure { result ->
assertThat(result.output)
.contains("The custom report reportId may not be same as one of the default reports")
}
}
}
}
Expand Down

0 comments on commit 6fe3aab

Please sign in to comment.