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

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

View File

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