Add file version regex and regex tests
This commit is contained in:
@@ -6,12 +6,15 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var versionRegex = regexp.MustCompile("_v[0-9]+-[0-9]+-[0-9]+b?")
|
var (
|
||||||
|
fileVersionRegex = regexp.MustCompile(`_v[0-9]+-[0-9]+-[0-9]+b?`)
|
||||||
|
rawVersionRegex = regexp.MustCompile(`^[0-9]+\.[0-9]+\.[0-9]+b?\*?$`)
|
||||||
|
)
|
||||||
|
|
||||||
// GetIdentifierAndVersion splits the given file path into its identifier and version.
|
// GetIdentifierAndVersion splits the given file path into its identifier and version.
|
||||||
func GetIdentifierAndVersion(versionedPath string) (identifier, version string, ok bool) {
|
func GetIdentifierAndVersion(versionedPath string) (identifier, version string, ok bool) {
|
||||||
// extract version
|
// extract version
|
||||||
rawVersion := versionRegex.FindString(versionedPath)
|
rawVersion := fileVersionRegex.FindString(versionedPath)
|
||||||
if rawVersion == "" {
|
if rawVersion == "" {
|
||||||
return "", "", false
|
return "", "", false
|
||||||
}
|
}
|
||||||
|
|||||||
51
updates/filename_test.go
Normal file
51
updates/filename_test.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package updates
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testRegexMatch(t *testing.T, testRegex *regexp.Regexp, testString string, shouldMatch bool) {
|
||||||
|
if testRegex.MatchString(testString) != shouldMatch {
|
||||||
|
if shouldMatch {
|
||||||
|
t.Errorf("regex %s should match %s", testRegex, testString)
|
||||||
|
} else {
|
||||||
|
t.Errorf("regex %s should not match %s", testRegex, testString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testRegexFind(t *testing.T, testRegex *regexp.Regexp, testString string, shouldMatch bool) {
|
||||||
|
if (testRegex.FindString(testString) != "") != shouldMatch {
|
||||||
|
if shouldMatch {
|
||||||
|
t.Errorf("regex %s should find %s", testRegex, testString)
|
||||||
|
} else {
|
||||||
|
t.Errorf("regex %s should not find %s", testRegex, testString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegexes(t *testing.T) {
|
||||||
|
testRegexMatch(t, rawVersionRegex, "0.1.2", true)
|
||||||
|
testRegexMatch(t, rawVersionRegex, "0.1.2*", true)
|
||||||
|
testRegexMatch(t, rawVersionRegex, "0.1.2b", true)
|
||||||
|
testRegexMatch(t, rawVersionRegex, "0.1.2b*", true)
|
||||||
|
testRegexMatch(t, rawVersionRegex, "12.13.14", true)
|
||||||
|
|
||||||
|
testRegexMatch(t, rawVersionRegex, "v0.1.2", false)
|
||||||
|
testRegexMatch(t, rawVersionRegex, "0.", false)
|
||||||
|
testRegexMatch(t, rawVersionRegex, "0.1", false)
|
||||||
|
testRegexMatch(t, rawVersionRegex, "0.1.", false)
|
||||||
|
testRegexMatch(t, rawVersionRegex, ".1.2", false)
|
||||||
|
testRegexMatch(t, rawVersionRegex, ".1.", false)
|
||||||
|
testRegexMatch(t, rawVersionRegex, "012345", false)
|
||||||
|
|
||||||
|
testRegexFind(t, fileVersionRegex, "/path/to/file_v1-2-3", true)
|
||||||
|
testRegexFind(t, fileVersionRegex, "/path/to/file_v1-2-3.exe", true)
|
||||||
|
|
||||||
|
testRegexFind(t, fileVersionRegex, "/path/to/file-v1-2-3", false)
|
||||||
|
testRegexFind(t, fileVersionRegex, "/path/to/file_v1.2.3", false)
|
||||||
|
testRegexFind(t, fileVersionRegex, "/path/to/file_1-2-3", false)
|
||||||
|
testRegexFind(t, fileVersionRegex, "/path/to/file_v1-2", false)
|
||||||
|
testRegexFind(t, fileVersionRegex, "/path/to/file-v1-2-3", false)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user