-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(calculator): strip non-semver text (#164)
- Loading branch information
1 parent
431e247
commit 511853c
Showing
6 changed files
with
113 additions
and
3 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
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/figure/gradle/semver/internal/calculator/Matchers.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,22 @@ | ||
/* | ||
* Copyright (C) 2024 Figure Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.figure.gradle.semver.internal.calculator | ||
|
||
fun String.stripNonSemverText(): String { | ||
val versionPattern = Regex(Patterns.VERSION_REGEX) | ||
val matchResult = versionPattern.find(this) | ||
return matchResult?.value ?: this | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/com/figure/gradle/semver/internal/calculator/Patterns.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,47 @@ | ||
/* | ||
* Copyright (C) 2024 Figure Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.figure.gradle.semver.internal.calculator | ||
|
||
internal object Patterns { | ||
// Numeric identifier pattern. (used for parsing major, minor, and patch) | ||
private const val NUMERIC = "0|[1-9]\\d*" | ||
|
||
// Alphanumeric or hyphen pattern. | ||
private const val ALPHANUMERIC_OR_HYPHEN = "[0-9a-zA-Z-]" | ||
|
||
// Letter or hyphen pattern. | ||
private const val LETTER_OR_HYPHEN = "[a-zA-Z-]" | ||
|
||
// Non-numeric identifier pattern. (used for parsing pre-release) | ||
private const val NON_NUMERIC = "\\d*$LETTER_OR_HYPHEN$ALPHANUMERIC_OR_HYPHEN*" | ||
|
||
// Dot-separated numeric identifier pattern. (<major>.<minor>.<patch>) | ||
private const val CORE_VERSION = "($NUMERIC)\\.($NUMERIC)\\.($NUMERIC)" | ||
|
||
// Numeric or non-numeric pre-release part pattern. | ||
private const val PRE_RELEASE_PART = "(?:$NUMERIC|$NON_NUMERIC)" | ||
|
||
// Pre-release identifier pattern. A hyphen followed by dot-separated | ||
// numeric or non-numeric pre-release parts. | ||
private const val PRE_RELEASE = "(?:-($PRE_RELEASE_PART(?:\\.$PRE_RELEASE_PART)*))" | ||
|
||
// Build-metadata identifier pattern. A + sign followed by dot-separated | ||
// alphanumeric build-metadata parts. | ||
private const val BUILD = "(?:\\+($ALPHANUMERIC_OR_HYPHEN+(?:\\.$ALPHANUMERIC_OR_HYPHEN+)*))" | ||
|
||
// Version parsing pattern: 1.2.3-alpha+build | ||
internal const val VERSION_REGEX: String = "$CORE_VERSION$PRE_RELEASE?$BUILD?" | ||
} |
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
38 changes: 38 additions & 0 deletions
38
src/test/kotlin/com/figure/gradle/semver/internal/calculator/MatchersKtTest.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,38 @@ | ||
/* | ||
* Copyright (C) 2024 Figure Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.figure.gradle.semver.internal.calculator | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
class MatchersKtTest : FunSpec({ | ||
test("should strip off prefix and return version") { | ||
"v1.2.3".stripNonSemverText() shouldBe "1.2.3" | ||
"libraries-v1.5.4".stripNonSemverText() shouldBe "1.5.4" | ||
} | ||
|
||
test("should return input if no version found") { | ||
"no-version-here".stripNonSemverText() shouldBe "no-version-here" | ||
} | ||
|
||
test("should handle empty input") { | ||
"".stripNonSemverText() shouldBe "" | ||
} | ||
|
||
test("should handle input with only version") { | ||
"2.3.4".stripNonSemverText() shouldBe "2.3.4" | ||
} | ||
}) |