Skip to content

Commit 4113eb4

Browse files
committed
Update ShellRunner#run to return void
Before this commit, `ShellRunner#run` was returning a boolean (and not void as it was before). The javadoc mentions that the "return value indicates if run operation happened and no further runners should be used", but it is not the ShellRunner's responsibility to decide the flow of execution (ie if another runner should run or not). Custom implementations have no context to decide to return true or false.
1 parent ac76047 commit 4113eb4

File tree

6 files changed

+28
-33
lines changed

6 files changed

+28
-33
lines changed

spring-shell-core/src/main/java/org/springframework/shell/core/ShellRunner.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@
2424
public interface ShellRunner {
2525

2626
/**
27-
* Execute {@code ShellRunner} with given args. Return value indicates if run
28-
* operation happened and no further runners should be used.
27+
* Execute {@code ShellRunner} with given args.
2928
* @param args the raw arguments
30-
* @return true if run execution happened
3129
* @throws Exception possible error during run
3230
*/
33-
default boolean run(String[] args) throws Exception {
34-
return false;
35-
}
31+
void run(String[] args) throws Exception;
3632

3733
}

spring-shell-core/src/main/java/org/springframework/shell/core/jline/InteractiveShellRunner.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2022 the original author or authors.
2+
* Copyright 2017-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,11 +69,10 @@ public InteractiveShellRunner(LineReader lineReader, PromptProvider promptProvid
6969
}
7070

