Skip to content

Commit

Permalink
feat(test): Allow filtering test variants based on active runtime ver…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
zml2008 committed Aug 24, 2024
1 parent 7768ac7 commit 18c554b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of mammoth, licensed under the MIT License.
*
* Copyright (c) 2021-2022 KyoriPowered
* Copyright (c) 2021-2024 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -48,6 +48,30 @@
* An extension that can be applied to test methods to provide test template invocation context.
*/
class GradleFunctionalTestExtension implements TestTemplateInvocationContextProvider {
private static final int CURRENT_JVM;

static {
final String versionProp = System.getProperty("java.version");
if (versionProp == null) {
throw new IllegalStateException("System property 'java.version' has not been set???");
}
final String[] versionComps = versionProp.split("\\.", -1);
if (versionComps.length < 1) {
throw new IllegalStateException("Empty version components in property value '" + versionProp + "'");
}

String toParse = versionComps[0];
if ("1".equals(toParse)) {
toParse = versionComps[1];
}

try {
CURRENT_JVM = Integer.parseInt(toParse);
} catch (final NumberFormatException ex) {
throw new IllegalArgumentException("Invalid JVM version component '" + toParse + "' in version property '" + versionProp + "'", ex);
}
}

@Override
public boolean supportsTestTemplate(final ExtensionContext context) {
final Optional<Method> method = context.getTestMethod();
Expand All @@ -58,6 +82,15 @@ public boolean supportsTestTemplate(final ExtensionContext context) {
return AnnotationSupport.isAnnotated(method, GradleFunctionalTest.class);
}

private static boolean permitsJavaVersion(final int minVersion, final int maxVersion) {
if (maxVersion < minVersion) {
throw new IllegalArgumentException("Maximum runtime version provided (" + maxVersion + ") is less than minimum version (" + minVersion + ")");
}

return CURRENT_JVM >= minVersion &&
CURRENT_JVM <= maxVersion;
}

@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(final ExtensionContext context) {
final Optional<GradleParameters> parameters = AnnotationSupport.findAnnotation(context.getElement(), GradleParameters.class);
Expand All @@ -70,9 +103,11 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
return Stream.of(this.produce(context, commonArgs, ""));
} else {
final Stream<TestTemplateInvocationContext> directVariants = variants.stream()
.filter(variant -> permitsJavaVersion(variant.minimumRuntimeVersion(), variant.maximumRuntimeVersion()))
.map(variant -> this.produce(context, commonArgs, variant.gradleVersion(), variant.extraArguments()));

final Stream<TestTemplateInvocationContext> resourceVariants = variantSources.stream()
.filter(variantRes -> permitsJavaVersion(variantRes.minimumRuntimeVersion(), variantRes.maximumRuntimeVersion()))
.flatMap(source -> this.readLines(source.value(), context.getRequiredTestClass().getResource(source.value()), source.optional()))
.filter(arr -> arr.length > 0)
.map(line -> this.produce(context, commonArgs, line[0], line.length > 1 ? line[1].split(" ", -1) : new String[0]));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of mammoth, licensed under the MIT License.
*
* Copyright (c) 2021-2022 KyoriPowered
* Copyright (c) 2021-2024 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -60,4 +60,24 @@
* @since 1.1.0
*/
String[] extraArguments() default {};

/**
* Minimum runtime version to execute this variant on.
*
* <p>By default, permits any version.</p>
*
* @return the minimum runtime version
* @since 1.4.0
*/
int minimumRuntimeVersion() default -1;

/**
* Maximum runtime version to execute this variant on.
*
* <p>By default, permits any version. This must be greater than or equal to {@link #minimumRuntimeVersion()}</p>
*
* @return the maximum runtime version
* @since 1.4.0
*/
int maximumRuntimeVersion() default Integer.MAX_VALUE;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of mammoth, licensed under the MIT License.
*
* Copyright (c) 2021-2022 KyoriPowered
* Copyright (c) 2021-2024 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -62,4 +62,24 @@
* @since 1.1.0
*/
boolean optional() default false;

/**
* Minimum runtime version to execute the resource's variants on.
*
* <p>By default, permits any version.</p>
*
* @return the minimum runtime version
* @since 1.4.0
*/
int minimumRuntimeVersion() default -1;

/**
* Maximum runtime version to execute the resource's variants on.
*
* <p>By default, permits any version. This must be greater than or equal to {@link #minimumRuntimeVersion()}</p>
*
* @return the maximum runtime version
* @since 1.4.0
*/
int maximumRuntimeVersion() default Integer.MAX_VALUE;
}

0 comments on commit 18c554b

Please sign in to comment.