diff --git a/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java b/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java index 3757e116ac..92b185224f 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java @@ -20,6 +20,7 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import com.google.common.base.Strings; import org.slf4j.Logger; import picocli.CommandLine; @@ -100,18 +101,20 @@ public class CommandLineUtils { final CommandLine commandLine, final List dependentOptionsNames) { return commandLine.getCommandSpec().options().stream() .filter(option -> Arrays.stream(option.names()).anyMatch(dependentOptionsNames::contains)) - .filter( - option -> { - try { - return !option.stringValues().isEmpty() - || commandLine.getDefaultValueProvider().defaultValue(option) != null; - } catch (Exception e) { - return false; - } - }) + .filter(CommandLineUtils::isOptionSet) .map(option -> option.names()[0]) .collect( Collectors.collectingAndThen( Collectors.toList(), StringUtils.joiningWithLastDelimiter(", ", " and "))); } + + private static boolean isOptionSet(final CommandLine.Model.OptionSpec option) { + final CommandLine commandLine = option.command().commandLine(); + try { + return !option.stringValues().isEmpty() + || !Strings.isNullOrEmpty(commandLine.getDefaultValueProvider().defaultValue(option)); + } catch (final Exception e) { + return false; + } + } } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java index 4b5a0d0bfa..c93ea66b42 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java @@ -3632,7 +3632,7 @@ public class BesuCommandTest extends CommandTestAbstract { createTempFile( "toml", "miner-coinbase=\"" - + requestedCoinbase.toString() + + requestedCoinbase + "\"\n" + "min-gas-price=42\n" + "miner-extra-data=\"0x1122334455667788990011223344556677889900112233445566778899001122\"\n"); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/CommandLineUtilsTest.java b/besu/src/test/java/org/hyperledger/besu/cli/CommandLineUtilsTest.java index 863bbc16b2..f8bc8d8972 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/CommandLineUtilsTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/CommandLineUtilsTest.java @@ -21,11 +21,19 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static picocli.CommandLine.defaultExceptionHandler; import org.hyperledger.besu.cli.util.CommandLineUtils; +import org.hyperledger.besu.cli.util.EnvironmentVariableDefaultProvider; +import org.hyperledger.besu.cli.util.TomlConfigFileDefaultProvider; import org.hyperledger.besu.util.StringUtils; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; @@ -53,9 +61,12 @@ public class CommandLineUtilsTest { final CommandLine commandLine; + final Map environment = new HashMap<>(); + AbstractTestCommand(final Logger logger) { this.logger = logger; commandLine = new CommandLine(this); + commandLine.setDefaultValueProvider(new EnvironmentVariableDefaultProvider(environment)); } // Completely disables p2p within Besu. @@ -230,6 +241,47 @@ public class CommandLineUtilsTest { assertThat(testCommand.option2).isEqualTo(20); } + @Test + public void multipleMainOptionsToml() throws IOException { + final Path toml = Files.createTempFile("toml", ""); + Files.write( + toml, + ("option-enabled=false\n" + "other-option-enabled=false\n" + "option2=30") + .getBytes(StandardCharsets.UTF_8)); + toml.toFile().deleteOnExit(); + + final AbstractTestCommand testCommand = new TestMultiCommandWithDeps(mockLogger); + testCommand.commandLine.setDefaultValueProvider( + new TomlConfigFileDefaultProvider(testCommand.commandLine, toml.toFile())); + testCommand.commandLine.parseWithHandlers(new RunLast(), defaultExceptionHandler()); + + verifyMultiOptionsConstraintLoggerCall( + mockLogger, + "--option2 and/or --option3 ignored because none of --option-enabled or --other-option-enabled was defined."); + + assertThat(testCommand.optionEnabled).isFalse(); + assertThat(testCommand.otherOptionEnabled).isFalse(); + assertThat(testCommand.option2).isEqualTo(30); + } + + @Test + public void multipleMainOptionsEnv() { + final AbstractTestCommand testCommand = new TestMultiCommandWithDeps(mockLogger); + testCommand.environment.put("BESU_OPTION_ENABLED", "false"); + testCommand.environment.put("BESU_OTHER_OPTION_ENABLED", "false"); + testCommand.environment.put("BESU_OPTION2", "40"); + + testCommand.commandLine.parseWithHandlers(new RunLast(), defaultExceptionHandler()); + + verifyMultiOptionsConstraintLoggerCall( + mockLogger, + "--option2 and/or --option3 ignored because none of --option-enabled or --other-option-enabled was defined."); + + assertThat(testCommand.optionEnabled).isFalse(); + assertThat(testCommand.otherOptionEnabled).isFalse(); + assertThat(testCommand.option2).isEqualTo(40); + } + /** * Check logger calls *