Skip to content

Commit

Permalink
new template function semver_compare (#55)
Browse files Browse the repository at this point in the history
Co-authored-by: Philipp Hempel <philipp.hempel@programmfabrik.de>
  • Loading branch information
phempel and Philipp Hempel authored Jul 19, 2021
1 parent d6e54dc commit 23b6cee
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/lib/template/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/pkg/errors"
"github.com/programmfabrik/apitest/pkg/lib/datastore"
"golang.org/x/mod/semver"

"github.com/programmfabrik/apitest/pkg/lib/cjson"
"github.com/programmfabrik/apitest/pkg/lib/csv"
Expand Down Expand Up @@ -430,6 +431,15 @@ func (loader *Loader) Render(
}
return string(b)
},
"semver_compare": func(v, w string) (int, error) {
if !semver.IsValid(v) {
return 0, errors.Errorf("version string %s is invalid", v)
}
if !semver.IsValid(w) {
return 0, errors.Errorf("version string %s is invalid", w)
}
return semver.Compare(v, w), nil
},
}
tmpl, err := template.New("tmpl").Funcs(funcMap).Parse(string(tmplBytes))
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions test/semver/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"http_server": {
"addr": ":9999",
"dir": "../_res",
"testmode": false
},
"name": "tests for semantic version comparisons (semver)",
"tests": [
"@test_semver_compare.json"
]
}
46 changes: 46 additions & 0 deletions test/semver/test_semver_compare.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "semantic version comparisons (semver)",
"request": {
"server_url": "http://localhost:9999",
"endpoint": "bounce-json",
"method": "POST",
"body": {
"semver_easydb_release": {{ semver_compare "v5.86.0" "v6.0.0" }},
"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" }}
}
},
"response": {
"statuscode": 200,
"body": {
"body": {
"semver_easydb_release:control": {
"is_integer": true
},
"semver_easydb_release": -1,

"semver_easydb_patch:control": {
"is_integer": true
},
"semver_easydb_patch": -1,

"semver_lt:control": {
"is_integer": true
},
"semver_lt": -1,

"semver_eq:control": {
"is_integer": true
},
"semver_eq": 0,

"semver_gt:control": {
"is_integer": true
},
"semver_gt": 1
}
}
}
}

0 comments on commit 23b6cee

Please sign in to comment.