-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from deviceinsight/feature/allow-excluding-fi…
…les-from-property-replacement Allow excluding files from property replacement
- Loading branch information
Showing
6 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/com/deviceinsight/helm/PropertyReplacement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.deviceinsight.helm | ||
|
||
import org.apache.maven.plugins.annotations.Parameter | ||
import java.io.File | ||
import java.nio.file.FileSystems | ||
import java.nio.file.PathMatcher | ||
|
||
class PropertyReplacement { | ||
|
||
@Parameter(property = "exclusions") | ||
var exclusions: List<String> = emptyList() | ||
|
||
private val exclusionPatchMatchers = lazy { exclusions.toPathMatchers() } | ||
|
||
companion object { | ||
private val SUBSTITUTED_EXTENSIONS = setOf("json", "tpl", "yml", "yaml") | ||
private val PATH_MATCHER_PATTERN = Regex("^(glob|regex):.*$") | ||
} | ||
|
||
fun isPropertyReplacementCandidate(file: File): Boolean { | ||
val extension = file.extension.lowercase() | ||
return SUBSTITUTED_EXTENSIONS.contains(extension) | ||
&& file.doesNotMatchAnyExclusion() | ||
} | ||
|
||
private fun File.doesNotMatchAnyExclusion() = exclusionPatchMatchers.value.none { it.matches(this.toPath()) } | ||
|
||
private fun List<String>.toPathMatchers(): List<PathMatcher> = this | ||
.map { prependGlobIfMissing(it) } | ||
.map { FileSystems.getDefault().getPathMatcher(it) } | ||
|
||
private fun prependGlobIfMissing(path: String): String = if (PATH_MATCHER_PATTERN.matches(path)) path else "glob:$path" | ||
|
||
} | ||
|
35 changes: 35 additions & 0 deletions
35
src/test/kotlin/com/deviceinsight/helm/PropertyReplacementTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.deviceinsight.helm | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ValueSource | ||
import java.io.File | ||
|
||
class PropertyReplacementTest { | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = ["test.txt", "test.xml"]) | ||
fun `isPropertyReplacementCandidate should return false if file extension is not in inclusion list`(filename: String) { | ||
val file = File(filename) | ||
val propertyReplacement = PropertyReplacement() | ||
assertThat(propertyReplacement.isPropertyReplacementCandidate(file)).isFalse | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = ["test.json", "test.tpl", "test.yml", "test.yaml"]) | ||
fun `isPropertyReplacementCandidate should return true if file extension is in inclusion list`(filename: String) { | ||
val file = File(filename) | ||
val propertyReplacement = PropertyReplacement() | ||
assertThat(propertyReplacement.isPropertyReplacementCandidate(file)).isTrue | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = ["dashboards/test.json", "templates/test.json", "templates/default/test.json"]) | ||
fun `isPropertyReplacementCandidate should return false if file matches exclusion pattern`(filename: String) { | ||
val file = File(filename) | ||
val propertyReplacement = PropertyReplacement() | ||
propertyReplacement.exclusions = | ||
listOf("regex:dashboards/.*\\.json", "glob:templates/*.json", "templates/**/*.json") | ||
assertThat(propertyReplacement.isPropertyReplacementCandidate(file)).isFalse | ||
} | ||
} |