Skip to content

Commit

Permalink
Fix test that fails due to MavenResolutionResult validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Sep 12, 2024
1 parent d2d6cc5 commit 003325e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

import java.util.function.Consumer;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Assertions {
private Assertions() {
}
Expand Down Expand Up @@ -67,9 +65,11 @@ public static SourceSpecs pomXml(@Language("xml") @Nullable String before, @Lang
}

private static SourceFile pomResolvedSuccessfully(SourceFile sourceFile, TypeValidation typeValidation) {
sourceFile.getMarkers()
.findFirst(MavenResolutionResult.class)
.orElseThrow(() -> new IllegalStateException("No MavenResolutionResult found"));
if (typeValidation.dependencyModel()) {
sourceFile.getMarkers()
.findFirst(MavenResolutionResult.class)
.orElseThrow(() -> new IllegalStateException("No MavenResolutionResult found"));
}
return sourceFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;
import org.openrewrite.xml.tree.Xml;

import java.io.IOException;
Expand Down Expand Up @@ -222,7 +223,11 @@ void unresolvableTransitiveDependencyDueToInvalidPom(@TempDir Path localReposito
@Test
void unresolvableDependency() {
rewriteRun(
spec -> spec.executionContext(new InMemoryExecutionContext()),
spec -> spec.executionContext(new InMemoryExecutionContext())
.typeValidationOptions(TypeValidation.builder()
// Skip test framework validation that a MavenResolutionResult is present since this tests exercises error handling
.dependencyModel(false)
.build()),
pomXml(
"""
<project>
Expand All @@ -247,7 +252,8 @@ void unresolvableDependency() {
</dependencies>
</project>
""",
spec -> spec.afterRecipe(after -> {
spec -> spec
.afterRecipe(after -> {
Optional<ParseExceptionResult> maybeParseException = after.getMarkers().findFirst(ParseExceptionResult.class);
assertThat(maybeParseException).hasValueSatisfying(per -> assertThat(per.getMessage()).contains("Unable to download POM: com.google.guava:guava:doesnotexist. Tried repositories"));
})
Expand Down Expand Up @@ -326,17 +332,15 @@ void unresolvableTransitiveDependencyDueToInvalidPom() {
</project>
""",
spec -> spec.afterRecipe(pom ->
{
assertThat(pom.getMarkers().findFirst(MavenResolutionResult.class))
.isPresent()
.get()
.extracting(mrr -> mrr.getDependencies().get(Scope.Compile))
.matches(deps -> deps.size() == 1)
.extracting(deps -> deps.get(0))
.matches(dep -> dep.getGroupId().equals("org.jvnet.staxex") &&
dep.getArtifactId().equals("stax-ex") &&
dep.getVersion().equals("1.0"));
}))
assertThat(pom.getMarkers().findFirst(MavenResolutionResult.class))
.isPresent()
.get()
.extracting(mrr -> mrr.getDependencies().get(Scope.Compile))
.matches(deps -> deps.size() == 1)
.extracting(deps -> deps.get(0))
.matches(dep -> dep.getGroupId().equals("org.jvnet.staxex") &&
dep.getArtifactId().equals("stax-ex") &&
dep.getVersion().equals("1.0"))))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,76 @@
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
* Controls the test framework's validation of invariants which are expected to hold true in an LST both before and
* after the recipe run. Originally this applied only to validating the well-formedness of type metadata in Java LSTs
* (hence the name TypeValidation), but has since expanded to include concepts relevant to other types of sources.
* "InvariantValidation" would be a more accurate name, but "TypeValidation" is kept for backwards compatibility.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(fluent = true)
@Builder
public class TypeValidation {

/**
* Controls whether class declarations are validated to have valid type metadata.
*/
@Builder.Default
private boolean classDeclarations = true;

/**
* Controls whether identifiers declarations are validated to have valid type metadata.
* Within even a well-formed Java LST not all identifiers typically have type metadata, so even when enabled some
* identifiers are allowed to have null type.
*/
@Builder.Default
private boolean identifiers = true;

/**
* Controls whether method declarations are validated to have valid type metadata.
*/
@Builder.Default
private boolean methodDeclarations = true;

/**
* Controls whether field declarations are validated to have valid type metadata.
*/
@Builder.Default
private boolean variableDeclarations = true;

/**
* Controls whether method invocations are validated to have valid type metadata.
*/
@Builder.Default
private boolean methodInvocations = true;

/**
* Controls whether constructor invocations are validated to have valid type metadata.
*/
@Builder.Default
private boolean constructorInvocations = true;

/**
* Controls whether sources expected to have dependency resolution metadata in a model marker are validated to have
* that model attached. For example, a Maven pom is expected to have a MavenResolutionResult model attached.
*/
@Builder.Default
private boolean dependencyModel = true;

/**
* Enable all invariant validation checks.
*/
public static TypeValidation all() {
return new TypeValidation();
}

/**
* Skip all invariant validation checks.
*/
public static TypeValidation none() {
return new TypeValidation(false,false,false,false,false,false);
return new TypeValidation(false, false, false, false, false, false, false);
}

static TypeValidation before(RecipeSpec testMethodSpec, RecipeSpec testClassSpec) {
Expand Down

0 comments on commit 003325e

Please sign in to comment.