mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 10:27:46 +08:00
ShouldAutoUpdate function
This commit is contained in:
parent
7ddf3836d0
commit
8f1fd2fcd9
@ -44,6 +44,7 @@ const (
|
||||
)
|
||||
|
||||
// supported Version.AutoUpdate values
|
||||
// BUG(cryptix): make this a custom type that implements json.Unmarshaller() to verify values
|
||||
const (
|
||||
UpdateNever = "never"
|
||||
UpdatePatch = "patch"
|
||||
|
||||
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/jbenet/go-ipfs/config"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
|
||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
|
||||
@ -60,3 +61,51 @@ func CheckForUpdate() (*check.Result, error) {
|
||||
func AbleToApply() error {
|
||||
return update.New().CanUpdate()
|
||||
}
|
||||
|
||||
// ShouldAutoUpdate decides wether a new version should be applied
|
||||
// checks against config setting and new version string. returns false in case of error
|
||||
func ShouldAutoUpdate(setting, newVer string) bool {
|
||||
if setting == config.UpdateNever {
|
||||
return false
|
||||
}
|
||||
|
||||
nv, err := semver.NewVersion(newVer)
|
||||
if err != nil {
|
||||
log.Error("could not parse version string: %s", err)
|
||||
return false
|
||||
}
|
||||
|
||||
n := nv.Slice()
|
||||
c := currentVersion.Slice()
|
||||
|
||||
switch setting {
|
||||
|
||||
case config.UpdatePatch:
|
||||
if n[0] < c[0] {
|
||||
return false
|
||||
}
|
||||
|
||||
if n[1] < c[1] {
|
||||
return false
|
||||
}
|
||||
|
||||
return n[2] > c[2]
|
||||
|
||||
case config.UpdateMinor:
|
||||
if n[0] != c[0] {
|
||||
return false
|
||||
}
|
||||
|
||||
return n[1] > c[1] || (n[1] == c[1] && n[2] > c[2])
|
||||
|
||||
case config.UpdateMajor:
|
||||
for i := 0; i < 3; i++ {
|
||||
if n[i] < c[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@ -1,6 +1,11 @@
|
||||
package updates
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
|
||||
"github.com/jbenet/go-ipfs/config"
|
||||
)
|
||||
|
||||
// TestParseVersion just makes sure that we dont commit a bad version number
|
||||
func TestParseVersion(t *testing.T) {
|
||||
@ -9,3 +14,46 @@ func TestParseVersion(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldAutoUpdate(t *testing.T) {
|
||||
tests := []struct {
|
||||
setting, currV, newV string
|
||||
should bool
|
||||
}{
|
||||
{config.UpdateNever, "0.0.1", "1.0.0", false},
|
||||
{config.UpdateNever, "0.0.1", "0.1.0", false},
|
||||
{config.UpdateNever, "0.0.1", "0.0.1", false},
|
||||
{config.UpdateNever, "0.0.1", "0.0.2", false},
|
||||
|
||||
{config.UpdatePatch, "0.0.1", "1.0.0", false},
|
||||
{config.UpdatePatch, "0.0.1", "0.1.0", false},
|
||||
{config.UpdatePatch, "0.0.1", "0.0.1", false},
|
||||
{config.UpdatePatch, "0.0.2", "0.0.1", false},
|
||||
{config.UpdatePatch, "0.0.1", "0.0.2", true},
|
||||
|
||||
{config.UpdateMinor, "0.1.1", "1.0.0", false},
|
||||
{config.UpdateMinor, "0.1.1", "0.2.0", true},
|
||||
{config.UpdateMinor, "0.1.1", "0.1.2", true},
|
||||
{config.UpdateMinor, "0.2.1", "0.1.9", false},
|
||||
{config.UpdateMinor, "0.1.2", "0.1.1", false},
|
||||
|
||||
{config.UpdateMajor, "1.0.0", "2.0.0", true},
|
||||
{config.UpdateMajor, "1.0.0", "1.1.0", true},
|
||||
{config.UpdateMajor, "1.0.0", "1.0.1", true},
|
||||
{config.UpdateMajor, "2.0.0", "1.0.0", false}, // don't downgrade
|
||||
{config.UpdateMajor, "2.5.0", "2.4.0", false},
|
||||
{config.UpdateMajor, "2.0.2", "2.0.1", false},
|
||||
}
|
||||
|
||||
for i, tc := range tests {
|
||||
var err error
|
||||
currentVersion, err = semver.NewVersion(tc.currV)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not parse test version: %v", err)
|
||||
}
|
||||
|
||||
if tc.should != ShouldAutoUpdate(tc.setting, tc.newV) {
|
||||
t.Fatalf("#%d failed for %+v", i, tc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user