1+ package dev .dochia .cli .core .command ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import org .junit .jupiter .api .extension .ExtendWith ;
5+ import org .mockito .ArgumentCaptor ;
6+ import org .mockito .Captor ;
7+ import org .mockito .Mock ;
8+ import org .mockito .junit .jupiter .MockitoExtension ;
9+ import picocli .CommandLine ;
10+
11+ import java .io .PrintWriter ;
12+ import java .io .StringWriter ;
13+
14+ import static org .assertj .core .api .Assertions .assertThat ;
15+ import static org .mockito .ArgumentMatchers .any ;
16+ import static org .mockito .Mockito .verify ;
17+ import static org .mockito .Mockito .when ;
18+
19+ @ ExtendWith (MockitoExtension .class )
20+ class ShortErrorMessageHandlerTest {
21+
22+ @ Mock
23+ private CommandLine mockCommandLine ;
24+ @ Mock
25+ private CommandLine .Model .CommandSpec mockCommandSpec ;
26+ @ Mock
27+ private CommandLine .IExitCodeExceptionMapper mockExitCodeMapper ;
28+ @ Captor
29+ private ArgumentCaptor <String > stringCaptor ;
30+
31+ private final StringWriter out = new StringWriter ();
32+ private final StringWriter err = new StringWriter ();
33+ private final ShortErrorMessageHandler handler = new ShortErrorMessageHandler ();
34+
35+ private CommandLine .Help .ColorScheme createColorScheme () {
36+ return CommandLine .Help .defaultColorScheme (CommandLine .Help .Ansi .OFF );
37+ }
38+
39+ @ Test
40+ void shouldPrintFullUsageAndReturnOkWhenHelpFullArgPresent () {
41+ // Given
42+ when (mockCommandLine .getOut ()).thenReturn (new PrintWriter (out ));
43+ String [] args = {"--help-full" };
44+ CommandLine .ParameterException ex = new CommandLine .ParameterException (mockCommandLine , "test error" );
45+
46+ // When
47+ int exitCode = handler .handleParseException (ex , args );
48+
49+ // Then
50+ assertThat (exitCode ).isEqualTo (CommandLine .ExitCode .OK );
51+ verify (mockCommandLine ).usage (any (PrintWriter .class ));
52+ }
53+
54+ @ Test
55+ void shouldPrintErrorAndSuggestionsWhenHelpFullNotPresent () {
56+ // Given
57+ when (mockCommandLine .getErr ()).thenReturn (new PrintWriter (err ));
58+ when (mockCommandLine .getCommandSpec ()).thenReturn (mockCommandSpec );
59+ when (mockCommandSpec .exitCodeOnInvalidInput ()).thenReturn (CommandLine .ExitCode .USAGE );
60+ when (mockCommandLine .getColorScheme ()).thenReturn (createColorScheme ());
61+ String [] args = {};
62+ CommandLine .ParameterException ex = new CommandLine .ParameterException (mockCommandLine , "test error" );
63+
64+ // When
65+ int exitCode = handler .handleParseException (ex , args );
66+
67+ // Then
68+ assertThat (exitCode ).isEqualTo (CommandLine .ExitCode .USAGE );
69+ assertThat (err .toString ())
70+ .contains ("test error" );
71+ }
72+
73+ @ Test
74+ void shouldUseMappedExitCodeWhenExitCodeMapperPresent () {
75+ // Given
76+ when (mockCommandLine .getExitCodeExceptionMapper ()).thenReturn (mockExitCodeMapper );
77+ when (mockCommandLine .getErr ()).thenReturn (new PrintWriter (err ));
78+ when (mockExitCodeMapper .getExitCode (any ())).thenReturn (42 );
79+ when (mockCommandLine .getColorScheme ()).thenReturn (createColorScheme ());
80+ String [] args = {};
81+ CommandLine .ParameterException ex = new CommandLine .ParameterException (mockCommandLine , "test error" );
82+
83+ // When
84+ int exitCode = handler .handleParseException (ex , args );
85+
86+ // Then
87+ assertThat (exitCode ).isEqualTo (42 );
88+ verify (mockExitCodeMapper ).getExitCode (ex );
89+ }
90+
91+ @ Test
92+ void shouldUseDefaultExitCodeWhenNoExitCodeMapper () {
93+ // Given
94+ when (mockCommandLine .getExitCodeExceptionMapper ()).thenReturn (null );
95+ when (mockCommandLine .getErr ()).thenReturn (new PrintWriter (err ));
96+ // when(mockCommandLine.getUsageHelpWidth()).thenReturn(80);
97+ when (mockCommandLine .getCommandSpec ()).thenReturn (mockCommandSpec );
98+ when (mockCommandSpec .exitCodeOnInvalidInput ()).thenReturn (123 );
99+ when (mockCommandLine .getColorScheme ()).thenReturn (createColorScheme ());
100+ String [] args = {};
101+ CommandLine .ParameterException ex = new CommandLine .ParameterException (mockCommandLine , "test error" );
102+
103+ // When
104+ int exitCode = handler .handleParseException (ex , args );
105+
106+ // Then
107+ assertThat (exitCode ).isEqualTo (123 );
108+ }
109+ }
0 commit comments