Additional testings on ignored option warning (#3993)

Add testing of TOML and ENV in command line util test

Signed-off-by: wcgcyx <wcgcyx@gmail.com>
pull/4000/head
Zhenyang Shi 2 years ago committed by GitHub
parent 5702ca090d
commit 2a7089cf9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java
  2. 2
      besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java
  3. 52
      besu/src/test/java/org/hyperledger/besu/cli/CommandLineUtilsTest.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<String> 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;
}
}
}

@ -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");

@ -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<String, String> 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
*

Loading…
Cancel
Save