7171
@Override
72-
public boolean run(String[] args) throws Exception {
72+
public void run(String[] args) throws Exception {
7373
shellContext.setInteractionMode(InteractionMode.INTERACTIVE);
7474
InputProvider inputProvider = new JLineInputProvider(lineReader, promptProvider);
7575
shell.run(inputProvider);
76-
return true;
7776
}
7877

7978
public static class JLineInputProvider implements InputProvider {

spring-shell-core/src/main/java/org/springframework/shell/core/jline/NonInteractiveShellRunner.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2024 the original author or authors.
2+
* Copyright 2021-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -133,18 +133,17 @@ public void setLineParser(Parser lineParser) {
133133
}
134134

135135
@Override
136-
public boolean run(String[] args) throws Exception {
136+
public void run(String[] args) throws Exception {
137137
List<String> commands = commandsFromArgs.apply(args);
138138
if (commands.isEmpty()) {
139-
return false;
139+
return;
140140
}
141141
List<ParsedLine> parsedLines = commands.stream()
142142
.map(rawCommandLine -> lineParser.parse(rawCommandLine, rawCommandLine.length() + 1))
143143
.collect(Collectors.toList());
144144
MultiParsedLineInputProvider inputProvider = new MultiParsedLineInputProvider(parsedLines);
145145
shellContext.setInteractionMode(InteractionMode.NONINTERACTIVE);
146146
shell.run(inputProvider);
147-
return true;
148147
}
149148

150149
/**

spring-shell-core/src/main/java/org/springframework/shell/core/jline/ScriptShellRunner.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2024 the original author or authors.
2+
* Copyright 2018-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,13 +64,13 @@ public ScriptShellRunner(Parser parser, Shell shell) {
6464
}
6565

6666
@Override
67-
public boolean run(String[] args) throws Exception {
67+
public void run(String[] args) throws Exception {
6868
String[] sourceArgs = args;
6969
if (ObjectUtils.isEmpty(sourceArgs)) {
70-
return false;
70+
return;
7171
}
7272
if (!(sourceArgs[0].startsWith("@") && sourceArgs[0].length() > 1)) {
73-
return false;
73+
return;
7474
}
7575

7676
List<File> scriptsToRun = Arrays.asList(args)
@@ -85,8 +85,6 @@ public boolean run(String[] args) throws Exception {
8585
shell.run(inputProvider);
8686
}
8787
}
88-
89-
return true;
9088
}
9189

9290
}

spring-shell-core/src/test/java/org/springframework/shell/core/jline/NonInteractiveShellRunnerTests.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,26 @@ class NonInteractiveShellRunnerTests {
4141

4242
@Test
4343
void testEmptyArgsDontRun() throws Exception {
44-
NonInteractiveShellRunner runner = new NonInteractiveShellRunner(null, null);
45-
assertThat(runner.run(new String[0])).isFalse();
44+
NonInteractiveShellRunner runner = new NonInteractiveShellRunner(shell, null);
45+
runner.run(new String[0]);
46+
Mockito.verifyNoInteractions(shell);
4647
}
4748

4849
@Test
4950
void testNonEmptyArgsRun() throws Exception {
5051
NonInteractiveShellRunner runner = new NonInteractiveShellRunner(shell, new DefaultShellContext());
5152
ArgumentCaptor<InputProvider> valueCapture = ArgumentCaptor.forClass(InputProvider.class);
5253
Mockito.doNothing().when(shell).run(valueCapture.capture());
53-
assertThat(runner.run(ofArgs("hi"))).isTrue();
54+
runner.run(ofArgs("hi"));
55+
Mockito.verify(shell).run(Mockito.any(InputProvider.class));
5456
}
5557

5658
@Test
5759
void shouldQuoteWithWhitespace() throws Exception {
5860
NonInteractiveShellRunner runner = new NonInteractiveShellRunner(shell, new DefaultShellContext());
5961
ArgumentCaptor<InputProvider> valueCapture = ArgumentCaptor.forClass(InputProvider.class);
6062
Mockito.doNothing().when(shell).run(valueCapture.capture());
61-
assertThat(runner.run(ofArgs("foo bar"))).isTrue();
63+
runner.run(ofArgs("foo bar"));
6264
InputProvider value = valueCapture.getValue();
6365
assertThat(value.readInput().rawText()).isEqualTo("\"foo bar\"");
6466
}
@@ -68,7 +70,7 @@ void shouldNotQuoteIfQuoted() throws Exception {
6870
NonInteractiveShellRunner runner = new NonInteractiveShellRunner(shell, new DefaultShellContext());
6971
ArgumentCaptor<InputProvider> valueCapture = ArgumentCaptor.forClass(InputProvider.class);
7072
Mockito.doNothing().when(shell).run(valueCapture.capture());
71-
assertThat(runner.run(ofArgs("'foo bar'"))).isTrue();
73+
runner.run(ofArgs("'foo bar'"));
7274
InputProvider value = valueCapture.getValue();
7375
assertThat(value.readInput().rawText()).isEqualTo("'foo bar'");
7476
}
@@ -78,7 +80,7 @@ void shouldNotQuoteWithoutWhitespace() throws Exception {
7880
NonInteractiveShellRunner runner = new NonInteractiveShellRunner(shell, new DefaultShellContext());
7981
ArgumentCaptor<InputProvider> valueCapture = ArgumentCaptor.forClass(InputProvider.class);
8082
Mockito.doNothing().when(shell).run(valueCapture.capture());
81-
assertThat(runner.run(ofArgs("foobar"))).isTrue();
83+
runner.run(ofArgs("foobar"));
8284
InputProvider value = valueCapture.getValue();
8385
assertThat(value.readInput().rawText()).isEqualTo("foobar");
8486
}

spring-shell-core/src/test/java/org/springframework/shell/core/jline/ScriptShellRunnerTests.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,37 @@
2222
import org.junit.jupiter.api.extension.ExtendWith;
2323
import org.junit.jupiter.api.io.TempDir;
2424
import org.mockito.Mock;
25+
import org.mockito.Mockito;
2526
import org.mockito.junit.jupiter.MockitoExtension;
2627

2728
import org.springframework.boot.ApplicationArguments;
2829
import org.springframework.boot.DefaultApplicationArguments;
2930
import org.springframework.shell.core.Shell;
30-
import org.springframework.shell.core.jline.ScriptShellRunner;
31-
32-
import static org.assertj.core.api.Assertions.*;
3331

3432
@ExtendWith(MockitoExtension.class)
3533
class ScriptShellRunnerTests {
3634

3735
@Mock
3836
Shell shell;
3937

40-
private final ScriptShellRunner runner = new ScriptShellRunner(null, null);
38+
private final ScriptShellRunner runner = new ScriptShellRunner(null, shell);
4139

4240
@Test
4341
void shouldNotRunWhenNoArgs() throws Exception {
44-
assertThat(runner.run(ofArgs())).isFalse();
42+
runner.run(ofArgs());
43+
Mockito.verifyNoInteractions(shell);
4544
}
4645

4746
@Test
4847
void shouldNotRunWhenInOptionValue() throws Exception {
49-
assertThat(runner.run(ofArgs("--foo", "@"))).isFalse();
48+
runner.run(ofArgs("--foo", "@"));
49+
Mockito.verifyNoInteractions(shell);
5050
}
5151

5252
@Test
5353
void shouldNotRunWhenJustFirstArgWithoutFile() throws Exception {
54-
assertThat(runner.run(ofArgs("@"))).isFalse();
54+
runner.run(ofArgs("@"));
55+
Mockito.verifyNoInteractions(shell);
5556
}
5657

5758
@Test
@@ -60,7 +61,7 @@ void shouldRunWhenFirstArgHavingFile(@TempDir Path workingDir) throws Exception
6061
Path file = Files.createFile(path);
6162
String pathStr = file.toAbsolutePath().toString();
6263
ScriptShellRunner shellRunner = new ScriptShellRunner(null, shell);
63-
assertThat(shellRunner.run(new String[] { "@" + pathStr })).isTrue();
64+
shellRunner.run(new String[] { "@" + pathStr });
6465
}
6566

6667
private static ApplicationArguments ofApplicationArguments(String... args) {

0 commit comments

Comments
 (0)