This commit is contained in:
Phillip Webb 2023-05-02 15:41:55 -07:00
parent 6b646f6a8e
commit b3226c55d2
4 changed files with 36 additions and 22 deletions

View File

@ -36,7 +36,6 @@ import org.apache.commons.compress.utils.IOUtils;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.springframework.boot.buildpack.platform.docker.DockerApi;
@ -47,6 +46,7 @@ import org.springframework.boot.buildpack.platform.docker.type.VolumeName;
import org.springframework.boot.buildpack.platform.io.FilePermissions;
import org.springframework.boot.gradle.junit.GradleCompatibility;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuild;
import org.springframework.boot.testsupport.junit.DisabledOnOs;
import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable;
import static org.assertj.core.api.Assertions.assertThat;
@ -60,7 +60,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@GradleCompatibility(configurationCache = true)
@DisabledIfDockerUnavailable
@org.springframework.boot.testsupport.junit.DisabledOnOs(os = { OS.LINUX, OS.MAC }, architecture = "aarch64",
@DisabledOnOs(os = { OS.LINUX, OS.MAC }, architecture = "aarch64",
disabledReason = "The builder image has no ARM support")
class BootBuildImageIntegrationTests {

View File

@ -33,6 +33,7 @@ import org.springframework.boot.buildpack.platform.docker.DockerApi.VolumeApi;
import org.springframework.boot.buildpack.platform.docker.type.ImageName;
import org.springframework.boot.buildpack.platform.docker.type.ImageReference;
import org.springframework.boot.buildpack.platform.docker.type.VolumeName;
import org.springframework.boot.testsupport.junit.DisabledOnOs;
import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable;
import static org.assertj.core.api.Assertions.assertThat;
@ -46,7 +47,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@ExtendWith(MavenBuildExtension.class)
@DisabledIfDockerUnavailable
@org.springframework.boot.testsupport.junit.DisabledOnOs(os = { OS.LINUX, OS.MAC }, architecture = "aarch64",
@DisabledOnOs(os = { OS.LINUX, OS.MAC }, architecture = "aarch64",
disabledReason = "The builder image has no ARM support")
class BuildImageTests extends AbstractArchiveIntegrationTests {

View File

@ -37,16 +37,22 @@ import org.junit.jupiter.api.extension.ExtendWith;
public @interface DisabledOnOs {
/**
* See {@link org.junit.jupiter.api.condition.DisabledOnOs#value()}.
* @return os
* The operating systems on which the annotated class or method should be disabled.
* @return the operating systems where the test is disabled
*/
OS[] os();
OS[] value() default {};
/**
* Architecture of the operating system.
* @return architecture
* The operating systems on which the annotated class or method should be disabled.
* @return the operating systems where the test is disabled
*/
String architecture();
OS[] os() default {};
/**
* The architectures on which the annotated class or method should be disabled.
* @return the architectures where the test is disabled
*/
String[] architecture() default {};
/**
* See {@link org.junit.jupiter.api.condition.DisabledOnOs#disabledReason()}.

View File

@ -16,42 +16,49 @@
package org.springframework.boot.testsupport.junit;
import java.util.Optional;
import java.util.Arrays;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.util.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
/**
* Evaluates {@link DisabledOnOs}.
*
* @author Moritz Halbritter
* @author Phillip Webb
*/
class DisabledOnOsCondition implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Optional<DisabledOnOs> annotation = AnnotationUtils.findAnnotation(context.getElement(), DisabledOnOs.class);
if (!context.getElement().isPresent()) {
return ConditionEvaluationResult.enabled("No element for @DisabledOnOs found");
}
MergedAnnotation<DisabledOnOs> annotation = MergedAnnotations
.from(context.getElement().get(), SearchStrategy.TYPE_HIERARCHY)
.get(DisabledOnOs.class);
if (!annotation.isPresent()) {
return ConditionEvaluationResult.enabled("No @DisabledOnOs found");
}
return evaluate(annotation.get());
return evaluate(annotation.synthesize());
}
private ConditionEvaluationResult evaluate(DisabledOnOs annotation) {
String architecture = System.getProperty("os.arch");
String os = System.getProperty("os.name");
if (annotation.architecture().equals(architecture)) {
for (OS targetOs : annotation.os()) {
if (targetOs.isCurrentOs()) {
String reason = annotation.disabledReason().isEmpty()
? String.format("Disabled on OS = %s, architecture = %s", os, architecture)
: annotation.disabledReason();
return ConditionEvaluationResult.disabled(reason);
}
}
boolean onDisabledOs = Arrays.stream(annotation.os()).anyMatch(OS::isCurrentOs);
boolean onDisabledArchitecture = Arrays.stream(annotation.architecture()).anyMatch(architecture::equals);
if (onDisabledOs && onDisabledArchitecture) {
String reason = annotation.disabledReason().isEmpty()
? String.format("Disabled on OS = %s, architecture = %s", os, architecture)
: annotation.disabledReason();
return ConditionEvaluationResult.disabled(reason);
}
return ConditionEvaluationResult
.enabled(String.format("Enabled on OS = %s, architecture = %s", os, architecture));