Skip to content

Commit 7153e9e

Browse files
Refactor isFileIncluded (#90)
* Add simple test for isFilesIncluded * Bump version & add note to CHANGELOG.md
1 parent cf906a9 commit 7153e9e

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
## [Unreleased]
77

8+
### Changed
9+
- Refactor error handling for failure during file inclusion check to be more verbose
10+
811
## [2.1.1] - 2023-08-02
912

1013
### Changed

build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,15 @@ tasks {
131131
)
132132
})
133133
}
134+
135+
test {
136+
useJUnitPlatform()
137+
}
134138
}
135139

136140
dependencies {
137141
implementation(kotlin("stdlib"))
138142
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
139143
implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.0")
144+
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
140145
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pluginGroup=com.schwarzit.spectral-intellij-plugin
33
pluginName=Spectral
44
pluginRepositoryUrl=https://github.com/SchwarzIT/spectral-intellij-plugin
55
# SemVer format -> https://semver.org
6-
pluginVersion=2.1.1
6+
pluginVersion=2.1.2
77
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
88
pluginSinceBuild=222
99
#pluginUntilBuild=231.*

src/main/kotlin/com/schwarzit/spectralIntellijPlugin/SpectralExternalAnnotator.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.intellij.psi.PsiFile
1616
import com.schwarzit.spectralIntellijPlugin.settings.ProjectSettingsState
1717
import org.jetbrains.yaml.psi.YAMLFile
1818
import java.nio.file.FileSystems
19+
import java.nio.file.Path
1920
import java.nio.file.Paths
2021

2122
class SpectralExternalAnnotator : ExternalAnnotator<Pair<PsiFile, Editor>, List<SpectralIssue>>() {
@@ -26,30 +27,31 @@ class SpectralExternalAnnotator : ExternalAnnotator<Pair<PsiFile, Editor>, List<
2627
override fun collectInformation(file: PsiFile, editor: Editor, hasErrors: Boolean): Pair<PsiFile, Editor>? {
2728
if (file !is JsonFile && file !is YAMLFile) return null
2829

30+
val settings = editor.project?.service<ProjectSettingsState>()
31+
val includedFiles = settings?.includedFiles?.lines() ?: emptyList()
32+
2933
try {
30-
val settings = editor.project?.service<ProjectSettingsState>()
31-
val includedFiles = settings?.includedFiles?.lines() ?: emptyList()
32-
if (!isFileIncluded(file, includedFiles)) return null
34+
if (!isFileIncluded(file.project.basePath ?: "", file.virtualFile.toNioPath(), includedFiles)) return null
3335
} catch (e: Throwable) {
34-
logger.error("Failed to check if current file is included")
36+
logger.error("Failed to check if current file is included. Parameters: basePath: ${file.project.basePath}, path: ${file.virtualFile.toNioPath()}, includedFiles: $includedFiles")
3537
return null
3638
}
3739

3840
return Pair(file, editor)
3941
}
4042

41-
private fun isFileIncluded(file: PsiFile, includedFiles: List<String>): Boolean {
43+
fun isFileIncluded(basePath: String, path: Path, includedFiles: List<String>): Boolean {
4244
var matcherPattern = "glob:{"
4345
val iterator = includedFiles.iterator()
4446
while (iterator.hasNext()) {
4547
val pathPattern = iterator.next()
46-
if (!Paths.get(pathPattern).isAbsolute) matcherPattern += file.project.basePath + "/"
48+
if (!Paths.get(pathPattern).isAbsolute) matcherPattern += "$basePath/"
4749
matcherPattern += pathPattern
4850
matcherPattern += if (iterator.hasNext()) "," else "}"
4951
}
5052
logger.trace(matcherPattern)
5153
val fileMatcher = FileSystems.getDefault().getPathMatcher(matcherPattern)
52-
return fileMatcher.matches(file.virtualFile.toNioPath())
54+
return fileMatcher.matches(path)
5355
}
5456

5557
override fun doAnnotate(info: Pair<PsiFile, Editor>): List<SpectralIssue> {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.schwarzit.spectralIntellijPlugin
2+
3+
import org.junit.jupiter.api.Assertions
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
7+
import java.nio.file.Paths
8+
import java.util.stream.Stream
9+
10+
class SpectralExternalAnnotatorTest {
11+
12+
@ParameterizedTest(name = "isFileIncluded {index} - {arguments}")
13+
@MethodSource("provideIsFileIncludedTestParams")
14+
fun isFileIncluded(basePath: String, path: String, includedFiles: List<String>, isIncluded: Boolean) {
15+
val spectralExternalAnnotator = SpectralExternalAnnotator()
16+
val fileIncluded = spectralExternalAnnotator.isFileIncluded(basePath, Paths.get(path), includedFiles)
17+
Assertions.assertEquals(fileIncluded, isIncluded)
18+
}
19+
20+
companion object {
21+
@JvmStatic
22+
fun provideIsFileIncludedTestParams(): Stream<Arguments> {
23+
return Stream.of(
24+
Arguments.of(
25+
"/home/user/project",
26+
"/home/user/project/src/openapi.json",
27+
listOf("**openapi.json"),
28+
true
29+
)
30+
)
31+
}
32+
}
33+
34+
}

0 commit comments

Comments
 (0)