Fix multiple invocations of the "shell" command

JCommander doesn't seem to reset objects when it populates them with data from
an argument list during command processing, so recreate the command objects
every time we do a run().

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=191332392
This commit is contained in:
mmuller 2018-04-02 12:46:13 -07:00 committed by jianglai
parent 54a8cd09ea
commit 18290911a0
2 changed files with 92 additions and 37 deletions

View file

@ -15,13 +15,18 @@
package google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.mockito.Mockito.mock;
import com.beust.jcommander.MissingCommandException;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -60,9 +65,63 @@ public class ShellCommandTest {
public ArrayList<ImmutableList<String>> calls = new ArrayList<>();
@Override
public void run(String[] args)
throws Exception {
public void run(String[] args) throws Exception {
calls.add(ImmutableList.copyOf(args));
}
}
@Test
public void testMultipleCommandInvocations() throws Exception {
RegistryCli cli =
new RegistryCli("unittest", ImmutableMap.of("test_command", TestCommand.class));
RegistryToolEnvironment.UNITTEST.setup();
cli.setEnvironment(RegistryToolEnvironment.UNITTEST);
cli.run(new String[] {"test_command", "-x", "xval", "arg1", "arg2"});
cli.run(new String[] {"test_command", "-x", "otherxval", "arg3"});
cli.run(new String[] {"test_command"});
assertThat(TestCommand.commandInvocations)
.containsExactly(
ImmutableList.of("xval", "arg1", "arg2"),
ImmutableList.of("otherxval", "arg3"),
ImmutableList.of("default value"));
}
@Test
public void testNonExistentCommand() throws Exception {
RegistryCli cli =
new RegistryCli("unittest", ImmutableMap.of("test_command", TestCommand.class));
cli.setEnvironment(RegistryToolEnvironment.UNITTEST);
assertThrows(MissingCommandException.class, () -> cli.run(new String[] {"bad_command"}));
}
@Parameters(commandDescription = "Test command")
static class TestCommand implements Command {
@Parameter(
names = {"-x", "--xparam"},
description = "test parameter"
)
String xparam = "default value";
// List for recording command invocations by run().
//
// This has to be static because it gets populated by multiple TestCommand instances, which are
// created in RegistryCli by using reflection to call the constructor.
static final List<List<String>> commandInvocations = new ArrayList<>();
@Parameter(description = "normal argument")
List<String> args;
public TestCommand() {}
@Override
public void run() {
ImmutableList.Builder<String> callRecord = new ImmutableList.Builder<>();
callRecord.add(xparam);
if (args != null) {
callRecord.addAll(args);
}
commandInvocations.add(callRecord.build());
}
}
}