Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-unixfs
Commits
8f1fd2fc
Commit
8f1fd2fc
authored
Oct 20, 2014
by
Henry
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ShouldAutoUpdate function
parent
7ddf3836
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
99 additions
and
1 deletion
+99
-1
config/version.go
config/version.go
+1
-0
updates/updates.go
updates/updates.go
+49
-0
updates/updates_test.go
updates/updates_test.go
+49
-1
No files found.
config/version.go
View file @
8f1fd2fc
...
...
@@ -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"
...
...
updates/updates.go
View file @
8f1fd2fc
...
...
@@ -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
}
updates/updates_test.go
View file @
8f1fd2fc
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
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment