From 853fe590a2e6d1bb5f327ddda2e0ebbcfbfdff24 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Fri, 21 Feb 2020 09:17:57 +0800 Subject: [PATCH] feat: add filter rule for chapi config --- .../chapi/app/analyser/config/ChapiConfig.kt | 3 ++- .../chapi/app/analyser/filter/FileFilter.kt | 4 +++ .../app/analyser/filter/FileFilterTest.kt | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 chapi-application/src/test/kotlin/chapi/app/analyser/filter/FileFilterTest.kt diff --git a/chapi-application/src/main/kotlin/chapi/app/analyser/config/ChapiConfig.kt b/chapi-application/src/main/kotlin/chapi/app/analyser/config/ChapiConfig.kt index 32468fc2..83076530 100644 --- a/chapi-application/src/main/kotlin/chapi/app/analyser/config/ChapiConfig.kt +++ b/chapi-application/src/main/kotlin/chapi/app/analyser/config/ChapiConfig.kt @@ -2,7 +2,8 @@ package chapi.app.analyser.config data class ChapiConfig( var language: String = "", - var withoutTest: Boolean = false + var withoutTest: Boolean = false, + var filterRule: String = "" ) { } diff --git a/chapi-application/src/main/kotlin/chapi/app/analyser/filter/FileFilter.kt b/chapi-application/src/main/kotlin/chapi/app/analyser/filter/FileFilter.kt index 8ce69552..d7d918e5 100644 --- a/chapi-application/src/main/kotlin/chapi/app/analyser/filter/FileFilter.kt +++ b/chapi-application/src/main/kotlin/chapi/app/analyser/filter/FileFilter.kt @@ -5,6 +5,10 @@ import chapi.app.analyser.config.ChapiConfig open class FileFilter { companion object { fun filterByLanguage(path: String, config: ChapiConfig): Boolean { + if (config.filterRule.isNotEmpty()) { + return config.filterRule.toRegex().matches(path) + } + return when (config.language) { "java" -> { path.endsWith(".java") diff --git a/chapi-application/src/test/kotlin/chapi/app/analyser/filter/FileFilterTest.kt b/chapi-application/src/test/kotlin/chapi/app/analyser/filter/FileFilterTest.kt new file mode 100644 index 00000000..2c3c6265 --- /dev/null +++ b/chapi-application/src/test/kotlin/chapi/app/analyser/filter/FileFilterTest.kt @@ -0,0 +1,26 @@ +package chapi.app.analyser.filter + +import chapi.app.analyser.config.ChapiConfig +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +internal class FileFilterTest { + @Test + internal fun shouldReturnTrueWhenInputLangJava() { + val config = ChapiConfig( + language = "java" + ) + val isJavaFile = FileFilter.filterByLanguage("hello.java", config) + assertEquals(isJavaFile, true) + } + + @Test + internal fun shouldEnableRegexToMatchFile() { + val config = ChapiConfig( + language = "java", + filterRule = ".*\\.java" + ) + val isJavaFile = FileFilter.filterByLanguage("hello.java", config) + assertEquals(isJavaFile, true) + } +}