Skip to content

Commit

Permalink
Move PathFilters from detekt-api to detekt-utils (#6866)
Browse files Browse the repository at this point in the history
* Move test to the correct project

* Simplify code

* Move PathFilters to detekt-utils
  • Loading branch information
BraisGabin committed May 6, 2024
1 parent 731735b commit 60b2413
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 53 deletions.
1 change: 1 addition & 0 deletions detekt-cli/build.gradle.kts
Expand Up @@ -22,6 +22,7 @@ val pluginsJarFiles by configurations.resolvable("pluginsJarFiles") {
dependencies {
implementation(libs.jcommander)
implementation(projects.detektTooling)
implementation(projects.detektUtils)
implementation(libs.kotlin.compilerEmbeddable) {
version {
strictly(libs.versions.kotlin.get())
Expand Down
Expand Up @@ -2,9 +2,9 @@ package io.gitlab.arturbosch.detekt.cli

import io.github.detekt.tooling.api.spec.ProcessingSpec
import io.github.detekt.tooling.api.spec.RulesSpec
import io.github.detekt.utils.PathFilters
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.RuleSet
import io.gitlab.arturbosch.detekt.api.internal.PathFilters
import java.nio.file.Path
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.absolute
Expand Down

This file was deleted.

@@ -1,8 +1,8 @@
package io.gitlab.arturbosch.detekt.core.util

import io.github.detekt.psi.absolutePath
import io.github.detekt.utils.PathFilters
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.internal.PathFilters
import org.jetbrains.kotlin.psi.KtFile
import java.nio.file.Path
import kotlin.io.path.Path
Expand Down
@@ -1,4 +1,4 @@
package io.gitlab.arturbosch.detekt.api.internal
package io.github.detekt.utils

import java.nio.file.Path
import java.nio.file.PathMatcher
Expand Down
@@ -1,4 +1,4 @@
package io.gitlab.arturbosch.detekt.api.internal
package io.github.detekt.utils

import java.nio.file.FileSystem
import java.nio.file.FileSystems
Expand All @@ -9,7 +9,7 @@ import java.nio.file.PathMatcher
* We only support the "glob:" syntax to stay os independently.
* Internally a globbing pattern is transformed to a regex respecting the Windows file system.
*/
fun pathMatcher(pattern: String): PathMatcher {
internal fun pathMatcher(pattern: String): PathMatcher {
val result = when (pattern.substringBefore(":")) {
"glob" -> pattern
"regex" -> throw IllegalArgumentException(USE_GLOB_MSG)
Expand Down
@@ -0,0 +1,42 @@
package io.github.detekt.utils

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import kotlin.io.path.Path

class PathFiltersSpec {

@Test
fun `returns an empty path filter when includes are empty and excludes are empty`() {
val pathFilter = PathFilters.of(emptyList(), emptyList())
assertThat(pathFilter).isNull()
}

@Test
fun `parses includes correctly`() {
val pathFilter = PathFilters.of(listOf("**/one/**", "**/two/**"), emptyList())
assertThat(pathFilter).isNotNull
assertThat(pathFilter?.isIgnored(Path("/one/path"))).isFalse
assertThat(pathFilter?.isIgnored(Path("/two/path"))).isFalse
assertThat(pathFilter?.isIgnored(Path("/three/path"))).isTrue
}

@Test
fun `parses excludes correctly`() {
val pathFilter = PathFilters.of(emptyList(), listOf("**/one/**", "**/two/**"))
assertThat(pathFilter).isNotNull
assertThat(pathFilter?.isIgnored(Path("/one/path"))).isTrue
assertThat(pathFilter?.isIgnored(Path("/two/path"))).isTrue
assertThat(pathFilter?.isIgnored(Path("/three/path"))).isFalse
}

@Test
fun `parses both includes and excludes correctly`() {
val pathFilter = PathFilters.of(listOf("**/one/**"), listOf("**/two/**"))
assertThat(pathFilter).isNotNull
assertThat(pathFilter?.isIgnored(Path("/one/path"))).isFalse
assertThat(pathFilter?.isIgnored(Path("/two/path"))).isTrue
assertThat(pathFilter?.isIgnored(Path("/three/path"))).isTrue
assertThat(pathFilter?.isIgnored(Path("/one/two/three/path"))).isTrue
}
}
@@ -1,4 +1,4 @@
package io.gitlab.arturbosch.detekt.api.internal
package io.github.detekt.utils

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
Expand Down

0 comments on commit 60b2413

Please sign in to comment.