davsclaus commented on code in PR #27951:
URL: https://github.com/apache/camel/pull/27951#discussion_r4296046339


##########
dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/BooleanOptionsTest.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.commands;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import picocli.CommandLine;
+import picocli.CommandLine.Model.CommandSpec;
+import picocli.CommandLine.Model.OptionSpec;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * A boolean option given without a value is on. picocli sets a flag to the 
opposite of its default, so an option that
+ * is on by default needs <tt>arity = "0..1", fallbackValue = "true"</tt>: 
otherwise <tt>--maven-wrapper</tt> would turn
+ * the Maven wrapper off.
+ */
+class BooleanOptionsTest extends CamelCommandBaseTestSupport {
+
+    private CommandLine commandLine;
+
+    @BeforeEach
+    void commandLine() {
+        StringWriter help = new StringWriter();
+        CamelJBangMain main = new CamelJBangMain() {
+            @Override
+            public void quit(int exitCode) {
+                // in the test
+            }
+
+            @Override
+            public void postAddCommands(CommandLine commandLine, String[] 
args) {
+                commandLine.setOut(new PrintWriter(help));
+                BooleanOptionsTest.this.commandLine = commandLine;
+            }
+        }.withPrinter(printer);
+        main.setDiscoverPlugins(false);
+        main.execute("--help");
+    }
+
+    @Test
+    void everyOptionThatIsOnByDefaultIsOnWhenGivenWithoutValue() {
+        List<String> wrong = new ArrayList<>();
+        check(commandLine, wrong);
+
+        assertThat(wrong).as("boolean options that are on by default, without 
arity = \"0..1\", fallbackValue = \"true\"")
+                .isEmpty();
+    }
+
+    @Test
+    void flagWithoutValue() {
+        assertThat(option("--maven-wrapper", "export", 
"--maven-wrapper")).isEqualTo(true);
+        assertThat(option("--maven-wrapper", "export", 
"--maven-wrapper=false")).isEqualTo(false);
+        assertThat(option("--maven-wrapper", "export")).isEqualTo(true);
+
+        // the local configuration is written only with --global=false
+        assertThat(option("--global", "config", "set", "--global", 
"foo=bar")).isEqualTo(true);
+        assertThat(option("--global", "config", "set", "--global=false", 
"foo=bar")).isEqualTo(false);
+
+        // the name after the flag is not taken as its value
+        CommandSpec log = parse("log", "--timestamp", "orders");
+        assertThat(log.findOption("--timestamp").<Boolean> 
getValue()).isTrue();
+        assertThat(log.positionalParameters().get(0).<String> 
getValue()).isEqualTo("orders");
+    }
+
+    // the value of the option of the command, once the command line is parsed
+    private Object option(String name, String... args) {
+        return parse(args).findOption(name).getValue();
+    }
+
+    private CommandSpec parse(String... args) {
+        CommandLine.ParseResult result = commandLine.parseArgs(args);
+        while (result.hasSubcommand()) {
+            result = result.subcommand();
+        }
+        return result.commandSpec();
+    }
+
+    private static void check(CommandLine commandLine, List<String> wrong) {
+        CommandSpec spec = commandLine.getCommandSpec();
+        for (OptionSpec option : spec.options()) {
+            boolean flag = option.type() == boolean.class || option.type() == 
Boolean.class;
+            boolean onByDefault = "true".equals(option.defaultValue()) || 
Boolean.TRUE.equals(option.initialValue());

Review Comment:
   Good catch, fixed in the next commit with a test that checks the arity and 
the fallback value of `--logging-color` on `run`, `dev` and `debug` directly, 
whatever the environment (it fails with `CI=true` when the fallback is removed).
   
   I did not add `defaultValue = "true"`: picocli applies the default value of 
the annotation when the option is not given, over the field initializer, so it 
would turn the colors on in CI and drop the auto-detection of 
`EnvironmentHelper.isColorEnabled()`.
   
   _Claude Code on behalf of davsclaus_



-- 
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]

Reply via email to