Skip to content

Commit

Permalink
test(inspections) #44 Add tests for clippy output parser
Browse files Browse the repository at this point in the history
  • Loading branch information
missingdays committed Jul 5, 2021
1 parent c23c0ed commit 315b7e8
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 3 deletions.
1 change: 1 addition & 0 deletions plugin-rust-agent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
compileOnly("org.jetbrains.teamcity.internal:agent:${teamcityVersion}")
testImplementation("org.testng:testng:6.8")
testImplementation("org.jmock:jmock:2.5.1")
testImplementation("org.assertj:assertj-core:3.11.1")
}

tasks.named<Test>("test") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import jetbrains.buildServer.rust.inspections.ClippyInspectionsParser
* Cargo runner service factory.
*/
class CargoBuildSessionFactory(
private val inspectionReporter: InspectionReporter,
private val clippyInspectionsParser: ClippyInspectionsParser
private val inspectionReporter: InspectionReporter
) : MultiCommandBuildSessionFactory {

override fun createSession(runnerContext: BuildRunnerContext) =
CargoCommandBuildSession(runnerContext as BuildRunnerContextEx, inspectionReporter, clippyInspectionsParser)
CargoCommandBuildSession(runnerContext as BuildRunnerContextEx, inspectionReporter, ClippyInspectionsParser())

override fun getBuildRunnerInfo(): AgentBuildRunnerInfo {
return object : AgentBuildRunnerInfo {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2000-2021 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* See LICENSE in the project root for license information.
*/

package jetbrains.buildServer.rust.inspections

import org.assertj.core.api.Java6Assertions.assertThat
import org.testng.annotations.BeforeMethod

import org.testng.annotations.Test

class ClippyInspectionsParserTest {

private lateinit var parser: ClippyInspectionsParser

private lateinit var clippyOutput: String
private val parserResult = mutableListOf<Inspection>()

@BeforeMethod
fun setUp() {
parser = ClippyInspectionsParser()
}

@Test
fun `should parse empty files without errors`() {
`given clippy produced`("nothing")
`when parser runs`()
`no inspections should be reported`()
}

@Test
fun `should parse clippy output with 1 error reported`() {
`given clippy produced`("single.error")
`when parser runs`()
`single error should be reported`()
}

@Test
fun `should parse clippy output with 1 warning reported`() {
`given clippy produced`("single.warning")
`when parser runs`()
`single warning should be reported`()
}

@Test
fun `should parse clippy output with error and warning reported`() {
`given clippy produced`("error.and.warning")
`when parser runs`()
`error and warning should be reported`()
}

@Test
fun `should parse clippy output with filename with colon symbol in name`() {
`given clippy produced`("file.with.colon")
`when parser runs`()
`error with filename with colon symbol should be reported`()
}

private fun `given clippy produced`(filename: String) {
clippyOutput = this::class.java.classLoader.getResource("cargo/clippy/$filename.txt")!!.readText()
}

private fun `when parser runs`() {
parserResult.clear()
for (line in clippyOutput.lineSequence()) {
parser.processLine(line)?.let(parserResult::add)
}
}

private fun `no inspections should be reported`() {
assertThat(parserResult).isEmpty()
}

private fun `single error should be reported`() {
assertThat(parserResult).hasSize(1)
val inspection = parserResult[0]
assertThat(inspection.type).isEqualTo(ClippyInspectionsParser.CLIPPY_ERROR)
assertThat(inspection.severity).isEqualTo(Inspection.Severity.ERROR)
assertThat(inspection.message).isEqualTo("for loop over `option`, which is an `Option`. This is more readably written as an `if let` statement")
assertThat(inspection.file).isEqualTo("clippy2.rs")
assertThat(inspection.line).isEqualTo(9)
}

private fun `single warning should be reported`() {
assertThat(parserResult).hasSize(1)
val inspection = parserResult[0]
assertThat(inspection.type).isEqualTo(ClippyInspectionsParser.CLIPPY_WARNING)
assertThat(inspection.severity).isEqualTo(Inspection.Severity.WARNING)
assertThat(inspection.message).isEqualTo("unused variable: `y`")
assertThat(inspection.file).isEqualTo("clippy.rs")
assertThat(inspection.line).isEqualTo(16)
}

private fun `error and warning should be reported`() {
assertThat(parserResult).hasSize(2)
val error = parserResult[0]
assertThat(error.type).isEqualTo(ClippyInspectionsParser.CLIPPY_ERROR)
assertThat(error.severity).isEqualTo(Inspection.Severity.ERROR)
assertThat(error.message).isEqualTo("for loop over `option`, which is an `Option`. This is more readably written as an `if let` statement")
assertThat(error.file).isEqualTo("clippy2.rs")
assertThat(error.line).isEqualTo(9)

val warning = parserResult[1]
assertThat(warning.type).isEqualTo(ClippyInspectionsParser.CLIPPY_WARNING)
assertThat(warning.severity).isEqualTo(Inspection.Severity.WARNING)
assertThat(warning.message).isEqualTo("`assert!(false)` should probably be replaced")
assertThat(warning.file).isEqualTo("clippy.rs")
assertThat(warning.line).isEqualTo(19)
}

private fun `error with filename with colon symbol should be reported`() {
assertThat(parserResult).hasSize(1)
val inspection = parserResult[0]
assertThat(inspection.type).isEqualTo(ClippyInspectionsParser.CLIPPY_ERROR)
assertThat(inspection.severity).isEqualTo(Inspection.Severity.ERROR)
assertThat(inspection.message).isEqualTo("for loop over `option`, which is an `Option`. This is more readably written as an `if let` statement")
assertThat(inspection.file).isEqualTo("weird:file:name.rs")
assertThat(inspection.line).isEqualTo(9)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Checking clippy2 v0.0.1 (/home/evgenii_bovykin/IdeaProjects/teamcity-rust-plugin/servers/TeamCity-2021.1/buildAgent/work/2d3c4d163bcdcf0e)t
error: for loop over `option`, which is an `Option`. This is more readably written as an `if let` statement
--> clippy2.rs:9:14
|
9 | for x in option {
| ^^^^^^
|
= note: `#[deny(clippy::for_loops_over_fallibles)]` on by default
= help: consider replacing `for x in option` with `if let Some(x) = option`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#for_loops_over_fallibles

warning: `assert!(false)` should probably be replaced
--> clippy.rs:19:5
|
19 | assert!(false);
| ^^^^^^^^^^^^^^^
|
= note: `#[warn(clippy::assertions_on_constants)]` on by default
= help: use `panic!()` or `unreachable!()`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `clippy2`

To learn more, run the command again with --verbose.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Checking clippy2 v0.0.1 (/home/evgenii_bovykin/IdeaProjects/teamcity-rust-plugin/servers/TeamCity-2021.1/buildAgent/work/2d3c4d163bcdcf0e)
error: for loop over `option`, which is an `Option`. This is more readably written as an `if let` statement
--> weird:file:name.rs:9:14
|
9 | for x in option {
| ^^^^^^
|
= note: `#[deny(clippy::for_loops_over_fallibles)]` on by default
= help: consider replacing `for x in option` with `if let Some(x) = option`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#for_loops_over_fallibles

error: aborting due to 2 previous errors; 4 warnings emitted

error: could not compile `clippy2`

To learn more, run the command again with --verbose.
Empty file.
16 changes: 16 additions & 0 deletions plugin-rust-agent/src/test/resources/cargo/clippy/single.error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Checking clippy2 v0.0.1 (/home/evgenii_bovykin/IdeaProjects/teamcity-rust-plugin/servers/TeamCity-2021.1/buildAgent/work/2d3c4d163bcdcf0e)
error: for loop over `option`, which is an `Option`. This is more readably written as an `if let` statement
--> clippy2.rs:9:14
|
9 | for x in option {
| ^^^^^^
|
= note: `#[deny(clippy::for_loops_over_fallibles)]` on by default
= help: consider replacing `for x in option` with `if let Some(x) = option`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#for_loops_over_fallibles

error: aborting due to 2 previous errors; 4 warnings emitted

error: could not compile `clippy2`

To learn more, run the command again with --verbose.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Checking clippy2 v0.0.1 (/home/evgenii_bovykin/IdeaProjects/teamcity-rust-plugin/servers/TeamCity-2021.1/buildAgent/work/2d3c4d163bcdcf0e)
warning: unused variable: `y`
--> clippy.rs:16:9
|
16 | let y = 1_f64 / x;
| ^ help: if this is intentional, prefix it with an underscore: `_y`
|
= note: `#[warn(unused_variables)]` on by default

error: aborting due to 2 previous errors; 4 warnings emitted

error: could not compile `clippy2`

To learn more, run the command again with --verbose.

0 comments on commit 315b7e8

Please sign in to comment.