Skip to content

Commit bb9ce03

Browse files
Implement MSBuildMaxVersion
1 parent 5b7caef commit bb9ce03

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* SonarScanner for .NET
3+
* Copyright (C) 2016-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package com.sonar.it.scanner.msbuild.utils;
21+
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
24+
import java.lang.annotation.ElementType;
25+
import java.lang.annotation.Retention;
26+
import java.lang.annotation.RetentionPolicy;
27+
import java.lang.annotation.Target;
28+
29+
@Retention(RetentionPolicy.RUNTIME)
30+
@Target({ElementType.METHOD, ElementType.TYPE})
31+
@ExtendWith(MSBuildVersionCondition.class)
32+
public @interface MSBuildMaxVersion {
33+
int value();
34+
}

its/src/test/java/com/sonar/it/scanner/msbuild/utils/MSBuildMinVersion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
@Retention(RetentionPolicy.RUNTIME)
2929
@Target({ElementType.METHOD, ElementType.TYPE})
30-
@ExtendWith(MSBuildMinVersionCondition.class)
30+
@ExtendWith(MSBuildVersionCondition.class)
3131
public @interface MSBuildMinVersion {
3232
int value();
3333
}

its/src/test/java/com/sonar/it/scanner/msbuild/utils/MSBuildMinVersionCondition.java renamed to its/src/test/java/com/sonar/it/scanner/msbuild/utils/MSBuildVersionCondition.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,42 @@
2020
package com.sonar.it.scanner.msbuild.utils;
2121

2222
import java.nio.file.Path;
23+
import java.util.Optional;
24+
2325
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
2426
import org.junit.jupiter.api.extension.ExecutionCondition;
2527
import org.junit.jupiter.api.extension.ExtensionContext;
28+
import org.junit.platform.commons.util.AnnotationUtils;
2629

27-
public class MSBuildMinVersionCondition implements ExecutionCondition {
30+
public class MSBuildVersionCondition implements ExecutionCondition {
2831
private static int msbuildMajorVersion = -1;
2932

3033
@Override
3134
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
3235
if (OSPlatform.isWindows()) {
3336
final var method = context.getRequiredTestMethod();
34-
final var annotation = method.getDeclaredAnnotation(MSBuildMinVersion.class);
35-
if (annotation != null) {
37+
Optional<MSBuildMinVersion> minAnnotation = AnnotationUtils.findAnnotation(method, MSBuildMinVersion.class);
38+
Optional<MSBuildMaxVersion> maxAnnotation = AnnotationUtils.findAnnotation(method, MSBuildMaxVersion.class);
39+
40+
if (minAnnotation.isPresent() || maxAnnotation.isPresent()) {
3641
var currentVersion = getMSBuildMajorVersion();
37-
var minVersion = annotation.value();
38-
return currentVersion >= minVersion
39-
? ConditionEvaluationResult.enabled("MSBuild version is " + currentVersion + ", which is greater than or equal to " + minVersion)
40-
: ConditionEvaluationResult.disabled("MSBuild version is " + currentVersion + ", which is less than " + minVersion);
42+
int minVersion = minAnnotation.map(MSBuildMinVersion::value).orElse(Integer.MIN_VALUE);
43+
int maxVersion = maxAnnotation.map(MSBuildMaxVersion::value).orElse(Integer.MAX_VALUE);
44+
45+
if (currentVersion >= minVersion && currentVersion <= maxVersion) {
46+
return ConditionEvaluationResult.enabled(
47+
String.format("MSBuild version %d satisfies constraints [min: %s, max: %s]",
48+
currentVersion,
49+
minAnnotation.isPresent() ? minVersion : "N/A",
50+
maxAnnotation.isPresent() ? maxVersion : "N/A"));
51+
}
52+
else {
53+
return ConditionEvaluationResult.disabled(
54+
String.format("MSBuild version check failed: MsBuild Version %d [min: %s, max: %s]",
55+
currentVersion,
56+
minAnnotation.isPresent() ? String.valueOf(minVersion) : "N/A",
57+
maxAnnotation.isPresent() ? String.valueOf(maxVersion) : "N/A"));
58+
}
4159
}
4260
}
4361
return ConditionEvaluationResult.enabled("Test enabled");

0 commit comments

Comments
 (0)