Skip to content

Commit

Permalink
Merge 80825db into 9d6c38a
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav committed Nov 12, 2016
2 parents 9d6c38a + 80825db commit 6544355
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 68 deletions.
41 changes: 41 additions & 0 deletions internal/semver/compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package semver

// CompatibleRange generates the compatibility range for generated code and
// plugins.
//
// Assuming current Version is 1.2.3-pre we get:
//
// begin >= 1.0.0
// end < 1.3.0-pre
func CompatibleRange(v Version) (r Range) {
r.Begin = v
r.End = v

r.Begin.Pre = nil
r.Begin.Patch = 0
r.Begin.Minor = 0

r.End.Patch = 0
r.End.Minor++
return r
}
49 changes: 26 additions & 23 deletions version/version_test.go → internal/semver/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,52 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package version
package semver

import (
"testing"

"go.uber.org/thriftrw/internal/semver"

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

func TestCodeCompatRange(t *testing.T) {
func TestCompatibleRange(t *testing.T) {
test := []struct {
libVer string
compatVer []string
invalidVer []string
library string
compatible []string
incompatible []string
}{
{
"1.2.3",
[]string{"1.2.3", "1.2.4", "1.0.0"},
[]string{"1.3.0", "0.9.9"},
library: "1.2.3",
compatible: []string{"1.2.3", "1.2.4", "1.0.0"},
incompatible: []string{"1.3.0", "0.9.9"},
},
{
"2.0.0",
[]string{"2.0.0", "2.0.9", "2.1.0-pre"},
[]string{"2.1.0", "1.9.9", "2.0.0-pre"},
library: "2.0.0",
compatible: []string{"2.0.0", "2.0.9", "2.1.0-pre"},
incompatible: []string{"2.1.0", "1.9.9", "2.0.0-pre"},
},
}
for _, tt := range test {
t.Run(tt.libVer, func(t *testing.T) {
compatRange := computeGenCodeCompabilityRange(tt.libVer)
for _, compatVer := range tt.compatVer {
t.Run(compatVer, func(t *testing.T) {
genv, err := semver.Parse(compatVer)
t.Run(tt.library, func(t *testing.T) {
libVersion, err := Parse(tt.library)
require.NoError(t, err)

compatRange := CompatibleRange(libVersion)

for _, v := range tt.compatible {
t.Run(v, func(t *testing.T) {
version, err := Parse(v)
require.NoError(t, err)
assert.True(t, compatRange.IsCompatibleWith(genv))
assert.True(t, compatRange.Contains(version))
})
}
for _, invalidVer := range tt.invalidVer {
t.Run(invalidVer, func(t *testing.T) {
genv, err := semver.Parse(invalidVer)

for _, v := range tt.incompatible {
t.Run(v, func(t *testing.T) {
version, err := Parse(v)
require.NoError(t, err)
assert.False(t, compatRange.IsCompatibleWith(genv))
assert.False(t, compatRange.Contains(version))
})
}
})
Expand Down
32 changes: 32 additions & 0 deletions internal/semver/range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package semver

// Range is the range of version numbers that lie in [Begin, End).
type Range struct {
Begin Version
End Version
}

// Contains returns true if the given semver version number is in this range.
func (r *Range) Contains(other Version) bool {
return other.Compare(&r.Begin) >= 0 && other.Compare(&r.End) < 0
}
28 changes: 23 additions & 5 deletions version/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@

package version

import "log"
import (
"log"

"go.uber.org/thriftrw/internal/semver"
)

var compatRange = computeCompatibleRange()

func computeCompatibleRange() semver.Range {
v, err := semver.Parse(Version)
if err != nil {
panic(err)
}

return semver.CompatibleRange(v)
}

// CheckCompatWithGeneratedCodeAt will panic if the ThriftRW version used to
// generated code (given by `genCodeVersion`) is not compatible with the
Expand All @@ -35,10 +50,13 @@ import "log"
// This function will ensure that the version mismatch is detected and help
// avoid bugs that could be caused by this discrepancy.
func CheckCompatWithGeneratedCodeAt(genCodeVersion string, fromPkg string) {
genv := parseSemVerOrPanic(genCodeVersion)
if !genCodeCompatbilityRange.IsCompatibleWith(genv) {
v, err := semver.Parse(genCodeVersion)
if err != nil {
panic(err)
}

if !compatRange.Contains(v) {
log.Panicf(`incompatible version from generated package %q, expected >=%s and <%s, got %s`,
fromPkg, &genCodeCompatbilityRange.begin,
&genCodeCompatbilityRange.end, &genv)
fromPkg, &compatRange.Begin, &compatRange.End, &v)
}
}
40 changes: 0 additions & 40 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,5 @@

package version

import (
"go.uber.org/thriftrw/internal/semver"
)

// Version is the current ThriftRW version.
const Version = "0.6.0"

var genCodeCompatbilityRange = computeGenCodeCompabilityRange(Version)

type genCodeCompatbilityRangeHolder struct {
begin semver.Version
end semver.Version
}

// computeGenCodeCompabilityRange generates the compatibility range for generated code.
// Assuming current Version is 1.2.3-pre we get:
// begin >= 1.0.0
// end < 1.3.0-pre
func computeGenCodeCompabilityRange(version string) (r genCodeCompatbilityRangeHolder) {
r.begin = parseSemVerOrPanic(version)
r.end = r.begin

r.begin.Pre = nil
r.begin.Patch = 0
r.begin.Minor = 0

r.end.Patch = 0
r.end.Minor++
return r
}

func (r *genCodeCompatbilityRangeHolder) IsCompatibleWith(other semver.Version) bool {
return (other.Compare(&r.begin) >= 0 && other.Compare(&r.end) < 0)
}

func parseSemVerOrPanic(v string) semver.Version {
semVer, err := semver.Parse(v)
if err != nil {
panic(err)
}
return semVer
}

0 comments on commit 6544355

Please sign in to comment.