christophd commented on code in PR #29663:
URL: https://github.com/apache/camel/pull/29663#discussion_r4546983488
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginType.java:
##########
@@ -77,4 +80,8 @@ public String getRepos() {
public String getVendor() {
return vendor;
}
+
+ public String[] getAppliesTo() {
+ return appliesTo;
Review Comment:
Done in the second commit — `getAppliesTo()` returns `appliesTo.clone()`.
_Claude Code on behalf of Christoph Deppisch_
##########
dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/PluginFilteringTest.java:
##########
@@ -0,0 +1,435 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://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 org.apache.camel.dsl.jbang.core.common;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.commands.CliExportContext;
+import org.apache.camel.dsl.jbang.run.common.StringPrinter;
+import org.apache.camel.dsl.jbang.run.export.ExportContext;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import picocli.CommandLine;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * Tests that plugins not relevant to the current command are skipped entirely
— not downloaded, not customized.
+ * <p>
+ * Each test configures cached fake plugins (so no Maven download is needed).
To detect whether a plugin was loaded or
+ * skipped, some tests use a "broken" plugin entry whose cache points to a
non-existent jar: if the resolution code
+ * attempts to load the broken plugin, it will fail with a {@link
CliException}. If the plugin is properly filtered out,
+ * the resolution succeeds.
+ */
+class PluginFilteringTest {
+
+ @TempDir
+ Path tempDir;
+
+ private Path previousHome;
+ private String camelVersion;
+
+ @BeforeEach
+ void setup() {
+ previousHome = CommandLineHelper.getHomeDir();
+ CommandLineHelper.useHomeDir(tempDir.toString());
+ camelVersion = new
org.apache.camel.catalog.DefaultCamelCatalog().getCatalogVersion();
+ }
+
+ @AfterEach
+ void restoreHome() {
+ CommandLineHelper.useHomeDir(previousHome.toString());
+ }
+
+ // ---- Target-based filtering in getActivePlugins ----
+
+ @Test
+ void pluginNotMatchingTargetIsNotLoaded() throws Exception {
+ writeTwoPluginConfig("alpha", "beta");
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "alpha");
+
+ assertThat(plugins).containsKey("alpha")
+ .doesNotContainKey("beta");
+ }
+
+ @Test
+ void nullTargetLoadsAllPlugins() throws Exception {
+ writeTwoPluginConfig("alpha", "beta");
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, null);
+
+ assertThat(plugins).containsKey("alpha")
+ .containsKey("beta");
+ }
+
+ @Test
+ void shellTargetLoadsAllPlugins() throws Exception {
+ writeTwoPluginConfig("alpha", "beta");
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "shell");
+
+ assertThat(plugins).containsKey("alpha")
+ .containsKey("beta");
+ }
+
+ // ---- The "run" command should not load unrelated plugins ----
+
+ @Test
+ void runCommandDoesNotLoadUnrelatedPlugin() throws Exception {
+ writeTwoPluginConfig("alpha", "beta");
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "run");
+
+ assertThat(plugins).doesNotContainKey("alpha")
+ .doesNotContainKey("beta");
+ }
+
+ // ---- The "export" command should not load unrelated plugins ----
+
+ @Test
+ void exportCommandDoesNotLoadUnrelatedPlugin() throws Exception {
+ writeTwoPluginConfig("alpha", "beta");
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "export");
+
+ assertThat(plugins).doesNotContainKey("alpha")
+ .doesNotContainKey("beta");
+ }
+
+ // ---- A broken unrelated plugin should not break the "run" target ----
+
+ @Test
+ void brokenUnrelatedPluginDoesNotBreakTargetedResolution() throws
Exception {
+ Path goodJar = tempDir.resolve("fake-good.jar");
+ FakePluginJar.write(goodJar, "good");
+
+ writeConfig(
+ buildEntry("good", camelVersion, goodJar,
Files.getLastModifiedTime(goodJar).toMillis()),
+ buildBrokenEntry("broken"));
+
+ CamelJBangMain main = new CamelJBangMain();
+
+ // targeting "good" should skip the broken plugin entirely
+ assertThatCode(() -> PluginHelper.getActivePlugins(main, null, "good"))
+ .doesNotThrowAnyException();
+
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "good");
+ assertThat(plugins).containsKey("good")
+ .doesNotContainKey("broken");
+ }
+
+ @Test
+ void brokenUnrelatedPluginBreaksNullTarget() throws Exception {
+ Path goodJar = tempDir.resolve("fake-good.jar");
+ FakePluginJar.write(goodJar, "good");
+
+ writeConfig(
+ buildEntry("good", camelVersion, goodJar,
Files.getLastModifiedTime(goodJar).toMillis()),
+ buildBrokenEntry("broken"));
+
+ CamelJBangMain main = new CamelJBangMain();
+
+ // null target loads ALL plugins, so the broken one causes a failure
+ assertThatThrownBy(() -> PluginHelper.getActivePlugins(main, null,
null))
+ .isInstanceOf(CliException.class)
+ .hasMessageContaining("camel-jbang-plugin-broken not found");
+ }
+
+ // ---- appliesTo filtering ----
+
+ @Test
+ void pluginWithAppliesToIsLoadedForMatchingTarget() throws Exception {
+ Path jar = tempDir.resolve("fake-exporter.jar");
+ FakePluginJar.write(jar, "exporter");
+
+ JsonObject entry = buildEntry("exporter", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
+ entry.put("appliesTo", jsonArray("export"));
+ writeConfig(entry);
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "export");
+
+ assertThat(plugins).containsKey("exporter");
+ }
+
+ @Test
+ void pluginWithAppliesToIsNotLoadedForNonMatchingTarget() throws Exception
{
+ Path jar = tempDir.resolve("fake-exporter.jar");
+ FakePluginJar.write(jar, "exporter");
+
+ JsonObject entry = buildEntry("exporter", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
+ entry.put("appliesTo", jsonArray("export"));
+ writeConfig(entry);
+
+ CamelJBangMain main = new CamelJBangMain();
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "run");
+
+ assertThat(plugins).doesNotContainKey("exporter");
+ }
+
+ @Test
+ void pluginWithAppliesToExportIsLoadedByCliExportContext() throws
Exception {
+ Path jar = tempDir.resolve("fake-exporter.jar");
+ FakePluginJar.write(jar, "exporter");
+
+ JsonObject entry = buildEntry("exporter", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
+ entry.put("appliesTo", jsonArray("export"));
+ writeConfig(entry);
+
+ StringPrinter printer = new StringPrinter();
+ CamelJBangMain main = new CamelJBangMain().withPrinter(printer);
+
+ ExportContext context = CliExportContext.create(main, printer, true);
+
+ assertThatCode(() -> context.getPlugins(null))
+ .doesNotThrowAnyException();
+ }
+
+ // ---- auto-migration of old configs for known plugins ----
+
+ @Test
+ void oldConfigForKnownPluginIsAutoMigrated() throws Exception {
+ Path jar = tempDir.resolve("fake-test.jar");
+ FakePluginJar.write(jar, "test");
+
+ // simulate old config: known plugin "test" without appliesTo
+ JsonObject entry = buildEntry("test", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
+ writeConfig(entry);
+
+ StringPrinter printer = new StringPrinter();
+ CamelJBangMain main = new CamelJBangMain().withPrinter(printer);
+
+ // target "export" — without migration, "test" plugin would be skipped
+ Map<String, Plugin> plugins = PluginHelper.getActivePlugins(main,
null, "export");
+ assertThat(plugins).containsKey("test");
+
+ // verify the config was updated on disk
+ JsonObject savedConfig = PluginHelper.getPluginConfig();
+ JsonObject savedPlugins = savedConfig.getMap("plugins");
+ JsonObject savedPlugin = savedPlugins.getMap("test");
+ assertThat(savedPlugin.get("appliesTo"))
+ .isInstanceOf(List.class)
+ .asList().contains("export");
+ }
+
+ @Test
+ void oldConfigForKnownPluginEmitsWarning() throws Exception {
+ Path jar = tempDir.resolve("fake-test.jar");
+ FakePluginJar.write(jar, "test");
+
+ JsonObject entry = buildEntry("test", camelVersion, jar,
Files.getLastModifiedTime(jar).toMillis());
+ writeConfig(entry);
+
+ StringPrinter printer = new StringPrinter();
+ CamelJBangMain main = new CamelJBangMain().withPrinter(printer);
+
+ PluginHelper.getActivePlugins(main, null, "export");
+
+ assertThat(printer.getOutput())
+ .contains("appliesTo");
+ }
+
+ // ---- CliExportContext should not resolve unrelated plugins ----
+
+ @Test
+ void cliExportContextDoesNotResolveUnrelatedPlugins() throws Exception {
+ Path goodJar = tempDir.resolve("fake-good.jar");
+ FakePluginJar.write(goodJar, "good");
+
+ writeConfig(
+ buildEntry("good", camelVersion, goodJar,
Files.getLastModifiedTime(goodJar).toMillis()),
+ buildBrokenEntry("broken"));
+
+ StringPrinter printer = new StringPrinter();
+ CamelJBangMain main = new CamelJBangMain().withPrinter(printer);
+
+ ExportContext context = CliExportContext.create(main, printer, true);
+
+ // CliExportContext.create() calls getActivePlugins(main, repos)
WITHOUT a target filter.
Review Comment:
Done in the second commit — comment updated.
_Claude Code on behalf of Christoph Deppisch_
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java:
##########
@@ -817,6 +835,12 @@ public static void enable(PluginType pluginType) {
newPlugin.put("command", pluginType.getCommand());
newPlugin.put("description", pluginType.getDescription());
newPlugin.put("firstVersion", pluginType.getFirstVersion());
+ String[] appliesTo = pluginType.getAppliesTo();
+ if (appliesTo.length > 0) {
+ JsonArray appliesToArray = new JsonArray();
Review Comment:
Done in the second commit — extracted `toJsonArray(String[])` and use it in
all three sites.
_Claude Code on behalf of Christoph Deppisch_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]