From 32a84c2a796698db3401f5fc276202126250bcfa Mon Sep 17 00:00:00 2001 From: Martin Rode Date: Mon, 19 Jul 2021 12:34:07 +0200 Subject: [PATCH] apitest: semver_compare, add docu and default to "v0.0.0" --- README.md | 4 ++++ go.mod | 1 + pkg/lib/template/template_loader.go | 8 ++++++++ test/semver/test_semver_compare.json | 10 ++++++++-- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a4f718f..45d354d 100644 --- a/README.md +++ b/README.md @@ -1850,6 +1850,10 @@ Example: ** oauth2_basic_auth** returns the authentication header for basic authentication for the given oauth client. +## `semver_compare [version 1] [version 2]` + +**semver_compare** compares to semantic version strings. This calls https://pkg.go.dev/golang.org/x/mod/semver#Compare, so check there for additional documentation. If the version is `""` the version `v0.0.0` is assumed. Before comparing, the function checks if the strings are valid. In case they are not, an error is returned. + # HTTP Server The apitest tool includes an HTTP Server. It can be used to serve files from the local disk temporarily. The HTTP Server can run in test mode. In this mode, the apitest tool does not run any tests, but starts the HTTP Server in the foreground, until CTRL-C in pressed. diff --git a/go.mod b/go.mod index 87e3393..c11a894 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/spf13/cobra v0.0.5 github.com/spf13/viper v1.5.0 github.com/tidwall/gjson v1.3.4 + golang.org/x/mod v0.4.2 golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect ) diff --git a/pkg/lib/template/template_loader.go b/pkg/lib/template/template_loader.go index 667c3c1..c25d9ec 100644 --- a/pkg/lib/template/template_loader.go +++ b/pkg/lib/template/template_loader.go @@ -432,6 +432,14 @@ func (loader *Loader) Render( return string(b) }, "semver_compare": func(v, w string) (int, error) { + if v == "" { + // empty version + v = "v0.0.0" + } + if w == "" { + // empty version + w = "v0.0.0" + } if !semver.IsValid(v) { return 0, errors.Errorf("version string %s is invalid", v) } diff --git a/test/semver/test_semver_compare.json b/test/semver/test_semver_compare.json index 85da676..b2eb619 100644 --- a/test/semver/test_semver_compare.json +++ b/test/semver/test_semver_compare.json @@ -9,7 +9,8 @@ "semver_easydb_patch": {{ semver_compare "v5.86.2" "v6.0.0" }}, "semver_lt": {{ semver_compare "v5.1.2" "v6.0.0" }}, "semver_eq": {{ semver_compare "v6.0.0" "v6.0.0" }}, - "semver_gt": {{ semver_compare "v7.1.2" "v6.0.0" }} + "semver_gt": {{ semver_compare "v7.1.2" "v6.0.0" }}, + "semver_eq2": {{ semver_compare "" "v0.0.0" }} } }, "response": { @@ -39,7 +40,12 @@ "semver_gt:control": { "is_integer": true }, - "semver_gt": 1 + "semver_gt": 1, + + "semver_eq2:control": { + "is_integer": true + }, + "semver_eq2": 0 } } }