-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix a problem where the provider version trim property is ignored
- Loading branch information
Showing
9 changed files
with
92 additions
and
89 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
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
43 changes: 43 additions & 0 deletions
43
provider/src/main/kotlin/au/com/dius/pact/provider/ProviderVersion.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,43 @@ | ||
package au.com.dius.pact.provider | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import java.util.function.Supplier | ||
|
||
/** | ||
* Should always wrap a provider version string with this Supplier in order to avoid repeating any logic. | ||
*/ | ||
class ProviderVersion(var source: String?) : Supplier<String> { | ||
|
||
companion object { | ||
const val FALLBACK_VALUE = "0.0.0" | ||
const val SNAPSHOT_DEFINITION_STRING = "-SNAPSHOT" | ||
val snapshotRegex = Regex(".*($SNAPSHOT_DEFINITION_STRING)") | ||
val logger: Logger = LoggerFactory.getLogger(ProviderVersion::class.java) | ||
} | ||
|
||
init { | ||
source = source ?: FALLBACK_VALUE | ||
|
||
if (source == FALLBACK_VALUE) { | ||
logger.warn("Provider version not set, defaulting to '$FALLBACK_VALUE") | ||
} | ||
} | ||
|
||
override fun get(): String { | ||
val trimSnapshotProperty = System.getProperty(ProviderVerifier.PACT_PROVIDER_VERSION_TRIM_SNAPSHOT) | ||
val isTrimSnapshot: Boolean = if (trimSnapshotProperty == null || trimSnapshotProperty.isBlank()) { | ||
false | ||
} else { | ||
trimSnapshotProperty.toBoolean() | ||
} | ||
return if (isTrimSnapshot) trimSnapshot(source!!) else source!! | ||
} | ||
|
||
private fun trimSnapshot(providerVersion: String): String { | ||
if (providerVersion.contains(SNAPSHOT_DEFINITION_STRING)) { | ||
return providerVersion.removeRange(snapshotRegex.find(providerVersion)!!.groups[1]!!.range) | ||
} | ||
return providerVersion | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
provider/src/test/groovy/au/com/dius/pact/provider/ProviderVersionSpec.groovy
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,30 @@ | ||
package au.com.dius.pact.provider | ||
|
||
import spock.lang.Specification | ||
|
||
class ProviderVersionSpec extends Specification { | ||
|
||
def cleanup() { | ||
System.clearProperty(ProviderVerifier.PACT_PROVIDER_VERSION_TRIM_SNAPSHOT) | ||
} | ||
|
||
def 'provider version respects the property pact.provider.version.trimSnapshot'() { | ||
|
||
given: | ||
System.setProperty(ProviderVerifier.PACT_PROVIDER_VERSION_TRIM_SNAPSHOT, propertyValue as String) | ||
|
||
expect: | ||
new ProviderVersion(projectVersion).get() == result | ||
|
||
where: | ||
propertyValue | projectVersion | result | ||
'true' | '1.0.0-NOT-A-SNAPSHOT-abc-SNAPSHOT' | '1.0.0-NOT-A-SNAPSHOT-abc' | ||
'true' | '1.0.0-NOT-A-SNAPSHOT-abc-SNAPSHOT-re234hj' | '1.0.0-NOT-A-SNAPSHOT-abc-re234hj' | ||
'true' | '1.0.0-SNAPSHOT-re234hj' | '1.0.0-re234hj' | ||
'false' | '1.0.0-SNAPSHOT-re234hj' | '1.0.0-SNAPSHOT-re234hj' | ||
'aweirdstring' | '1.0.0-SNAPSHOT-re234hj' | '1.0.0-SNAPSHOT-re234hj' | ||
'true' | null | '0.0.0' | ||
'false' | null | '0.0.0' | ||
_ | '1.0.0-SNAPSHOT-re234hj' | '1.0.0-SNAPSHOT-re234hj' | ||
} | ||
} |