Move version comparison to separate package
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Arsen Musayelyan 2022-11-30 11:46:51 -08:00
parent 715fd6ccc9
commit 99b70859d1
2 changed files with 6 additions and 11 deletions

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
package vercmp
import (
_ "embed"
@ -26,16 +26,11 @@ import (
"golang.org/x/exp/slices"
)
//go:generate scripts/gen-version.sh
//go:embed version.txt
var version string
// vercmp compares two version strings.
// Compare compares two version strings.
// It returns 1 if v1 is greater,
// 0 if the versions are equal,
// and -1 if v2 is greater
func vercmp(v1, v2 string) int {
func Compare(v1, v2 string) int {
if v1 == v2 {
return 0
}

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
package vercmp
import (
"testing"
@ -67,13 +67,13 @@ func TestVerCmp(t *testing.T) {
for _, it := range table {
t.Run(it.v1+"/"+it.v2, func(t *testing.T) {
c := vercmp(it.v1, it.v2)
c := Compare(it.v1, it.v2)
if c != it.expected {
t.Errorf("Expected %d, got %d", it.expected, c)
}
// Ensure opposite comparison gives opposite value
c = -vercmp(it.v2, it.v1)
c = -Compare(it.v2, it.v1)
if c != it.expected {
t.Errorf("Expected %d, got %d (opposite)", it.expected, c)
}