-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from DateBro/master
fix: fix self-update bugs
- Loading branch information
Showing
7 changed files
with
201 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package repo | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type Version struct { | ||
Major int | ||
Minor int | ||
Patch int | ||
} | ||
|
||
func parseVersion(vStr string) (*Version, error) { | ||
vStr = strings.TrimPrefix(vStr, "v") | ||
|
||
// split by hyphen to remove pre-release info, then split by dot to get version info | ||
parts := strings.Split(strings.Split(vStr, "-")[0], ".") | ||
if len(parts) < 3 { | ||
return nil, fmt.Errorf("invalid version format: %s", vStr) | ||
} | ||
|
||
major, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
minor, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
patch, err := strconv.Atoi(parts[2]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Version{Major: major, Minor: minor, Patch: patch}, nil | ||
} | ||
|
||
func (v *Version) Equal(other *Version) bool { | ||
return v.compare(other) == 0 | ||
} | ||
|
||
func (v *Version) GreaterThan(other *Version) bool { | ||
return v.compare(other) > 0 | ||
} | ||
|
||
func (v *Version) LessThan(other *Version) bool { | ||
return v.compare(other) < 0 | ||
} | ||
|
||
func (v *Version) compare(other *Version) int { | ||
if v.Major != other.Major { | ||
return v.Major - other.Major | ||
} | ||
if v.Minor != other.Minor { | ||
return v.Minor - other.Minor | ||
} | ||
return v.Patch - other.Patch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package repo | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_parseVersion(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
vStr string | ||
want *Version | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "happy path for normal release version name", | ||
vStr: "v0.7.1", | ||
want: &Version{Major: 0, Minor: 7, Patch: 1}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "happy path for old aur-git release version name", | ||
vStr: "0.7.3.r15.g43a3080", | ||
want: &Version{Major: 0, Minor: 7, Patch: 3}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "happy path for new aur-git release version name", | ||
vStr: "0.7.3-r15-g43a3080", | ||
want: &Version{Major: 0, Minor: 7, Patch: 3}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "empty version name", | ||
vStr: "", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "only major and minor version", | ||
vStr: "0.7", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid format version name, major/minor/patch version is not a number", | ||
vStr: "xx.xx.xx", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "user customized version name", | ||
vStr: "test version", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "only dots, no version number", | ||
vStr: "...", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := parseVersion(tt.vStr) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("parseVersion() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("parseVersion() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestVersion_GreaterThan(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
v *Version | ||
other *Version | ||
want bool | ||
}{ | ||
{ | ||
name: "happy path", | ||
v: &Version{Major: 0, Minor: 7, Patch: 1}, | ||
other: &Version{Major: 0, Minor: 7, Patch: 0}, | ||
want: true, | ||
}, | ||
{ | ||
name: "same version number", | ||
v: &Version{Major: 0, Minor: 7, Patch: 1}, | ||
other: &Version{Major: 0, Minor: 7, Patch: 1}, | ||
want: false, | ||
}, | ||
{ | ||
name: "less than other version number", | ||
v: &Version{Major: 0, Minor: 7, Patch: 1}, | ||
other: &Version{Major: 0, Minor: 7, Patch: 3}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.v.GreaterThan(tt.other); got != tt.want { | ||
t.Errorf("GreaterThan() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |