Skip to content

Commit

Permalink
🚸 Fix output for CI, add simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiulpin authored and qodana-bot committed Sep 16, 2024
1 parent 7021e67 commit 4a97d9e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
6 changes: 3 additions & 3 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/json"
"fmt"
"github.com/JetBrains/qodana-cli/v2024/platform"
log "github.com/sirupsen/logrus"
"io"
"os"
"os/exec"
Expand All @@ -31,8 +32,6 @@ import (
"strings"
"testing"

log "github.com/sirupsen/logrus"

"github.com/JetBrains/qodana-cli/v2024/core"
)

Expand Down Expand Up @@ -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" {
Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions core/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
60 changes: 60 additions & 0 deletions core/container_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
13 changes: 11 additions & 2 deletions platform/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4a97d9e

Please sign in to comment.