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

Add failure message assertions to Gradle's "expect failure" tests #4693

Merged
merged 2 commits into from Apr 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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