Skip to content

Commit

Permalink
✨ adding string::compareSemanticVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Jul 1, 2024
1 parent 3fc810d commit a554c72
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 9 deletions.
30 changes: 30 additions & 0 deletions tests.d/1003-lib-string/00.tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function testString::bumpSemanticVersion() {
echo "→ bumping 1.2.3-alpha patch false"
string::bumpSemanticVersion "1.2.156-alpha" "patch" "false" && echo "${RETURNED_VALUE}"

echo
echo "→ bumping aze patch false"
(string::bumpSemanticVersion "aze" "patch" "false") || echo "Failed as expected"

test::endTest "Testing string::bumpSemanticVersion" 0
}

Expand Down Expand Up @@ -220,6 +224,31 @@ function testString::trim() {
test::endTest "Testing string::trim function" 0
}

function testString::compareSemanticVersion() {
echo "→ string::compareSemanticVersion '1.2.3' '1.2.3'"
string::compareSemanticVersion '1.2.3' '1.2.3' && echo "0=${RETURNED_VALUE}"
echo
echo "→ string::compareSemanticVersion '1.2.3-alpha' '1.2.4+az123'"
string::compareSemanticVersion '1.2.3-alpha' '1.2.4+az123' && echo "-1=${RETURNED_VALUE}"
echo
echo "→ string::compareSemanticVersion '1.2.3' '1.2.2'"
string::compareSemanticVersion '1.2.3' '1.2.2' && echo "1=${RETURNED_VALUE}"
echo
echo "→ string::compareSemanticVersion '2.2.3' '1.2.3-alpha'"
string::compareSemanticVersion '2.2.3' '1.2.3-alpha' && echo "1=${RETURNED_VALUE}"
echo
echo "→ string::compareSemanticVersion '1.2.3+a1212' '1.3.3'"
string::compareSemanticVersion '1.2.3+a1212' '1.3.3' && echo "-1=${RETURNED_VALUE}"
echo
echo "→ string::compareSemanticVersion '1.2.3-alpha+a123123' '1.2.3-alpha+123zer'"
string::compareSemanticVersion '1.2.3-alpha+a123123' '1.2.3-alpha+123zer' && echo "0=${RETURNED_VALUE}"
echo
echo "→ string::compareSemanticVersion '1.2a.3' '1.2.3derp'"
(string::compareSemanticVersion '1.2a.3' '1.2.3derp') || echo "Failed as expected"

test::endTest "Testing string::compareSemanticVersion function" 0
}

