|
| 1 | +package com.devoxx.genie.packaging; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.Comparator; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.regex.Pattern; |
| 14 | +import java.util.zip.ZipEntry; |
| 15 | +import java.util.zip.ZipFile; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.fail; |
| 18 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 19 | + |
| 20 | +/** |
| 21 | + * Regression test for issue #1054. |
| 22 | + * |
| 23 | + * <p>The IntelliJ Platform ships its own Kotlin coroutines runtime. When the plugin |
| 24 | + * distribution bundles its <em>own</em> copy of {@code kotlinx-coroutines-core(-jvm)} the |
| 25 | + * plugin's {@code PluginClassLoader} loads classes such as |
| 26 | + * {@code kotlinx.coroutines.flow.FlowCollector} while the platform's {@code PathClassLoader} |
| 27 | + * loads {@code kotlinx.coroutines.flow.internal.SafeCollector}. When the platform inline |
| 28 | + * completion API hands a {@code SafeCollector} to the plugin's compiled Kotlin |
| 29 | + * ({@code DevoxxGenieInlineCompletionProvider.getSuggestionDebounced}) the cross-classloader |
| 30 | + * cast fails with: |
| 31 | + * |
| 32 | + * <pre> |
| 33 | + * java.lang.ClassCastException: class kotlinx.coroutines.flow.internal.SafeCollector |
| 34 | + * cannot be cast to class kotlinx.coroutines.flow.FlowCollector |
| 35 | + * (SafeCollector is in unnamed module of loader PathClassLoader; |
| 36 | + * FlowCollector is in unnamed module of loader PluginClassLoader) |
| 37 | + * </pre> |
| 38 | + * |
| 39 | + * <p>{@code stripBinaryIncompatibleRuntimeJars} already removes these jars from the |
| 40 | + * {@code prepareSandbox}/{@code prepareTestSandbox} outputs (so {@code runIde} and the |
| 41 | + * test sandbox are clean and the bug is invisible during development), but it was NOT applied |
| 42 | + * to the {@code buildPlugin} distribution that is actually published. This test inspects the |
| 43 | + * built distribution ZIP and fails if any forbidden coroutines jar is present in the plugin |
| 44 | + * {@code lib/} directory. |
| 45 | + */ |
| 46 | +class PluginDistributionPackagingTest { |
| 47 | + |
| 48 | + /** |
| 49 | + * Jars that must never be bundled in the plugin distribution because the IntelliJ Platform |
| 50 | + * provides them at runtime. Mirrors {@code binaryIncompatibleRuntimeJarPatterns} in |
| 51 | + * {@code build.gradle.kts} (the coroutines entries). |
| 52 | + */ |
| 53 | + private static final List<Pattern> FORBIDDEN_LIB_JARS = Arrays.asList( |
| 54 | + Pattern.compile("kotlinx-coroutines-core-.*\\.jar"), |
| 55 | + Pattern.compile("kotlinx-coroutines-core-jvm-.*\\.jar") |
| 56 | + ); |
| 57 | + |
| 58 | + @Test |
| 59 | + void distributionMustNotBundlePlatformCoroutinesJars() throws Exception { |
| 60 | + Optional<File> distribution = findLatestDistributionZip(); |
| 61 | + |
| 62 | + // The distribution is only produced by `./gradlew buildPlugin`. When it is absent |
| 63 | + // (e.g. a plain `./gradlew test` on a clean checkout) there is nothing to verify, so |
| 64 | + // skip rather than fail spuriously. Run `./gradlew buildPlugin` to exercise this test. |
| 65 | + assumeTrue(distribution.isPresent(), |
| 66 | + "No plugin distribution ZIP found under build/distributions/ - run ./gradlew buildPlugin first"); |
| 67 | + |
| 68 | + File zip = distribution.get(); |
| 69 | + List<String> offending = new ArrayList<>(); |
| 70 | + |
| 71 | + try (ZipFile zipFile = new ZipFile(zip)) { |
| 72 | + var entries = zipFile.entries(); |
| 73 | + while (entries.hasMoreElements()) { |
| 74 | + ZipEntry entry = entries.nextElement(); |
| 75 | + String name = entry.getName(); |
| 76 | + if (entry.isDirectory()) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + // Only inspect jars that live in the packaged plugin's lib/ directory, |
| 80 | + // e.g. "DevoxxGenie/lib/kotlinx-coroutines-core-jvm-1.8.0.jar". |
| 81 | + int libIdx = name.indexOf("/lib/"); |
| 82 | + if (libIdx < 0) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + String fileName = name.substring(name.lastIndexOf('/') + 1); |
| 86 | + for (Pattern forbidden : FORBIDDEN_LIB_JARS) { |
| 87 | + if (forbidden.matcher(fileName).matches()) { |
| 88 | + offending.add(name); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (!offending.isEmpty()) { |
| 96 | + fail("Plugin distribution '" + zip.getName() + "' bundles coroutines jars that conflict " |
| 97 | + + "with the IntelliJ Platform classloader (issue #1054). " |
| 98 | + + "These must be excluded from the published plugin:\n " |
| 99 | + + String.join("\n ", offending)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Locates the most recently modified plugin distribution ZIP produced by buildPlugin. |
| 105 | + * Walks up from the working directory so the test works regardless of the module the |
| 106 | + * test runner is launched from. |
| 107 | + */ |
| 108 | + private static Optional<File> findLatestDistributionZip() { |
| 109 | + Path dir = Paths.get("").toAbsolutePath(); |
| 110 | + for (int depth = 0; depth < 4 && dir != null; depth++) { |
| 111 | + File distributions = dir.resolve("build").resolve("distributions").toFile(); |
| 112 | + if (distributions.isDirectory()) { |
| 113 | + File[] zips = distributions.listFiles( |
| 114 | + (d, fileName) -> fileName.endsWith(".zip") && fileName.startsWith("DevoxxGenie")); |
| 115 | + if (zips != null && zips.length > 0) { |
| 116 | + return Arrays.stream(zips).max(Comparator.comparingLong(File::lastModified)); |
| 117 | + } |
| 118 | + } |
| 119 | + dir = dir.getParent(); |
| 120 | + } |
| 121 | + return Optional.empty(); |
| 122 | + } |
| 123 | +} |
0 commit comments