diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go index 3fe58146..385f99d3 100644 --- a/cmd/cmd_test.go +++ b/cmd/cmd_test.go @@ -23,6 +23,7 @@ import ( "encoding/json" "fmt" "github.com/JetBrains/qodana-cli/v2024/platform" + log "github.com/sirupsen/logrus" "io" "os" "os/exec" @@ -31,8 +32,6 @@ import ( "strings" "testing" - log "github.com/sirupsen/logrus" - "github.com/JetBrains/qodana-cli/v2024/core" ) @@ -222,6 +221,7 @@ func TestPullInNative(t *testing.T) { } func TestAllCommandsWithContainer(t *testing.T) { + platform.Version = "0.1.0" linter := "registry.jetbrains.team/p/sa/containers/qodana-dotnet:latest" if os.Getenv("GITHUB_ACTIONS") == "true" { @@ -233,7 +233,7 @@ func TestAllCommandsWithContainer(t *testing.T) { //_ = os.Setenv(qodanaCliContainerKeep, "true") //_ = os.Setenv(qodanaCliContainerName, "qodana-cli-test-new1") platform.DisableColor() - core.CheckForUpdates("0.1.0") + core.CheckForUpdates(platform.Version) projectPath := createProject(t, "qodana_scan_python") // create temp directory for cache diff --git a/core/container.go b/core/container.go index 86d4525d..cf4a607a 100644 --- a/core/container.go +++ b/core/container.go @@ -73,14 +73,15 @@ func runQodanaContainer(ctx context.Context, options *QodanaOptions) int { platform.ErrorMessage("Container engine is not running a Linux platform, other platforms are not supported by Qodana") return 1 } - checkImage(options.Linter) fixDarwinCaches(options) for i, stage := range scanStages { scanStages[i] = platform.PrimaryBold("[%d/%d] ", i+1, len(scanStages)+1) + platform.Primary(stage) } - if !(options.SkipPull) { + if options.SkipPull { + checkImage(options.Linter) + } else { PullImage(docker, options.Linter) } progress, _ := platform.StartQodanaSpinner(scanStages[0]) @@ -126,17 +127,17 @@ func checkImage(linter string) { } if isUnofficialLinter(linter) { - platform.WarningMessage("You are using an unofficial Qodana linter: %s\n", linter) + platform.WarningMessageCI("You are using an unofficial Qodana linter: %s\n", linter) } if !hasExactVersionTag(linter) { - platform.WarningMessage( + platform.WarningMessageCI( "You are running a Qodana linter without an exact version tag: %s \n Consider pinning the version in your configuration to ensure version compatibility: %s\n", linter, - strings.Join([]string{linter, platform.ReleaseVersion}, ":"), + strings.Join([]string{strings.Split(linter, ":")[0], platform.ReleaseVersion}, ":"), ) } else if !isCompatibleLinter(linter) { - platform.WarningMessage( + platform.WarningMessageCI( "You are using a non-compatible Qodana linter %s with the current CLI (%s) \n Consider updating CLI or using a compatible linter %s \n", linter, platform.Version, @@ -229,6 +230,7 @@ func PrepareContainerEnvSettings() { // PullImage pulls docker image and prints the process. func PullImage(client *client.Client, image string) { + checkImage(image) platform.PrintProcess( func(_ *pterm.SpinnerPrinter) { pullImage(context.Background(), client, image) diff --git a/core/container_test.go b/core/container_test.go new file mode 100644 index 00000000..c7be594f --- /dev/null +++ b/core/container_test.go @@ -0,0 +1,60 @@ +package core + +import ( + "fmt" + "github.com/JetBrains/qodana-cli/v2024/platform" + "testing" +) + +func TestImageChecks(t *testing.T) { + testCases := []struct { + linter string + isUnofficial bool + hasExactVersion bool + isCompatible bool + }{ + { + "hadolint", + true, + false, + false, + }, + { + "jetbrains/qodana", + false, + false, + false, + }, + { + "jetbrains/qodana:latest", + false, + false, + false, + }, + { + "jetbrains/qodana:2022.1", + false, + true, + false, + }, + { + fmt.Sprintf("jetbrains/qodana:%s", platform.ReleaseVersion), + false, + true, + true, + }, + } + for _, tc := range testCases { + t.Run(tc.linter, func(t *testing.T) { + if isUnofficialLinter(tc.linter) != tc.isUnofficial { + t.Errorf("isUnofficial: got %v, want %v", isUnofficialLinter(tc.linter), tc.isUnofficial) + } + if hasExactVersionTag(tc.linter) != tc.hasExactVersion { + t.Errorf("hasExactVersion: got %v, want %v", hasExactVersionTag(tc.linter), tc.hasExactVersion) + } + if isCompatibleLinter(tc.linter) != tc.isCompatible { + t.Errorf("isCompatible: got %v, want %v", isCompatibleLinter(tc.linter), tc.isCompatible) + } + }) + } +} diff --git a/platform/output.go b/platform/output.go index e304b636..c52afabf 100644 --- a/platform/output.go +++ b/platform/output.go @@ -109,14 +109,20 @@ func SuccessMessage(message string, a ...interface{}) { func WarningMessage(message string, a ...interface{}) { message = fmt.Sprintf(message, a...) icon := warningStyle.Sprint("\n! ") - pterm.Println(formatMessageForCI(icon, Primary(message))) + pterm.Println(icon, Primary(message)) +} + +// WarningMessageCI prints a warning message to the CI environment (additional highlighting). +func WarningMessageCI(message string, a ...interface{}) { + message = fmt.Sprintf(message, a...) + pterm.Println(formatMessageForCI("warning", message)) } // ErrorMessage prints an error message with the icon. func ErrorMessage(message string, a ...interface{}) { message = fmt.Sprintf(message, a...) icon := errorStyle.Sprint("✗ ") - pterm.Println(formatMessageForCI(icon, errorStyle.Sprint(message))) + pterm.Println(icon, errorStyle.Sprint(message)) } // PrintLinterLog prints the linter logs with color, when needed. @@ -283,6 +289,9 @@ func getProblemsFoundMessage(newProblems int) string { func formatMessageForCI(level, format string, a ...interface{}) string { message := fmt.Sprintf(format, a...) ci := cienvironment.DetectCIEnvironment() + if ci == nil { + return message + } name := getCIName(ci) if name == "github-actions" { return fmt.Sprintf("::%s::%s", level, message)