16
16
package org .springframework .shell .jline ;
17
17
18
18
import 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 ;
19
25
20
26
import org .springframework .boot .DefaultApplicationArguments ;
27
+ import org .springframework .shell .InputProvider ;
28
+ import org .springframework .shell .Shell ;
29
+ import org .springframework .shell .context .DefaultShellContext ;
21
30
22
31
import static org .assertj .core .api .Assertions .assertThat ;
23
32
33
+ @ ExtendWith (MockitoExtension .class )
24
34
public class NonInteractiveShellRunnerTests {
25
35
36
+ @ Spy
37
+ @ InjectMocks
38
+ private Shell shell ;
39
+
26
40
@ Test
27
41
public void testEmptyArgsDontRun () {
28
42
NonInteractiveShellRunner runner = new NonInteractiveShellRunner (null , null );
@@ -36,4 +50,37 @@ public void testNonEmptyArgsRun() {
36
50
DefaultApplicationArguments args = new DefaultApplicationArguments ("hi" );
37
51
assertThat (runner .canRun (args )).isTrue ();
38
52
}
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
+ }
39
86
}
0 commit comments