Skip to content

Commit

Permalink
bug fix for file filter matching (#10)
Browse files Browse the repository at this point in the history
* bug fix for file filter matching

* bump version to 0.2.1
  • Loading branch information
marcoferrer authored Nov 8, 2018
1 parent 57f840f commit 35ff678
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 0.2.1
_2018-11-02_
* Fix: Address regression in file filter matching

## Version 0.2.0
_2018-11-02_
* New: Added doc generation for configuration api
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[ ![JCenter](https://api.bintray.com/packages/marcoferrer/kroto-plus/protoc-gen-kroto-plus/images/download.svg) ](https://bintray.com/marcoferrer/kroto-plus/protoc-gen-kroto-plus/_latestVersion)
[![Maven Central](https://img.shields.io/maven-central/v/com.github.marcoferrer.krotoplus/protoc-gen-kroto-plus.svg?label=Maven%20Central)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.github.marcoferrer.krotoplus%22%20a%3A%22protoc-gen-kroto-plus%22)

## Version 0.2.0
## Version 0.2.1
* [CHANGELOG](https://github.com/marcoferrer/kroto-plus/blob/master/CHANGELOG.md)
* Most notable changes
* Update to Kotlin 1.3.0
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ subprojects{ subproject ->
apply plugin: 'kotlin'

group = 'com.github.marcoferrer.krotoplus'
version = '0.2.0'
version = '0.2.1'

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
Expand Down
2 changes: 1 addition & 1 deletion community-scripts/extendable-messages/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def krotoPlusVersion = '0.2.0'
def krotoPlusVersion = '0.2.1'

plugins {
id "org.jetbrains.kotlin.jvm" version "1.3.0"
Expand Down
2 changes: 1 addition & 1 deletion community-scripts/var-arg-extensions/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def krotoPlusVersion = '0.2.0'
def krotoPlusVersion = '0.2.1'

plugins {
id "org.jetbrains.kotlin.jvm" version "1.3.0"
Expand Down
2 changes: 1 addition & 1 deletion example-project/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
"grpc": '1.15.1',
"kotlin": '1.3.0',
"coroutines": '1.0.0',
"krotoplus": '0.2.0'
"krotoplus": '0.2.1'
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ data class RegexFilter(val include: List<Regex>, val exclude: List<Regex>) {

fun isEmpty() = include.isEmpty() && exclude.isEmpty()

fun matches(value: String) =
isEmpty() || (include.any { it.matches(value) } || exclude.all { !it.matches(value) })
fun matches(value: String) = when{
include.isNotEmpty() -> include.any { it.matches(value) } && exclude.none { it.matches(value) }
exclude.isNotEmpty() -> exclude.none { it.matches(value) }
else -> true
}
}

fun globPatternToRegexString(globPattern: String): String = globPattern
Expand Down
108 changes: 108 additions & 0 deletions protoc-gen-kroto-plus/src/test/kotlin/FileFilterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.github.marcoferrer.krotoplus.utils

import com.github.marcoferrer.krotoplus.config.FileFilter
import kotlin.test.Test
import kotlin.test.assertEquals

class FileFilterTest {

val testPaths = listOf(
"google/protobuf",
"test/dummy/a/1",
"test/dummy/a/2",
"test/dummy/b/1",
"test/dummy/b/2",
"test/dummy/c/1",
"test/dummy/c/2"
)

@Test
fun `include all paths`(){
val fileFilter = FileFilter.getDefaultInstance()
val matches = testPaths.filter { fileFilter.matches(it) }
assertEquals(7, matches.size)
}

@Test
fun `Include single path`(){
val fileFilter = FileFilter.newBuilder().addIncludePath("test/dummy/a/*").build()
val matches = testPaths.filter { fileFilter.matches(it) }
assertEquals(2, matches.size)
assertEquals("test/dummy/a/1",matches[0])
assertEquals("test/dummy/a/2",matches[1])
}

@Test
fun `Include multiple paths`(){
val fileFilter = FileFilter.newBuilder()
.addIncludePath("test/dummy/a/*")
.addIncludePath("google/*")
.build()
val matches = testPaths.filter { fileFilter.matches(it) }
assertEquals(3, matches.size)
assertEquals("google/protobuf",matches[0])
assertEquals("test/dummy/a/1",matches[1])
assertEquals("test/dummy/a/2",matches[2])
}

@Test
fun `Exclude single path`(){
val fileFilter = FileFilter.newBuilder()
.addExcludePath("google/*")
.build()

val matches = testPaths.filter { fileFilter.matches(it) }
assertEquals(6, matches.size)
assertEquals("test/dummy/a/1",matches[0])
assertEquals("test/dummy/a/2",matches[1])
assertEquals("test/dummy/b/1",matches[2])
assertEquals("test/dummy/b/2",matches[3])
assertEquals("test/dummy/c/1",matches[4])
assertEquals("test/dummy/c/2",matches[5])
}

@Test
fun `Exclude multiple paths`(){
val fileFilter = FileFilter.newBuilder()
.addExcludePath("google/*")
.addExcludePath("test/dummy/b/*")
.build()

val matches = testPaths.filter { fileFilter.matches(it) }
assertEquals(4, matches.size)
assertEquals("test/dummy/a/1",matches[0])
assertEquals("test/dummy/a/2",matches[1])
assertEquals("test/dummy/c/1",matches[2])
assertEquals("test/dummy/c/2",matches[3])
}

@Test
fun `Include and exclude paths`(){
val fileFilter = FileFilter.newBuilder()
.addIncludePath("test/dummy/*")
.addExcludePath("test/dummy/*/1")
.build()

val matches = testPaths.filter { fileFilter.matches(it) }
assertEquals(3, matches.size)
assertEquals("test/dummy/a/2",matches[0])
assertEquals("test/dummy/b/2",matches[1])
assertEquals("test/dummy/c/2",matches[2])
}

@Test
fun `Include and exclude multiple paths`(){
val fileFilter = FileFilter.newBuilder()
.addIncludePath("google/*")
.addIncludePath("test/dummy/*/1")
.addExcludePath("test/dummy/a/*")
.addExcludePath("test/dummy/c/*")
.build()

val matches = testPaths.filter { fileFilter.matches(it) }
assertEquals(2, matches.size)
assertEquals("google/protobuf",matches[0])
assertEquals("test/dummy/b/1",matches[1])
}

}

0 comments on commit 35ff678

Please sign in to comment.