Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (cli): Classpath Duplicate Detection Test #297

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ maven.install(
"org.snakeyaml:snakeyaml-engine:2.7",
"org.slf4j:slf4j-jdk14:2.0.9",
"org.slf4j:slf4j-simple:2.0.9",
"io.github.classgraph:classgraph:4.8.162"
],
fetch_sources = True,
lock_file = "//:maven_install.json",
Expand Down
1 change: 1 addition & 0 deletions cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ java_binary(
"@maven//:com_google_guava_guava",
"@maven//:com_google_truth_truth",
"@maven//:info_picocli_picocli",
"@maven//:io_github_classgraph_classgraph",
"@maven//:junit_junit",
],
) for name in glob([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2023 The Enola <https://enola.dev> Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.enola.cli;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ClasspathHellDuplicatesCheckerTest {

@Test
public void testIfThereAreAnyDuplicateJARsOnTheClasspath() throws Exception {
var problems = new HashMap<String, List<String>>();
try (ScanResult scanResult = new ClassGraph().scan()) {
var results = scanResult.getAllResources().classFilesOnly().findDuplicatePaths();
System.out.println("ClassGraph duplicate results: " + results);
for (var duplicate : results) {
String resourceName = duplicate.getKey();
if (!isHarmlessDuplicate(resourceName)) {
boolean addIt = true;
List<String> jars =
duplicate.getValue().stream()
.map(resource -> resource.getURL().toExternalForm())
.collect(Collectors.toList());
for (String jar : jars) {
if (skipJAR(jar)) {
addIt = false;
break;
}
}
if (addIt) {
problems.put(resourceName, jars);
}
}
}
}
if (!problems.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, List<String>> entry : problems.entrySet()) {
sb.append(entry.getKey());
sb.append('\n');
for (String location : entry.getValue()) {
sb.append(" ");
sb.append(location);
sb.append('\n');
}
}
Assert.fail(sb.toString());
}
}

private boolean skipJAR(String jarPath) {
// Bazel Rules etc. not runtime classpath
return jarPath.contains("java_tools/Runner_deploy.jar")

// TODO: Fix the sad mess :( of duplicate Protobuf & gRPC JARs!
|| jarPath.contains("protobuf")
|| jarPath.contains("grpc");
}

protected boolean isHarmlessDuplicate(String resourcePath) {
return resourcePath.equals("module-info.class");
}
}
28 changes: 26 additions & 2 deletions maven_install.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
"__INPUT_ARTIFACTS_HASH": 1511132585,
"__RESOLVED_ARTIFACTS_HASH": -1683949065,
"__INPUT_ARTIFACTS_HASH": 521153455,
"__RESOLVED_ARTIFACTS_HASH": -171608629,
"artifacts": {
"aopalliance:aopalliance": {
"shasums": {
Expand Down Expand Up @@ -241,6 +241,13 @@
},
"version": "4.7.5"
},
"io.github.classgraph:classgraph": {
"shasums": {
"jar": "ea30b2d5e29e89d52706bcecf7a6ae3b44682d4a1566a5f22b9453f9be2a970c",
"sources": "543151480066884e68090d7a8889f7a7b584b3760201af280cd577eaf1f9ecfd"
},
"version": "4.8.162"
},
"io.grpc:grpc-api": {
"shasums": {
"jar": "7fbc4beb922f40e6a84987c8dfbc833ee12e332849161f13f32928a728bb5597",
Expand Down Expand Up @@ -1198,6 +1205,21 @@
"info.picocli:picocli": [
"picocli"
],
"io.github.classgraph:classgraph": [
"io.github.classgraph",
"nonapi.io.github.classgraph.classloaderhandler",
"nonapi.io.github.classgraph.classpath",
"nonapi.io.github.classgraph.concurrency",
"nonapi.io.github.classgraph.fastzipfilereader",
"nonapi.io.github.classgraph.fileslice",
"nonapi.io.github.classgraph.fileslice.reader",
"nonapi.io.github.classgraph.json",
"nonapi.io.github.classgraph.recycler",
"nonapi.io.github.classgraph.reflection",
"nonapi.io.github.classgraph.scanspec",
"nonapi.io.github.classgraph.types",
"nonapi.io.github.classgraph.utils"
],
"io.grpc:grpc-api": [
"io.grpc"
],
Expand Down Expand Up @@ -2020,6 +2042,8 @@
"commons-io:commons-io:jar:sources",
"info.picocli:picocli",
"info.picocli:picocli:jar:sources",
"io.github.classgraph:classgraph",
"io.github.classgraph:classgraph:jar:sources",
"io.grpc:grpc-api",
"io.grpc:grpc-api:jar:sources",
"io.grpc:grpc-context",
Expand Down