Skip to content

Commit

Permalink
[#439] Bugfix: CommandSpec injected into Mixins had a null CommandLine
Browse files Browse the repository at this point in the history
Closes #439
  • Loading branch information
remkop committed Aug 11, 2018
1 parent 2230750 commit dc2b2fc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -3196,6 +3196,9 @@ void validate() {
/** Sets the CommandLine constructed with this {@code CommandSpec} model. */
protected CommandSpec commandLine(CommandLine commandLine) {
this.commandLine = commandLine;
for (CommandSpec mixedInSpec : mixins.values()) {
mixedInSpec.commandLine(commandLine);
}
for (CommandLine sub : commands.values()) {
sub.getCommandSpec().parent(this);
}
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/picocli/CommandLineMixinTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.File;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Callable;

import static org.junit.Assert.*;
import static picocli.HelpTestUtil.usageString;
Expand Down Expand Up @@ -785,4 +786,68 @@ public void testMixinStandardHelpOptions_AreAddedLast() {
assertEquals(expected, baos.toString());
}

static class Issue439Mixin {
@Spec CommandSpec spec;

@Option(names = "--trex")
void setTRexFences(final String value) {
throw new ParameterException(spec.commandLine(), "TREX error");
}
}

static class Issue439Command {
@Mixin Issue439Mixin mixin;
@Spec CommandSpec spec;

@Option(names = "--raptor")
void setRaptorFences(final String value) {
throw new ParameterException(spec.commandLine(), "RAPTOR error");
}
}

@Test
public void testIssue439InjectedSpecInMixinHasNullCommandLineAnnotations() {
CommandLine cmd = new CommandLine(new Issue439Command());
assertExceptionThrownFromSetter(cmd);
}

@Test
public void testIssue439InjectedSpecInMixinHasNullCommandLineProgrammatic() {
final CommandSpec mixinSpec = CommandSpec.create();
ISetter trexSetter = new ISetter() {
public <T> T set(T value) {
throw new ParameterException(mixinSpec.commandLine(), "TREX error");
}
};
mixinSpec.addOption(OptionSpec.builder("--trex").
type(String.class).setter(trexSetter).build());

final CommandSpec commandSpec = CommandSpec.create();
commandSpec.addMixin("mixin", mixinSpec);
ISetter raptorSetter = new ISetter() {
public <T> T set(T value) {
throw new ParameterException(commandSpec.commandLine(), "RAPTOR error");
}
};
commandSpec.addOption(OptionSpec.builder("--raptor").
type(String.class).setter(raptorSetter).build());

CommandLine cmd = new CommandLine(commandSpec);
assertExceptionThrownFromSetter(cmd);
}

private void assertExceptionThrownFromSetter(CommandLine cmd) {
try {
cmd.parse("--trex", "abc");
fail("expected ParameterException");
} catch (ParameterException ex) {
assertEquals("TREX error", ex.getMessage());
}
try {
cmd.parse("--raptor", "xyz");
fail("expected ParameterException");
} catch (ParameterException ex) {
assertEquals("RAPTOR error", ex.getMessage());
}
}
}

0 comments on commit dc2b2fc

Please sign in to comment.