Skip to content

Commit

Permalink
apitest: semver_compare, add docu and default to "v0.0.0"
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrode committed Jul 19, 2021
1 parent 23b6cee commit 32a84c2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
8 changes: 8 additions & 0 deletions pkg/lib/template/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 8 additions & 2 deletions test/semver/test_semver_compare.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit 32a84c2

Please sign in to comment.