Skip to content

Commit

Permalink
Merge pull request #5 from Ariel-Rodriguez/workflow
Browse files Browse the repository at this point in the history
Add CI workflow
  • Loading branch information
Ariel-Rodriguez authored Feb 7, 2023
2 parents 3ccef60 + c064697 commit b80370c
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 16 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI
run-name: Checks
on: [push]
jobs:
Linux-sh:
runs-on: ubuntu-latest
steps:
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 Testing branch ${{ github.ref }}"
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- name: Give executable permissions to semver2.sh
run: |
chmod +x ${{ github.workspace }}/semver2.sh
- name: Give executable permissions to test.sh
run: |
chmod +x ${{ github.workspace }}/test.sh
- run: ls -lha ${{ github.workspace }}
- name: test
run: ${{ github.workspace }}/test.sh
shell: sh
Windows-cmd:
runs-on: windows-latest
steps:
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 Testing branch ${{ github.ref }}"
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- name: Give executable permissions to semver2.sh
run: |
chmod +x ${{ github.workspace }}/semver2.sh
- name: Give executable permissions to test.sh
run: |
chmod +x ${{ github.workspace }}/test.sh
- name: test
run: sh ${{ github.workspace }}/test.sh
shell: cmd
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# sh-semversion-2

[![checks](https://github.com/Ariel-Rodriguez/sh-semversion-2/actions/workflows/github-ci.yml/badge.svg?branch=main)](https://github.com/Ariel-Rodriguez/sh-semversion-2/actions/workflows/github-ci.yml)

SH script to compare versions using semversion 2.0 spec.

- highly-cross-compatible shell compliant with the POSIX standard.
Expand Down Expand Up @@ -61,7 +63,9 @@ npx sh-semversion-2 1.0.0-rc.0.a+metadata v1.0.0-rc.0+metadata
## Tests

```sh
./test.sh
./test.sh print-only-failures
# abort on first failure match
./test.sh print-only-failures early-exit=true
```

```sh
Expand Down
34 changes: 23 additions & 11 deletions semver2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,15 @@ semver_compare() {
fi

# Spec 11.3 a pre-release version has lower precedence than a normal version:
# Example: 1.0.0-alpha < 1.0.0.

# 1.0.0 < 1.0.0-alpha
if [ -z "$a_pre" ]; then
debug "Because a pre-release version has lower precedence than a normal version"
debug "Because A is the stable release. Pre-release version has lower precedence than a released version"
outcome "1"
return
fi

# 1.0.0-alpha < 1.0.0
if [ -z "$b_pre" ]; then
debug "Because a pre-release version has lower precedence than a normal version"
debug "Because B is the stable release. Pre-release version has lower precedence than a released version"
outcome "-1"
return
fi
Expand All @@ -238,7 +237,7 @@ semver_compare() {
}

cursor=1
while [ $cursor -lt 4 ]
while [ $cursor -lt 5 ]
do
a=$(printf %s "$a_pre" | cut -d'.' -f $cursor)
b=$(printf %s "$b_pre" | cut -d'.' -f $cursor)
Expand All @@ -259,16 +258,21 @@ semver_compare() {
# MUST be determined by comparing each dot separated identifier from left to right until a difference is found

# Spec 11.4.4: A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.

if [ -n "$a" ] && [ -z "$b" ]; then
# When A is larger than B
debug "Because A has more pre-identifiers"
# When A is larger than B and preidentifiers are 1+n
# 1.0.0-alpha.beta.1 1.0.0-alpha.beta
# 1.0.0-alpha.beta.1.2 1.0.0-alpha.beta.1
debug "Because A has larger set of pre-identifiers"
outcome "1"
return
fi

# When A is shorter than B
# When A is shorter than B and preidentifiers are 1+n
# 1.0.0-alpha.beta 1.0.0-alpha.beta.d
# 1.0.0-alpha.beta 1.0.0-alpha.beta.1.2
if [ -z "$a" ] && [ -n "$b" ]; then
debug "Because B has more pre-identifiers"
debug "Because B has larger set of pre-identifiers"
outcome "-1"
return
fi
Expand All @@ -278,6 +282,7 @@ semver_compare() {
if [ "$(isNumber "$a")" = "true" ] || [ "$(isNumber "$b")" = "true" ]; then

# if both identifiers are numbers, then compare and proceed
# 1.0.0-beta.3 1.0.0-beta.2
if [ "$(isNumber "$a")" = "true" ] && [ "$(isNumber "$b")" = "true" ]; then
if [ "$(compareNumber "$a" "$b")" != "0" ]; then
debug "Number is not equal $(compareNumber "$a" "$b")"
Expand All @@ -287,12 +292,15 @@ semver_compare() {
fi

# Spec 11.4.3
# 1.0.0-alpha.1 1.0.0-alpha.beta.d
# 1.0.0-beta.3 1.0.0-1.2
if [ "$(isNumber "$a")" = "false" ]; then
debug "Because Numeric identifiers always have lower precedence than non-numeric identifiers."
outcome "1"
return
fi

# 1.0.0-alpha.d 1.0.0-alpha.beta.1
# 1.0.0-1.1 1.0.0-beta.1.2
if [ "$(isNumber "$b")" = "false" ]; then
debug "Because Numeric identifiers always have lower precedence than non-numeric identifiers."
outcome "-1"
Expand All @@ -301,6 +309,7 @@ semver_compare() {
else
# Spec 11.4.2
# Identifiers with letters or hyphens are compared lexically in ASCII sort order.
# 1.0.0-alpha 1.0.0-beta.alpha
if [ "$(compareString "$a" "$b")" != "0" ]; then
debug "cardinal is not equal $(compareString a b)"
outcome "$(compareString "$a" "$b")"
Expand All @@ -312,6 +321,7 @@ semver_compare() {
if [ "$cursor" = 1 ]; then

# When both versions are single return equals
# 1.0.0-alpha 1.0.0-alpha
if [ -n "$(isSingleIdentifier "$b_pre" "$b")" ] && [ -n "$(isSingleIdentifier "$a_pre" "$a")" ]; then
debug "Because both have single identifier"
outcome "0"
Expand All @@ -322,13 +332,15 @@ semver_compare() {
# Spec 11.4.4: A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.

# When A is larger than B
# 1.0.0-alpha.beta 1.0.0-alpha
if [ -n "$(isSingleIdentifier "$b_pre" "$b")" ] && [ -z "$(isSingleIdentifier "$a_pre" "$a")" ]; then
debug "Because of single identifier, A has more pre-identifiers"
outcome "1"
return
fi

# When A is shorter than B
# 1.0.0-alpha 1.0.0-alpha.beta
if [ -z "$(isSingleIdentifier "$b_pre" "$b")" ] && [ -n "$(isSingleIdentifier "$a_pre" "$a")" ]; then
debug "Because of single identifier, B has more pre-identifiers"
outcome "-1"
Expand Down
96 changes: 92 additions & 4 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
#!/bin/bash

set -eu
set -u

# Include semver_compare tool
source ./semver2.sh 1.0.0 1.0.0
source ./semver2.sh

# specific target tests

isLowerThanSpec=(
1.0.0-alpha 1.0.0-alpha.1
# Spec 11.4.4: A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.
1.0.0-alpha 1.0.0-alpha.beta
# Spec 11.4.2 Identifiers with letters or hyphens are compared lexically in ASCII sort order.
1.0.0-alpha 1.0.0-beta
1.0.0-alpha 1.0.0-beta.2
1.0.0-alpha 1.0.0-beta.11
1.0.0-alpha 1.0.0-rc.1
# Spec 11.3 a pre-release version has lower precedence than a normal version
1.0.0-alpha 1.0.0
# Spec 11.4.3 Because Numeric identifiers always have lower precedence than non-numeric identifiers
1.0.0-alpha.1 1.0.0-alpha.beta
1.0.0-alpha.1 1.0.0-beta
1.0.0-alpha.1 1.0.0-beta.2
1.0.0-alpha.1 1.0.0-beta.11
1.0.0-alpha.1 1.0.0-rc.1
1.0.0-alpha.1 1.0.0
1.0.0-alpha.beta 1.0.0-beta
1.0.0-alpha.beta 1.0.0-beta.2
1.0.0-alpha.beta 1.0.0-beta.11
1.0.0-alpha.beta 1.0.0-rc.1
1.0.0-alpha.beta 1.0.0
1.0.0-beta 1.0.0-beta.2
1.0.0-beta 1.0.0-beta.11
1.0.0-beta 1.0.0-rc.1
1.0.0-beta 1.0.0
1.0.0-beta.2 1.0.0-beta.11
1.0.0-beta.2 1.0.0-rc.1
1.0.0-beta.2 1.0.0
1.0.0-beta.11 1.0.0-rc.1
)

# -----
# brute force tests
#
# semver2.0 happy path tests ordered by precedence
# spec 11.4
# 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.
Expand All @@ -16,6 +54,7 @@ tests=(
1.0.0-alpha
1.0.0-alpha.1
1.0.0-alpha.beta
1.0.0-alpha.beta.1
1.0.0-beta
1.0.0-beta.2
1.0.0-beta.11
Expand Down Expand Up @@ -72,20 +111,69 @@ _assert_is() {
esac

printf "%s $reason\n"
if [ -n "$reason" ]; then exit 1; fi
if [ -n "$reason" ]; then printf "1"; fi
}

exitcode=0
failcount=0
testscount=0

printf "\nSpecific target tests...\n\n"
for (( i=0; i<=$(( ${#isLowerThanSpec[@]} -1 )); i+=2 ))
do
outcome=$(_assert_is "${isLowerThanSpec[i]}" "${isLowerThanSpec[i+1]}" "$(semver_compare "${isLowerThanSpec[i]}" "${isLowerThanSpec[i+1]}")" "-1")
echo "$outcome"
# increase failcount by 1
testscount=$((testscount + 1))

if [[ "$outcome" == "FAILED "* ]]; then
failcount=$((failcount + 1))
exitcode=1
if [ -n "$*" ] && [ "$1" == "early-exit=true" ]; then
break
fi
fi
done

printf "\n\nBrute force tests...\n\n"
for (( i=0; i<=$(( ${#tests[@]} -1 )); i+=1 ))
do
for (( j=0; j<$(( ${#tests[@]} )); j+=1 ))
do
expected="0"
if [ "$j" -lt "$i" ]; then expected="1"; fi
if [ "$j" -gt "$i" ]; then expected="-1"; fi
printf "\n%s $(_assert_is "${tests[i]}" "${tests[j]}" "$(semver_compare "${tests[i]}" "${tests[j]}")" "$expected")"
outcome=$(_assert_is "${tests[i]}" "${tests[j]}" "$(semver_compare "${tests[i]}" "${tests[j]}")" "$expected")

# print outcome only if optional program parameter contains "print-only-failures"
if [ -n "$*" ] && [[ "$*" == *"print-only-failures"* ]]; then
if [[ "$outcome" == "FAILED "* ]]; then
echo "$outcome"
fi
# print a progress bar
printf "\r"
printf "Progress: %s%%" "$((100 * i / ${#tests[@]}))"
else
printf "%s\n$outcome"
fi

# increase failcount by 1
testscount=$((testscount + 1))

if [[ "$outcome" == "FAILED "* ]]; then
failcount=$((failcount + 1))
exitcode=1
if [ -n "$*" ] && [ "$1" == "early-exit=true" ]; then
printf "\n%sTotal failures $failcount out of $testscount tests OK\n"
exit $exitcode
fi
fi
done
done

printf "\n%sTotal failures $failcount of $testscount tests OK\n"
exit $exitcode

# OK 1.0.0-alpha < 1.0.0-alpha.1
# OK 1.0.0-alpha < 1.0.0-alpha.beta
# OK 1.0.0-alpha < 1.0.0-beta
Expand Down

0 comments on commit b80370c

Please sign in to comment.