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
Make plugin testable by stubbing the detekt instance avoiding compiler classpath errors #177
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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
1 change: 0 additions & 1 deletion
1
src/main/kotlin/io/gitlab/arturbosch/detekt/idea/config/DetektConfigStorage.kt
This file contains 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 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 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 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 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 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
95 changes: 95 additions & 0 deletions
95
src/test/kotlin/io/gitlab/arturbosch/detekt/idea/DetektStub.kt
This file contains 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,95 @@ | ||
package io.gitlab.arturbosch.detekt.idea | ||
|
||
import io.github.detekt.psi.FilePath | ||
import io.github.detekt.tooling.api.AnalysisResult | ||
import io.github.detekt.tooling.api.Detekt | ||
import io.github.detekt.tooling.api.DetektProvider | ||
import io.github.detekt.tooling.api.spec.ProcessingSpec | ||
import io.github.detekt.tooling.internal.DefaultAnalysisResult | ||
import io.gitlab.arturbosch.detekt.api.CodeSmell | ||
import io.gitlab.arturbosch.detekt.api.Debt | ||
import io.gitlab.arturbosch.detekt.api.Detektion | ||
import io.gitlab.arturbosch.detekt.api.Entity | ||
import io.gitlab.arturbosch.detekt.api.Finding | ||
import io.gitlab.arturbosch.detekt.api.Issue | ||
import io.gitlab.arturbosch.detekt.api.Location | ||
import io.gitlab.arturbosch.detekt.api.Notification | ||
import io.gitlab.arturbosch.detekt.api.ProjectMetric | ||
import io.gitlab.arturbosch.detekt.api.RuleSetId | ||
import io.gitlab.arturbosch.detekt.api.Severity | ||
import io.gitlab.arturbosch.detekt.api.SourceLocation | ||
import io.gitlab.arturbosch.detekt.api.TextLocation | ||
import org.jetbrains.kotlin.com.intellij.openapi.util.Key | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import org.jetbrains.kotlin.resolve.BindingContext | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
|
||
class DetektProviderStub : DetektProvider { | ||
|
||
override fun get(processingSpec: ProcessingSpec): Detekt = DetektStub() | ||
} | ||
|
||
class DetektStub : Detekt { | ||
|
||
override fun run(): AnalysisResult { | ||
throw UnsupportedOperationException() | ||
} | ||
|
||
override fun run(path: Path): AnalysisResult { | ||
throw UnsupportedOperationException() | ||
} | ||
|
||
override fun run(sourceCode: String, filename: String): AnalysisResult { | ||
if (!filename.contains("Poko.kt")) { | ||
throw UnsupportedOperationException("Only Poko.kt runs are supported.") | ||
} | ||
return DefaultAnalysisResult(object : Detektion { | ||
override val findings: Map<RuleSetId, List<Finding>> = mapOf( | ||
"empty-blocks" to listOf( | ||
CodeSmell( | ||
Issue( | ||
"EmptyDefaultConstructor", | ||
Severity.Minor, | ||
"empty", | ||
Debt.FIVE_MINS | ||
), | ||
Entity( | ||
"Poko", | ||
"testData.Poko.kt", | ||
Location( | ||
SourceLocation(3, 10), | ||
TextLocation(28, 30), | ||
FilePath(Paths.get(filename)) | ||
) | ||
), | ||
"empty constructor" | ||
) | ||
) | ||
) | ||
override val metrics: Collection<ProjectMetric> = emptyList() | ||
override val notifications: Collection<Notification> = emptyList() | ||
|
||
override fun add(notification: Notification) { | ||
// ignore | ||
} | ||
|
||
override fun add(projectMetric: ProjectMetric) { | ||
// ignore | ||
} | ||
|
||
override fun <V> addData(key: Key<V>, value: V) { | ||
// ignore | ||
} | ||
|
||
override fun <V> getData(key: Key<V>): V? { | ||
// ignore | ||
return null | ||
} | ||
}) | ||
} | ||
|
||
override fun run(files: Collection<KtFile>, bindingContext: BindingContext): AnalysisResult { | ||
throw UnsupportedOperationException() | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/test/resources/META-INF/services/io.github.detekt.tooling.api.DetektProvider
This file contains 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 @@ | ||
io.gitlab.arturbosch.detekt.idea.DetektProviderStub |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line was wrong. It would have skipped every Kotlin file but did not because apparently
psiFile.language != KotlinLanguage.INSTANCE
,Fixing the tests made this mistake obvious.
Testing for
KtFile
does not work either as IntelliJ may use some kind of LightPsiElement structures.We have to compare language id's here. Kotlin light elements also use KotlinLanguage.INSTANCE.