1616package org .springframework .shell .jline ;
1717
1818import org .junit .jupiter .api .Test ;
19+ import org .junit .jupiter .api .extension .ExtendWith ;
20+ import org .mockito .ArgumentCaptor ;
21+ import org .mockito .InjectMocks ;
22+ import org .mockito .Mockito ;
23+ import org .mockito .Spy ;
24+ import org .mockito .junit .jupiter .MockitoExtension ;
1925
2026import org .springframework .boot .DefaultApplicationArguments ;
27+ import org .springframework .shell .InputProvider ;
28+ import org .springframework .shell .Shell ;
29+ import org .springframework .shell .context .DefaultShellContext ;
2130
2231import static org .assertj .core .api .Assertions .assertThat ;
2332
33+ @ ExtendWith (MockitoExtension .class )
2434public class NonInteractiveShellRunnerTests {
2535
36+ @ Spy
37+ @ InjectMocks
38+ private Shell shell ;
39+
2640 @ Test
2741 public void testEmptyArgsDontRun () {
2842 NonInteractiveShellRunner runner = new NonInteractiveShellRunner (null , null );
@@ -36,4 +50,37 @@ public void testNonEmptyArgsRun() {
3650 DefaultApplicationArguments args = new DefaultApplicationArguments ("hi" );
3751 assertThat (runner .canRun (args )).isTrue ();
3852 }
53+
54+ @ Test
55+ public void shouldQuoteWithWhitespace () throws Exception {
56+ NonInteractiveShellRunner runner = new NonInteractiveShellRunner (shell , new DefaultShellContext ());
57+ DefaultApplicationArguments args = new DefaultApplicationArguments ("foo bar" );
58+ ArgumentCaptor <InputProvider > valueCapture = ArgumentCaptor .forClass (InputProvider .class );
59+ Mockito .doNothing ().when (shell ).run (valueCapture .capture ());
60+ runner .run (args );
61+ InputProvider value = valueCapture .getValue ();
62+ assertThat (value .readInput ().rawText ()).isEqualTo ("\" foo bar\" " );
63+ }
64+
65+ @ Test
66+ public void shouldNotQuoteIfQuoted () throws Exception {
67+ NonInteractiveShellRunner runner = new NonInteractiveShellRunner (shell , new DefaultShellContext ());
68+ DefaultApplicationArguments args = new DefaultApplicationArguments ("'foo bar'" );
69+ ArgumentCaptor <InputProvider > valueCapture = ArgumentCaptor .forClass (InputProvider .class );
70+ Mockito .doNothing ().when (shell ).run (valueCapture .capture ());
71+ runner .run (args );
72+ InputProvider value = valueCapture .getValue ();
73+ assertThat (value .readInput ().rawText ()).isEqualTo ("'foo bar'" );
74+ }
75+
76+ @ Test
77+ public void shouldNotQuoteWithoutWhitespace () throws Exception {
78+ NonInteractiveShellRunner runner = new NonInteractiveShellRunner (shell , new DefaultShellContext ());
79+ DefaultApplicationArguments args = new DefaultApplicationArguments ("foobar" );
80+ ArgumentCaptor <InputProvider > valueCapture = ArgumentCaptor .forClass (InputProvider .class );
81+ Mockito .doNothing ().when (shell ).run (valueCapture .capture ());
82+ runner .run (args );
83+ InputProvider value = valueCapture .getValue ();
84+ assertThat (value .readInput ().rawText ()).isEqualTo ("foobar" );
85+ }
3986}
0 commit comments