Skip to content

Commit

Permalink
Add semver and semverCompare.
Browse files Browse the repository at this point in the history
  • Loading branch information
technosophos committed Mar 15, 2017
1 parent 1f3c301 commit 23597e5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ string is passed in, functions will attempt to conver with
{{ derivePassword 1 "long" "password" "user" "example.com" }}
```

## SemVer Functions:

These functions provide version parsing and comparisons for SemVer 2 version
strings.

- semver: Parse a semantic version and return a Version object.
- semverCompare: Compare a SemVer range to a particular version.

## Principles:

Expand Down
8 changes: 8 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,13 @@ Crypto Functions:
Given parameters (in order) are:
`counter` (starting with 1), `password_type` (maximum, long, medium, short, basic, or pin), `password`,
`user`, and `site`
SemVer Functions:
These functions provide version parsing and comparisons for SemVer 2 version
strings.
- semver: Parse a semantic version and return a Version object.
- semverCompare: Compare a SemVer range to a particular version.
*/
package sprig
4 changes: 4 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,8 @@ var genericMap = map[string]interface{}{

// UUIDs:
"uuidv4": uuidv4,

// SemVer:
"semver": semver,
"semverCompare": semverCompare,
}
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ import:
- package: golang.org/x/crypto
subpackages:
- scrypt
- package: github.com/Masterminds/semver
version: v1.2.2
23 changes: 23 additions & 0 deletions semver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sprig

import (
sv2 "github.com/Masterminds/semver"
)

func semverCompare(constraint, version string) (bool, error) {
c, err := sv2.NewConstraint(constraint)
if err != nil {
return false, err
}

v, err := sv2.NewVersion(version)
if err != nil {
return false, err
}

return c.Check(v), nil
}

func semver(version string) (*sv2.Version, error) {
return sv2.NewVersion(version)
}
31 changes: 31 additions & 0 deletions semver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sprig

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSemverCompare(t *testing.T) {
tests := map[string]string{
`{{ semverCompare "1.2.3" "1.2.3" }}`: `true`,
`{{ semverCompare "^1.2.0" "1.2.3" }}`: `true`,
`{{ semverCompare "^1.2.0" "2.2.3" }}`: `false`,
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}

func TestSemver(t *testing.T) {
tests := map[string]string{
`{{ $s := semver "1.2.3-beta.1+c0ff33" }}{{ $s.Prerelease }}`: "beta.1",
`{{ $s := semver "1.2.3-beta.1+c0ff33" }}{{ $s.Major}}`: "1",
`{{ semver "1.2.3" | (semver "1.2.3").Compare }}`: `0`,
`{{ semver "1.2.3" | (semver "1.3.3").Compare }}`: `1`,
`{{ semver "1.4.3" | (semver "1.2.3").Compare }}`: `-1`,
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}

0 comments on commit 23597e5

Please sign in to comment.