Skip to content

Commit

Permalink
Refactor isFileIncluded (#90)
Browse files Browse the repository at this point in the history
* Add simple test for isFilesIncluded

* Bump version & add note to CHANGELOG.md
  • Loading branch information
markbrockhoff authored Aug 22, 2023
1 parent cf906a9 commit 7153e9e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

## [Unreleased]

### Changed
- Refactor error handling for failure during file inclusion check to be more verbose

## [2.1.1] - 2023-08-02

### Changed
Expand Down
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,15 @@ tasks {
)
})
}

test {
useJUnitPlatform()
}
}

dependencies {
implementation(kotlin("stdlib"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pluginGroup=com.schwarzit.spectral-intellij-plugin
pluginName=Spectral
pluginRepositoryUrl=https://github.com/SchwarzIT/spectral-intellij-plugin
# SemVer format -> https://semver.org
pluginVersion=2.1.1
pluginVersion=2.1.2
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild=222
#pluginUntilBuild=231.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.intellij.psi.PsiFile
import com.schwarzit.spectralIntellijPlugin.settings.ProjectSettingsState
import org.jetbrains.yaml.psi.YAMLFile
import java.nio.file.FileSystems
import java.nio.file.Path
import java.nio.file.Paths

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

val settings = editor.project?.service<ProjectSettingsState>()
val includedFiles = settings?.includedFiles?.lines() ?: emptyList()

try {
val settings = editor.project?.service<ProjectSettingsState>()
val includedFiles = settings?.includedFiles?.lines() ?: emptyList()
if (!isFileIncluded(file, includedFiles)) return null
if (!isFileIncluded(file.project.basePath ?: "", file.virtualFile.toNioPath(), includedFiles)) return null
} catch (e: Throwable) {
logger.error("Failed to check if current file is included")
logger.error("Failed to check if current file is included. Parameters: basePath: ${file.project.basePath}, path: ${file.virtualFile.toNioPath()}, includedFiles: $includedFiles")
return null
}

return Pair(file, editor)
}

private fun isFileIncluded(file: PsiFile, includedFiles: List<String>): Boolean {
fun isFileIncluded(basePath: String, path: Path, includedFiles: List<String>): Boolean {
var matcherPattern = "glob:{"
val iterator = includedFiles.iterator()
while (iterator.hasNext()) {
val pathPattern = iterator.next()
if (!Paths.get(pathPattern).isAbsolute) matcherPattern += file.project.basePath + "/"
if (!Paths.get(pathPattern).isAbsolute) matcherPattern += "$basePath/"
matcherPattern += pathPattern
matcherPattern += if (iterator.hasNext()) "," else "}"
}
logger.trace(matcherPattern)
val fileMatcher = FileSystems.getDefault().getPathMatcher(matcherPattern)
return fileMatcher.matches(file.virtualFile.toNioPath())
return fileMatcher.matches(path)
}

override fun doAnnotate(info: Pair<PsiFile, Editor>): List<SpectralIssue> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.schwarzit.spectralIntellijPlugin

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.nio.file.Paths
import java.util.stream.Stream

class SpectralExternalAnnotatorTest {

@ParameterizedTest(name = "isFileIncluded {index} - {arguments}")
@MethodSource("provideIsFileIncludedTestParams")
fun isFileIncluded(basePath: String, path: String, includedFiles: List<String>, isIncluded: Boolean) {
val spectralExternalAnnotator = SpectralExternalAnnotator()
val fileIncluded = spectralExternalAnnotator.isFileIncluded(basePath, Paths.get(path), includedFiles)
Assertions.assertEquals(fileIncluded, isIncluded)
}

companion object {
@JvmStatic
fun provideIsFileIncludedTestParams(): Stream<Arguments> {
return Stream.of(
Arguments.of(
"/home/user/project",
"/home/user/project/src/openapi.json",
listOf("**openapi.json"),
true
)
)
}
}

}

0 comments on commit 7153e9e

Please sign in to comment.