function main() {
testString::bumpSemanticVersion
testString::kebabCaseToSnakeCase
Expand All @@ -233,6 +262,7 @@ function main() {
testString::split
testString::regexGetFirst
testString::trim
testString::compareSemanticVersion
}

main
44 changes: 44 additions & 0 deletions tests.d/1003-lib-string/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ Exit code: `0`
→ bumping 1.2.3-alpha patch false
1.2.157-alpha
→ bumping aze patch false
Failed as expected
```

**Error** output:

```log
ERROR Failed to bump the version ⌜aze⌝ because it is not valid semantic version.
```

### Testing string::kebabCaseToSnakeCase
Expand Down Expand Up @@ -238,3 +247,38 @@ hello=⌜hello⌝
hello=⌜hello⌝
```

### Testing string::compareSemanticVersion function

Exit code: `0`

**Standard** output:

```plaintext
→ string::compareSemanticVersion '1.2.3' '1.2.3'
0=0
→ string::compareSemanticVersion '1.2.3-alpha' '1.2.4+az123'
-1=-1
→ string::compareSemanticVersion '1.2.3' '1.2.2'
1=1
→ string::compareSemanticVersion '2.2.3' '1.2.3-alpha'
1=1
→ string::compareSemanticVersion '1.2.3+a1212' '1.3.3'
-1=-1
→ string::compareSemanticVersion '1.2.3-alpha+a123123' '1.2.3-alpha+123zer'
0=0
→ string::compareSemanticVersion '1.2a.3' '1.2.3derp'
Failed as expected
```

**Error** output:

```log
ERROR Failed to compare versions ⌜1.2a.3⌝ and ⌜1.2.3derp⌝ because they are not valid semantic versions.
```

10 changes: 3 additions & 7 deletions valet.d/lib-io
Original file line number Diff line number Diff line change
Expand Up @@ -844,13 +844,9 @@ function io::listDirectories() {
# fi
# ```
function io::isDirectoryWritable() {
path="${1:-}/writable-test"
if (: >"${path}") &>/dev/null; then
if ! rm "${path}" 1>/dev/null; then
return 1
fi
return 0
else
local path="${1:-}/writable-test-${BASHPID}"
if ! (: >"${path}") &>/dev/null || ! rm "${path}" 1>/dev/null; then
return 1
fi
return 0
}
57 changes: 55 additions & 2 deletions valet.d/lib-string
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,55 @@ function string::cutField() {
RETURNED_VALUE=""
}

# ## string::compareSemanticVersion
#
# This function allows to compare two semantic versions formatted like:
# major.minor.patch-prerelease+build
#
# - $1: **version1** _as string_:
# the first version to compare
# - $2: **version2** _as string_:
# the second version to compare
#
# Returns:
#
# - `RETURNED_VALUE`:
# - 0 if the versions are equal,
# - 1 if version1 is greater,
# - -1 if version2 is greater
#
# ```bash
# string::compareSemanticVersion "2.3.4-prerelease+build" "1.2.3-prerelease+build"
# local comparison="${RETURNED_VALUE}"
# ```
#
# > The prerelease and build are ignored in the comparison.
function string::compareSemanticVersion() {
local version1="${1#v}"
local version2="${2#v}"

local -i semVerIndex
local semVerNumber1 semVerNumber2
for semVerIndex in {0..2}; do
string::cutField "${version1}" "${semVerIndex}" "."
semVerNumber1="${RETURNED_VALUE%%-*}"
semVerNumber1="${semVerNumber1%%+*}"
string::cutField "${version2}" "${semVerIndex}" "."
semVerNumber2="${RETURNED_VALUE%%-*}"
semVerNumber2="${semVerNumber2%%+*}"
if [[ ! ${semVerNumber1} =~ ^[0-9]+$ || ! ${semVerNumber2} =~ ^[0-9]+$ ]]; then
core::fail "Failed to compare versions ⌜${version1}⌝ and ⌜${version2}⌝ because they are not valid semantic versions."
elif (( semVerNumber1 > semVerNumber2 )); then
RETURNED_VALUE=1
return 0
elif (( semVerNumber1 < semVerNumber2 )); then
RETURNED_VALUE=-1
return 0
fi
done
RETURNED_VALUE=0
}

# ## string::bumpSemanticVersion
#
# This function allows to bump a semantic version formatted like:
Expand Down Expand Up @@ -92,7 +141,11 @@ function string::bumpSemanticVersion() {
if [[ ${bumpLevel:-} == "minor" ]]; then level=1; fi
local newVersion semVerString
for semVerIndex in {0..2}; do
string::cutField "${version}" "${semVerIndex}" "." && semVerString="${RETURNED_VALUE%-*}"
string::cutField "${version}" "${semVerIndex}" "."
semVerString="${RETURNED_VALUE%-*}"
if [[ ! ${semVerString} =~ ^[0-9]+$ ]]; then
core::fail "Failed to bump the version ⌜${version}⌝ because it is not valid semantic version."
fi
semVerNumber="${semVerString%+}"
if [[ semVerIndex -eq level ]]; then semVerNumber=$((semVerNumber + 1)); fi
if [[ semVerIndex -gt level ]]; then semVerNumber=0; fi
Expand Down Expand Up @@ -452,4 +505,4 @@ function string::regexGetFirst() {
else
RETURNED_VALUE=""
fi
}
}

0 comments on commit a554c72

Please sign in to comment.