Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(version-checker): allow override version arg and regex #1182

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 28 additions & 6 deletions modules/version-checker/version_checker.go
Expand Up @@ -23,8 +23,8 @@ const (
)

const (
// versionRegexMatcher is a regex used to extract version string from shell command output.
versionRegexMatcher = `\d+(\.\d+)+`
// defaultVersionRegexMatcher is a regex used to extract version string from shell command output.
defaultVersionRegexMatcher = `\d+(\.\d+)+`
// defaultVersionArg is a default arg to pass in to get version output from shell command.
defaultVersionArg = "--version"
)
Expand All @@ -34,9 +34,13 @@ type CheckVersionParams struct {
BinaryPath string
// Binary is the name of the binary you want to check the version for.
Binary VersionCheckerBinary
// VersionArg is a string literal to pass in to get version output from shell command.
VersionArg string
// VersionConstraint is a string literal containing one or more conditions, which are separated by commas.
// More information here:https://www.terraform.io/language/expressions/version-constraints
VersionConstraint string
// VersionRegexMatcher is a regex used to extract version string from shell command output.
VersionRegexMatcher string
// WorkingDir is a directory you want to run the shell command.
WorkingDir string
}
Expand Down Expand Up @@ -82,12 +86,24 @@ func validateParams(params CheckVersionParams) error {
"invalid version constraint format found {%s}", params.VersionConstraint)
}

// Check the format of the version regex matcher if present.
if _, err := regexp.Compile(params.VersionRegexMatcher); params.VersionRegexMatcher != "" && err != nil {
return fmt.Errorf(
"invalid version regex matcher format found {%s}", params.VersionRegexMatcher)
}

return nil
}

// getVersionWithShellCommand get version by running a shell command.
func getVersionWithShellCommand(t testing.TestingT, params CheckVersionParams) (string, error) {
var versionArg = defaultVersionArg
var versionArg string
if params.VersionArg != "" {
versionArg = params.VersionArg
} else {
versionArg = defaultVersionArg
}

binary, err := getBinary(params)
if err != nil {
return "", err
Expand All @@ -105,7 +121,7 @@ func getVersionWithShellCommand(t testing.TestingT, params CheckVersionParams) (
"w/ version args {%s}: %w", binary, versionArg, err)
}

versionStr, err := extractVersionFromShellCommandOutput(output)
versionStr, err := extractVersionFromShellCommandOutput(params, output)
if err != nil {
return "", fmt.Errorf("failed to extract version from shell "+
"command output {%s}: %w", output, err)
Expand Down Expand Up @@ -135,8 +151,14 @@ func getBinary(params CheckVersionParams) (string, error) {

// extractVersionFromShellCommandOutput extracts version with regex string matching
// from the given shell command output string.
func extractVersionFromShellCommandOutput(output string) (string, error) {
regexMatcher := regexp.MustCompile(versionRegexMatcher)
func extractVersionFromShellCommandOutput(params CheckVersionParams, output string) (string, error) {
var regexMatcher *regexp.Regexp
if params.VersionRegexMatcher != "" {
regexMatcher = regexp.MustCompile(params.VersionRegexMatcher)
} else {
regexMatcher = regexp.MustCompile(defaultVersionRegexMatcher)
}

versionStr := regexMatcher.FindString(output)
if versionStr == "" {
return "", fmt.Errorf("failed to find version using regex matcher")
Expand Down
25 changes: 24 additions & 1 deletion modules/version-checker/version_checker_test.go
Expand Up @@ -41,6 +41,17 @@ func TestParamValidation(t *testing.T) {
containError: true,
expectedErrorMessage: "invalid version constraint format found {abc}",
},
{
name: "Invalid Version Regex Matcher Format",
param: CheckVersionParams{
Binary: Docker,
VersionConstraint: "1.2.3",
VersionRegexMatcher: "[notregex",
WorkingDir: ".",
},
containError: true,
expectedErrorMessage: "invalid version regex matcher format found {[notregex}",
},
{
name: "Success",
param: CheckVersionParams{
Expand Down Expand Up @@ -72,46 +83,52 @@ func TestExtractVersionFromShellCommandOutput(t *testing.T) {
expectedVersionStr string
containError bool
expectedErrorMessage string
param CheckVersionParams
}{
{
name: "Stand-alone version string",
outputStr: "version is 1.2.3",
expectedVersionStr: "1.2.3",
containError: false,
expectedErrorMessage: "",
param: CheckVersionParams{},
},
{
name: "version string with v prefix",
outputStr: "version is v1.0.0",
expectedVersionStr: "1.0.0",
containError: false,
expectedErrorMessage: "",
param: CheckVersionParams{},
},
{
name: "2 digit version string",
outputStr: "version is v1.0",
expectedVersionStr: "1.0",
containError: false,
expectedErrorMessage: "",
param: CheckVersionParams{},
},
{
name: "invalid output string",
outputStr: "version is vabc",
expectedVersionStr: "",
containError: true,
expectedErrorMessage: "failed to find version using regex matcher",
param: CheckVersionParams{},
},
{
name: "empty output string",
outputStr: "",
expectedVersionStr: "",
containError: true,
expectedErrorMessage: "failed to find version using regex matcher",
param: CheckVersionParams{},
},
}

for _, tc := range tests {
versionStr, err := extractVersionFromShellCommandOutput(tc.outputStr)
versionStr, err := extractVersionFromShellCommandOutput(tc.param, tc.outputStr)
if tc.containError {
require.EqualError(t, err, tc.expectedErrorMessage, tc.name)
} else {
Expand Down Expand Up @@ -210,6 +227,12 @@ func TestCheckVersionEndToEnd(t *testing.T) {
VersionConstraint: ">= 0.0.1",
WorkingDir: ".",
}},
{name: "Go", param: CheckVersionParams{
BinaryPath: "go",
VersionArg: "version",
VersionConstraint: ">= 1.17.0",
WorkingDir: ".",
}},
}

for _, tc := range tests {
Expand Down