Skip to content

Commit

Permalink
Linkage Checker to throw exception upon missing artifact (#2105)
Browse files Browse the repository at this point in the history
  • Loading branch information
suztomo authored Jun 18, 2021
1 parent 739be77 commit 4e8dc71
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.cloud.tools.opensource.dependencies.Bom;
import com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -110,6 +111,20 @@ public static LinkageChecker create(Bom bom, Path exclusionFile)
classPathBuilder.resolve(managedDependencies, true, DependencyMediation.MAVEN);
ImmutableList<ClassPathEntry> classpath = classPathResult.getClassPath();

ImmutableList<UnresolvableArtifactProblem> artifactProblems =
classPathResult.getArtifactProblems();
if (!artifactProblems.isEmpty()) {
for (UnresolvableArtifactProblem artifactProblem : artifactProblems) {
logger.severe(artifactProblem.toString());
}
throw new IOException(
"Could not resolve "
+ (artifactProblems.size() == 1
? "1 dependency"
: (artifactProblems.size() + " dependencies"))
+ ". See the message above for details.");
}

// When checking a BOM, entry point classes are the ones in the artifacts listed in the BOM
List<ClassPathEntry> artifactsInBom = classpath.subList(0, managedDependencies.size());
ImmutableSet<ClassPathEntry> entryPoints = ImmutableSet.copyOf(artifactsInBom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.Assert.fail;

import com.google.cloud.tools.opensource.dependencies.Artifacts;
import com.google.cloud.tools.opensource.dependencies.Bom;
import com.google.cloud.tools.opensource.dependencies.DependencyGraph;
import com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder;
import com.google.cloud.tools.opensource.dependencies.DependencyPath;
Expand Down Expand Up @@ -1247,4 +1248,27 @@ public void testFindLinkageProblems_shouldNotReportMethodHandleInvoke()

Truth.assertThat(problemsOnMethodHandle).isEmpty();
}

@Test
public void testBomResolutionWithMissingTransitiveDependency() throws Exception {
DefaultArtifact cglib = new DefaultArtifact("cglib:cglib-nodep:jar:2.2");
// cglib-nodep has 2 transitive dependencies, which do not exist in Maven Central
// cglib:cglib-nodep:jar:2.2
// - ant:ant:jar:1.6.2 (compile?)
// - xml-apis:xml-apis:jar:2.6.2 (compile?)
// - xerces:xerces-impl:jar:2.6.2 (compile?)
// https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/2097
Bom bomWithMissingTransitiveDependency =
new Bom(
"com.google.cloud:bom-with-missing-transitive-dependency:0.1",
ImmutableList.of(cglib));
try {
LinkageChecker.create(bomWithMissingTransitiveDependency);
fail("Linkage Checker should throw IOException");
} catch (IOException expected) {
assertEquals(
"Could not resolve 2 dependencies. See the message above for details.",
expected.getMessage());
}
}
}

0 comments on commit 4e8dc71

Please sign in to comment.