Skip to content

Commit d7bb64a

Browse files
committed
🚸 Fix output for CI, add simple tests
1 parent fa4449c commit d7bb64a

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

cmd/cmd_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"encoding/json"
2424
"fmt"
2525
"github.com/JetBrains/qodana-cli/v2024/platform"
26+
log "github.com/sirupsen/logrus"
2627
"io"
2728
"os"
2829
"os/exec"
@@ -31,8 +32,6 @@ import (
3132
"strings"
3233
"testing"
3334

34-
log "github.com/sirupsen/logrus"
35-
3635
"github.com/JetBrains/qodana-cli/v2024/core"
3736
)
3837

@@ -250,6 +249,7 @@ func TestPullInNative(t *testing.T) {
250249
}
251250

252251
func TestAllCommandsWithContainer(t *testing.T) {
252+
platform.Version = "0.1.0"
253253
linter := "registry.jetbrains.team/p/sa/containers/qodana-dotnet:latest"
254254

255255
if os.Getenv("GITHUB_ACTIONS") == "true" {
@@ -261,7 +261,7 @@ func TestAllCommandsWithContainer(t *testing.T) {
261261
//_ = os.Setenv(qodanaCliContainerKeep, "true")
262262
//_ = os.Setenv(qodanaCliContainerName, "qodana-cli-test-new1")
263263
platform.DisableColor()
264-
core.CheckForUpdates("0.1.0")
264+
core.CheckForUpdates(platform.Version)
265265
projectPath := createProject(t, "qodana_scan_python")
266266

267267
// create temp directory for cache

core/container.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@ func runQodanaContainer(ctx context.Context, options *QodanaOptions) int {
7171
platform.ErrorMessage("Container engine is not running a Linux platform, other platforms are not supported by Qodana")
7272
return 1
7373
}
74-
checkImage(options.Linter)
7574
fixDarwinCaches(options)
7675

7776
for i, stage := range scanStages {
7877
scanStages[i] = platform.PrimaryBold("[%d/%d] ", i+1, len(scanStages)+1) + platform.Primary(stage)
7978
}
8079

81-
if !(options.SkipPull) {
80+
if options.SkipPull {
81+
checkImage(options.Linter)
82+
} else {
8283
PullImage(docker, options.Linter)
8384
}
8485
progress, _ := platform.StartQodanaSpinner(scanStages[0])
@@ -124,17 +125,17 @@ func checkImage(linter string) {
124125
}
125126

126127
if isUnofficialLinter(linter) {
127-
platform.WarningMessage("You are using an unofficial Qodana linter: %s\n", linter)
128+
platform.WarningMessageCI("You are using an unofficial Qodana linter: %s\n", linter)
128129
}
129130

130131
if !hasExactVersionTag(linter) {
131-
platform.WarningMessage(
132+
platform.WarningMessageCI(
132133
"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",
133134
linter,
134-
strings.Join([]string{linter, platform.ReleaseVersion}, ":"),
135+
strings.Join([]string{strings.Split(linter, ":")[0], platform.ReleaseVersion}, ":"),
135136
)
136137
} else if !isCompatibleLinter(linter) {
137-
platform.WarningMessage(
138+
platform.WarningMessageCI(
138139
"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",
139140
linter,
140141
platform.Version,
@@ -227,6 +228,7 @@ func PrepareContainerEnvSettings() {
227228

228229
// PullImage pulls docker image and prints the process.
229230
func PullImage(client *client.Client, image string) {
231+
checkImage(image)
230232
platform.PrintProcess(
231233
func(_ *pterm.SpinnerPrinter) {
232234
pullImage(context.Background(), client, image)

core/container_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package core
2+
3+
import (
4+
"fmt"
5+
"github.com/JetBrains/qodana-cli/v2024/platform"
6+
"testing"
7+
)
8+
9+
func TestImageChecks(t *testing.T) {
10+
testCases := []struct {
11+
linter string
12+
isUnofficial bool
13+
hasExactVersion bool
14+
isCompatible bool
15+
}{
16+
{
17+
"hadolint",
18+
true,
19+
false,
20+
false,
21+
},
22+
{
23+
"jetbrains/qodana",
24+
false,
25+
false,
26+
false,
27+
},
28+
{
29+
"jetbrains/qodana:latest",
30+
false,
31+
false,
32+
false,
33+
},
34+
{
35+
"jetbrains/qodana:2022.1",
36+
false,
37+
true,
38+
false,
39+
},
40+
{
41+
fmt.Sprintf("jetbrains/qodana:%s", platform.ReleaseVersion),
42+
false,
43+
true,
44+
true,
45+
},
46+
}
47+
for _, tc := range testCases {
48+
t.Run(tc.linter, func(t *testing.T) {
49+
if isUnofficialLinter(tc.linter) != tc.isUnofficial {
50+
t.Errorf("isUnofficial: got %v, want %v", isUnofficialLinter(tc.linter), tc.isUnofficial)
51+
}
52+
if hasExactVersionTag(tc.linter) != tc.hasExactVersion {
53+
t.Errorf("hasExactVersion: got %v, want %v", hasExactVersionTag(tc.linter), tc.hasExactVersion)
54+
}
55+
if isCompatibleLinter(tc.linter) != tc.isCompatible {
56+
t.Errorf("isCompatible: got %v, want %v", isCompatibleLinter(tc.linter), tc.isCompatible)
57+
}
58+
})
59+
}
60+
}

platform/output.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,20 @@ func SuccessMessage(message string, a ...interface{}) {
109109
func WarningMessage(message string, a ...interface{}) {
110110
message = fmt.Sprintf(message, a...)
111111
icon := warningStyle.Sprint("\n! ")
112-
pterm.Println(formatMessageForCI(icon, Primary(message)))
112+
pterm.Println(icon, Primary(message))
113+
}
114+
115+
// WarningMessageCI prints a warning message to the CI environment (additional highlighting).
116+
func WarningMessageCI(message string, a ...interface{}) {
117+
message = fmt.Sprintf(message, a...)
118+
pterm.Println(formatMessageForCI("warning", message))
113119
}
114120

115121
// ErrorMessage prints an error message with the icon.
116122
func ErrorMessage(message string, a ...interface{}) {
117123
message = fmt.Sprintf(message, a...)
118124
icon := errorStyle.Sprint("✗ ")
119-
pterm.Println(formatMessageForCI(icon, errorStyle.Sprint(message)))
125+
pterm.Println(icon, errorStyle.Sprint(message))
120126
}
121127

122128
// PrintLinterLog prints the linter logs with color, when needed.
@@ -283,6 +289,9 @@ func getProblemsFoundMessage(newProblems int) string {
283289
func formatMessageForCI(level, format string, a ...interface{}) string {
284290
message := fmt.Sprintf(format, a...)
285291
ci := cienvironment.DetectCIEnvironment()
292+
if ci == nil {
293+
return message
294+
}
286295
name := getCIName(ci)
287296
if name == "github-actions" {
288297
return fmt.Sprintf("::%s::%s", level, message)

0 commit comments

Comments
 (0)