-
Notifications
You must be signed in to change notification settings - Fork 176
Integration & Testing of the Problems API #927
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
Open
vanessamj99
wants to merge
11
commits into
JLLeitschuh:main
Choose a base branch
from
vanessamj99:problems-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
50e6c70
Integrated the Problems API after a certain gradle version and the te…
vanessamj99 053ad04
Add mockito to libs.version.toml and used the libs. to reference it
vanessamj99 95342d2
using report Problem in report Problems
vanessamj99 79338ce
Addressed comments, injected problems, removed constructor - no op
vanessamj99 cacc317
moved compileOnly(gradleApi())
vanessamj99 cf97481
added integration test, made edits to reporter and consoleReportWorkA…
vanessamj99 35dce07
lint fixes
vanessamj99 d658c67
added problems api to the readme
vanessamj99 4c19380
reverted README and edited CHANGELOG
vanessamj99 ea67c00
aadded problems api integration back into change log
vanessamj99 4b6877c
deleted useless integration test file
vanessamj99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/reporter/ProblemsApiReporter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package org.jlleitschuh.gradle.ktlint.reporter | ||
|
|
||
| import org.gradle.api.problems.ProblemGroup | ||
| import org.gradle.api.problems.ProblemId | ||
| import org.gradle.api.problems.ProblemReporter | ||
| import org.gradle.api.problems.Problems | ||
| import org.gradle.api.problems.Severity | ||
| import org.jlleitschuh.gradle.ktlint.worker.SerializableLintError | ||
| import javax.inject.Inject | ||
|
|
||
| internal class ProblemsApiReporter @Inject constructor( | ||
| private val problems: Problems | ||
| ) { | ||
|
|
||
| companion object { | ||
| private val PROBLEM_GROUP = ProblemGroup.create("ktlint-gradle", "ktlint-gradle issue") | ||
| } | ||
|
|
||
| fun reportProblems(lintErrors: Map<String, List<SerializableLintError>>, ignoreFailures: Boolean) { | ||
| val severity = if (ignoreFailures) Severity.WARNING else Severity.ERROR | ||
| lintErrors.forEach { (filePath, errors) -> | ||
| errors.forEach { error -> | ||
| reportProblem(error, filePath, severity) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun reportProblem(error: SerializableLintError, filePath: String, severity: Severity) { | ||
JLLeitschuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| val reporter: ProblemReporter? = problems.reporter | ||
| val id = ProblemId.create(error.ruleId, error.detail, PROBLEM_GROUP) | ||
| reporter?.report(id) { | ||
| lineInFileLocation(filePath, error.line, error.col) | ||
| details(error.detail) | ||
| severity(severity) | ||
| solution("Run ktlintFormat to auto-fix this issue") | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/reporter/ProblemsApiReporterTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| package org.jlleitschuh.gradle.ktlint.reporter | ||
|
|
||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.gradle.api.Action | ||
| import org.gradle.api.problems.ProblemReporter | ||
| import org.gradle.api.problems.ProblemSpec | ||
| import org.gradle.api.problems.Problems | ||
| import org.jlleitschuh.gradle.ktlint.worker.SerializableLintError | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
| import org.mockito.kotlin.any | ||
| import org.mockito.kotlin.argumentCaptor | ||
| import org.mockito.kotlin.eq | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.never | ||
| import org.mockito.kotlin.times | ||
| import org.mockito.kotlin.verify | ||
| import org.mockito.kotlin.whenever | ||
| import org.gradle.api.problems.Severity as GradleSeverity | ||
|
|
||
| class ProblemsApiReporterTest { | ||
|
|
||
| private lateinit var problemsService: Problems | ||
| private lateinit var problemReporter: ProblemReporter | ||
| private lateinit var reporter: ProblemsApiReporter | ||
|
|
||
| @BeforeEach | ||
| fun setUp() { | ||
| problemsService = mock() | ||
| problemReporter = mock() | ||
| reporter = ProblemsApiReporter(problemsService) | ||
| whenever(problemsService.reporter).thenReturn(problemReporter) | ||
| } | ||
|
|
||
| @Test | ||
| fun `given a lint error, it correctly reports it to the Gradle Problems API`() { | ||
| val error = SerializableLintError( | ||
| line = 4, | ||
| col = 1, | ||
| ruleId = "no-wildcard-imports", | ||
| detail = "Wildcard import detected", | ||
| canBeAutoCorrected = true | ||
| ) | ||
| val filePath = "src/main/kotlin/TestFile.kt" | ||
|
|
||
| reporter.reportProblem(error, filePath, GradleSeverity.ERROR) | ||
|
|
||
| val specCaptor = argumentCaptor<Action<ProblemSpec>>() | ||
| verify(problemReporter).report(any(), specCaptor.capture()) | ||
|
|
||
| val spec: ProblemSpec = mock() | ||
| specCaptor.firstValue.execute(spec) | ||
|
|
||
| verify(spec).details("Wildcard import detected") | ||
| verify(spec).severity(GradleSeverity.ERROR) | ||
| verify(spec).lineInFileLocation(eq(filePath), eq(4), eq(1)) | ||
| verify(spec).solution("Run ktlintFormat to auto-fix this issue") | ||
| } | ||
|
|
||
| @Test | ||
| fun `given multiple lint errors, it correctly reports all of them`() { | ||
| val errors = mapOf( | ||
| "src/main/kotlin/File1.kt" to listOf( | ||
| SerializableLintError( | ||
| line = 1, | ||
| col = 1, | ||
| ruleId = "no-wildcard-imports", | ||
| detail = "Wildcard import detected", | ||
| canBeAutoCorrected = true | ||
| ) | ||
| ), | ||
| "src/main/kotlin/File2.kt" to listOf( | ||
| SerializableLintError( | ||
| line = 5, | ||
| col = 10, | ||
| ruleId = "no-unused-imports", | ||
| detail = "Unused import", | ||
| canBeAutoCorrected = false | ||
| ), | ||
| SerializableLintError( | ||
| line = 10, | ||
| col = 5, | ||
| ruleId = "no-trailing-whitespace", | ||
| detail = "Trailing whitespace", | ||
| canBeAutoCorrected = true | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| reporter.reportProblems(errors, false) // ignoreFailures = false, so ERROR severity | ||
|
|
||
| // Should report 3 total errors | ||
| verify(problemReporter, times(3)).report(any(), any()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `given no errors, it does not report any problems`() { | ||
| val emptyErrors = emptyMap<String, List<SerializableLintError>>() | ||
|
|
||
| reporter.reportProblems(emptyErrors, false) | ||
|
|
||
| verify(problemReporter, never()).report(any(), any()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `severity is WARNING when ignoreFailures is true`() { | ||
| val errors = mapOf( | ||
| "src/main/kotlin/TestFile.kt" to listOf( | ||
| SerializableLintError( | ||
| line = 1, | ||
| col = 1, | ||
| ruleId = "test-rule", | ||
| detail = "Test error", | ||
| canBeAutoCorrected = false | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| reporter.reportProblems(errors, true) // ignoreFailures = true, so WARNING severity | ||
|
|
||
| val specCaptor = argumentCaptor<Action<ProblemSpec>>() | ||
| verify(problemReporter).report(any(), specCaptor.capture()) | ||
|
|
||
| val spec: ProblemSpec = mock() | ||
| specCaptor.firstValue.execute(spec) | ||
|
|
||
| verify(spec).severity(GradleSeverity.WARNING) | ||
| } | ||
|
|
||
| @Test | ||
| fun `reports correct problem group and id`() { | ||
| val error = SerializableLintError( | ||
| line = 1, | ||
| col = 1, | ||
| ruleId = "no-wildcard-imports", | ||
| detail = "Wildcard import detected", | ||
| canBeAutoCorrected = true | ||
| ) | ||
| val filePath = "src/main/kotlin/TestFile.kt" | ||
|
|
||
| reporter.reportProblem(error, filePath, GradleSeverity.ERROR) | ||
|
|
||
| val idCaptor = argumentCaptor<org.gradle.api.problems.ProblemId>() | ||
| verify(problemReporter).report(idCaptor.capture(), any()) | ||
|
|
||
| val capturedId = idCaptor.firstValue | ||
| assertThat(capturedId.name).isEqualTo("no-wildcard-imports") | ||
| assertThat(capturedId.displayName).isEqualTo("Wildcard import detected") | ||
| assertThat(capturedId.group.name).isEqualTo("ktlint-gradle") | ||
| assertThat(capturedId.group.displayName).isEqualTo("ktlint-gradle issue") | ||
| } | ||
|
|
||
| @Test | ||
| fun `handles different error types correctly`() { | ||
| val autoCorrectableError = SerializableLintError( | ||
| line = 1, | ||
| col = 1, | ||
| ruleId = "auto-correctable", | ||
| detail = "Can be fixed", | ||
| canBeAutoCorrected = true | ||
| ) | ||
| val nonAutoCorrectableError = SerializableLintError( | ||
| line = 2, | ||
| col = 1, | ||
| ruleId = "manual-fix", | ||
| detail = "Must fix manually", | ||
| canBeAutoCorrected = false | ||
| ) | ||
|
|
||
| reporter.reportProblem(autoCorrectableError, "test.kt", GradleSeverity.ERROR) | ||
| reporter.reportProblem(nonAutoCorrectableError, "test.kt", GradleSeverity.ERROR) | ||
|
|
||
| // Both should be reported with ERROR severity | ||
| verify(problemReporter, times(2)).report(any(), any()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `reports errors with correct line and column information`() { | ||
| val error = SerializableLintError( | ||
| line = 42, | ||
| col = 15, | ||
| ruleId = "test-rule", | ||
| detail = "Test error", | ||
| canBeAutoCorrected = false | ||
| ) | ||
| val filePath = "src/main/kotlin/ComplexFile.kt" | ||
|
|
||
| reporter.reportProblem(error, filePath, GradleSeverity.ERROR) | ||
|
|
||
| val specCaptor = argumentCaptor<Action<ProblemSpec>>() | ||
| verify(problemReporter).report(any(), specCaptor.capture()) | ||
|
|
||
| val spec: ProblemSpec = mock() | ||
| specCaptor.firstValue.execute(spec) | ||
|
|
||
| verify(spec).lineInFileLocation(eq(filePath), eq(42), eq(15)) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.