ShouldAutoUpdate function
Henry committed
Oct 20, 2014 at 17:10 UTC
8f1fd2fcd9983a5ecc79e6a1ed0e8b89737a903b
3 files changed
+99
-1
config/version.go
+1
@@ -44,6 +44,7 @@ const (
44
)
45
46
// supported Version.AutoUpdate values
47
+// BUG(cryptix): make this a custom type that implements json.Unmarshaller() to verify values
48
const (
49
UpdateNever = "never"
50
UpdatePatch = "patch"
updates/updates.go
+49
@@ -4,6 +4,7 @@ import (
4
"fmt"
5
"os"
6
7
+ "github.com/jbenet/go-ipfs/config"
8
u "github.com/jbenet/go-ipfs/util"
9
10
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
@@ -60,3 +61,51 @@ func CheckForUpdate() (*check.Result, error) {
61
func AbleToApply() error {
62
return update.New().CanUpdate()
63
}
64
+
65
+// ShouldAutoUpdate decides wether a new version should be applied
66
+// checks against config setting and new version string. returns false in case of error
67
+func ShouldAutoUpdate(setting, newVer string) bool {
68
+ if setting == config.UpdateNever {
69
+ return false
70
+ }
71
+
72
+ nv, err := semver.NewVersion(newVer)
73
+ if err != nil {
74
+ log.Error("could not parse version string: %s", err)
75
+ return false
76
+ }
77
+
78
+ n := nv.Slice()
79
+ c := currentVersion.Slice()
80
+
81
+ switch setting {
82
+
83
+ case config.UpdatePatch:
84
+ if n[0] < c[0] {
85
+ return false
86
+ }
87
+
88
+ if n[1] < c[1] {
89
+ return false
90
+ }
91
+
92
+ return n[2] > c[2]
93
+
94
+ case config.UpdateMinor:
95
+ if n[0] != c[0] {
96
+ return false
97
+ }
98
+
99
+ return n[1] > c[1] || (n[1] == c[1] && n[2] > c[2])
100
+
101
+ case config.UpdateMajor:
102
+ for i := 0; i < 3; i++ {
103
+ if n[i] < c[i] {
104
+ return false
105
+ }
106
+ }
107
+ return true
108
+ }
109
+
110
+ return false
111
+}
updates/updates_test.go
+49
-1
@@ -1,6 +1,11 @@
1
package updates
2
3
-import "testing"
3
+import (
4
+ "testing"
5
+
6
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
7
+ "github.com/jbenet/go-ipfs/config"
8
+)
9
10
// TestParseVersion just makes sure that we dont commit a bad version number
11
func TestParseVersion(t *testing.T) {
@@ -9,3 +14,46 @@ func TestParseVersion(t *testing.T) {
14
t.Fatal(err)
15
}
16
}
17
+
18
+func TestShouldAutoUpdate(t *testing.T) {
19
+ tests := []struct {
20
+ setting, currV, newV string
21
+ should bool
22
+ }{
23
+ {config.UpdateNever, "0.0.1", "1.0.0", false},
24
+ {config.UpdateNever, "0.0.1", "0.1.0", false},
25
+ {config.UpdateNever, "0.0.1", "0.0.1", false},
26
+ {config.UpdateNever, "0.0.1", "0.0.2", false},
27
+
28
+ {config.UpdatePatch, "0.0.1", "1.0.0", false},
29
+ {config.UpdatePatch, "0.0.1", "0.1.0", false},
30
+ {config.UpdatePatch, "0.0.1", "0.0.1", false},
31
+ {config.UpdatePatch, "0.0.2", "0.0.1", false},
32
+ {config.UpdatePatch, "0.0.1", "0.0.2", true},
33
+
34
+ {config.UpdateMinor, "0.1.1", "1.0.0", false},
35
+ {config.UpdateMinor, "0.1.1", "0.2.0", true},
36
+ {config.UpdateMinor, "0.1.1", "0.1.2", true},
37
+ {config.UpdateMinor, "0.2.1", "0.1.9", false},
38
+ {config.UpdateMinor, "0.1.2", "0.1.1", false},
39
+
40
+ {config.UpdateMajor, "1.0.0", "2.0.0", true},
41
+ {config.UpdateMajor, "1.0.0", "1.1.0", true},
42
+ {config.UpdateMajor, "1.0.0", "1.0.1", true},
43
+ {config.UpdateMajor, "2.0.0", "1.0.0", false}, // don't downgrade
44
+ {config.UpdateMajor, "2.5.0", "2.4.0", false},
45
+ {config.UpdateMajor, "2.0.2", "2.0.1", false},
46
+ }
47
+
48
+ for i, tc := range tests {
49
+ var err error
50
+ currentVersion, err = semver.NewVersion(tc.currV)
51
+ if err != nil {
52
+ t.Fatalf("Could not parse test version: %v", err)
53
+ }
54
+
55
+ if tc.should != ShouldAutoUpdate(tc.setting, tc.newV) {
56
+ t.Fatalf("#%d failed for %+v", i, tc)
57
+ }
58
+ }
59
+}