Skip to content

Commit

Permalink
fix: zanata-cli basic options positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Jan 9, 2018
1 parent 05e00bb commit 1b323e9
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,19 @@ protected void processArgs(String... args) {
* @param options
*/
private void copyGlobalOptionsTo(BasicOptions options) {
options.setDebug(getDebug());
options.setErrors(getErrors());
if (!options.isDebugSet()) {
options.setDebug(getDebug());
}
if (!options.isErrorsSet()) {
options.setErrors(getErrors());
}
options.setHelp(getHelp());
options.setInteractiveMode(isInteractiveMode());
options.setQuiet(getQuiet());
if (!options.isInteractiveModeSet()) {
options.setInteractiveMode(isInteractiveMode());
}
if (!options.isQuietSet()) {
options.setQuiet(getQuiet());
}
}

private void printHelp(PrintWriter out) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public interface BasicOptions {

void setInteractiveMode(boolean interactiveMode);

boolean isInteractiveModeSet();

/**
* Used to generate the command line interface and its usage help. This name
* should match the Maven Mojo's 'goal' annotation and must match the @SubCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public abstract class BasicOptionsImpl implements BasicOptions {
private boolean quiet = false;
private boolean quietSet;
private boolean interactiveMode = true;
private boolean interactiveModeSet;
private List<CommandHook> commandHooks = new ArrayList<CommandHook>();

public BasicOptionsImpl() {
Expand Down Expand Up @@ -109,6 +110,7 @@ public boolean isInteractiveMode() {

@Override
public void setInteractiveMode(boolean interactiveMode) {
interactiveModeSet = true;
this.interactiveMode = interactiveMode;
}

Expand All @@ -133,6 +135,11 @@ public boolean isQuietSet() {
return quietSet;
}

@Override
public boolean isInteractiveModeSet() {
return interactiveModeSet;
}

@Override
public void setCommandHooks(List<CommandHook> commandHooks) {
this.commandHooks = commandHooks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,17 +362,25 @@ public static void applyUserConfig(ConfigurableOptions opts,
if (quiet != null)
opts.setQuiet(quiet);
}

if (!opts.isInteractiveModeSet()) {
Boolean batchMode = config.getBoolean("defaults.batchMode", null);
if (batchMode != null)
opts.setInteractiveMode(!batchMode);
}
if ((opts.getUsername() == null || opts.getKey() == null)
&& opts.getUrl() != null) {
SubnodeConfiguration servers = config.getSection("servers");
String prefix = ConfigUtil.findPrefix(servers, opts.getUrl());
if (prefix != null) {
if (opts.getUsername() == null) {
opts.setUsername(servers.getString(prefix + ".username",
null));
}
if (opts.getKey() == null) {
opts.setKey(servers.getString(prefix + ".key", null));
if (servers != null) {
String prefix = ConfigUtil.findPrefix(servers, opts.getUrl());
if (prefix != null) {
if (opts.getUsername() == null) {
opts.setUsername(servers.getString(prefix + ".username",
null));
}
if (opts.getKey() == null) {
opts.setKey(servers.getString(prefix + ".key", null));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.zanata.client.commands.ConsoleInteractor.DisplayMode.Question;
import static org.zanata.client.commands.ConsoleInteractor.DisplayMode.Warning;
import static org.zanata.client.commands.FileMappingRuleHandler.Placeholders.allHolders;
import static org.zanata.client.commands.Messages.get;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;

import javax.xml.bind.JAXBException;

import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleList;
Expand Down Expand Up @@ -110,6 +115,36 @@ public void optionTakesPrecedenceOverConfig() {
assertThat(opts.getExcludes()).contains("a.properties", "b.properties");
}

@Test
public void applyUserConfigTestDefault() throws MalformedURLException {
opts.setUsername("username");
opts.setUrl(new URL("http://localhost"));

HierarchicalINIConfiguration config =
Mockito.mock(HierarchicalINIConfiguration.class);
OptionsUtil.applyUserConfig(opts, config);

verify(config).getSection("servers");
verify(config).getBoolean("defaults.debug", null);
verify(config).getBoolean("defaults.errors", null);
verify(config).getBoolean("defaults.quiet", null);
verify(config).getBoolean("defaults.batchMode", null);
}

@Test
public void applyUserConfigTest() {
opts.setDebug(false);
opts.setErrors(false);
opts.setQuiet(false);
opts.setInteractiveMode(false);

HierarchicalINIConfiguration config =
Mockito.mock(HierarchicalINIConfiguration.class);
OptionsUtil.applyUserConfig(opts, config);

verifyZeroInteractions(config);
}

@Test
public void willWarnUserIfRuleSeemsWrong() {
opts.setInteractiveMode(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public abstract class ConfigurableMojo<O extends ConfigurableOptions> extends
@Parameter(defaultValue = "${settings.interactiveMode}")
private boolean interactiveMode = true;

private boolean interactiveModeSet;

/**
* Enable HTTP message logging.
*/
Expand Down Expand Up @@ -197,13 +199,19 @@ public boolean isQuietSet() {
return !getLog().isInfoEnabled();
}

@Override
public boolean isInteractiveModeSet() {
return interactiveModeSet;
}

@Override
public boolean isInteractiveMode() {
return interactiveMode;
}

@Override
public void setInteractiveMode(boolean interactiveMode) {
interactiveModeSet = true;
this.interactiveMode = interactiveMode;
}

Expand Down

0 comments on commit 1b323e9

Please sign in to comment.