-
Notifications
You must be signed in to change notification settings - Fork 183
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 #2439 from actiontech/issue1567
Issue1567:新增版本比较工具函数
- Loading branch information
Showing
11 changed files
with
1,357 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
|
||
goVersion "github.com/hashicorp/go-version" | ||
) | ||
// Check if version is less than version to be compared, for example: input (3.1.1,3.1.2) returns true, input (3.1.1,3.1.1) returns false, input (3.1.1,3.1.0) returns false | ||
func IsVersionLessThan(version, versionToBeCompared string) (bool, error) { | ||
version1, err := goVersion.NewVersion(version) | ||
if err != nil { | ||
return false, fmt.Errorf("input version is invalid:%w", err) | ||
} | ||
version2, err := goVersion.NewVersion(versionToBeCompared) | ||
if err != nil { | ||
return false, fmt.Errorf("input version to be compared is invalid:%w", err) | ||
} | ||
return version1.LessThan(version2), nil | ||
} |
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,87 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestIsVersionLessThan(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
version string | ||
versionToBeCompared string | ||
expectedResult bool | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "valid versions, version is less than versionToBeCompared", | ||
version: "1.2.3.1", | ||
versionToBeCompared: "1.2.4", | ||
expectedResult: true, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "valid versions, version is greater than versionToBeCompared", | ||
version: "1.2.4", | ||
versionToBeCompared: "1.2.3.1", | ||
expectedResult: false, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "valid versions, version is equal to versionToBeCompared", | ||
version: "1.2.3.2", | ||
versionToBeCompared: "1.2.3.2", | ||
expectedResult: false, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "invalid version", | ||
version: "invalid", | ||
versionToBeCompared: "1.2.3", | ||
expectedResult: false, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "invalid versionToBeCompared", | ||
version: "1.2.3", | ||
versionToBeCompared: "invalid", | ||
expectedResult: false, | ||
expectedError: true, | ||
}, { | ||
name: "ob versionToBeCompared 1", | ||
version: "4.2.1.1-101010012023111012", | ||
versionToBeCompared: "4.2.0", | ||
expectedResult: false, | ||
expectedError: false, | ||
}, { | ||
name: "ob versionToBeCompared 2", | ||
version: "4.2.1.2-101010012023111012", | ||
versionToBeCompared: "4.2.0.0-100001282023042317", | ||
expectedResult: false, | ||
expectedError: false, | ||
}, { | ||
name: "ob versionToBeCompared 3", | ||
version: "3.1.3-101010012023111012", | ||
versionToBeCompared: "4.10.0.0-100001282023042317", | ||
expectedResult: true, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "ob versionToBeCompared 4", | ||
version: "3.1.2-101010012023111012", | ||
versionToBeCompared: "4.10.0.0-100001282023042317", | ||
expectedResult: true, | ||
expectedError: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result, err := IsVersionLessThan(tc.version, tc.versionToBeCompared) | ||
if (err != nil) != tc.expectedError { | ||
t.Errorf("expected error: %v, but got: %v", tc.expectedError, err != nil) | ||
} else if result != tc.expectedResult { | ||
t.Errorf("expected result %v, but got %v", tc.expectedResult, result) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.