Skip to content

Commit 114dbec

Browse files
committed
Fix NonInteractiveShellRunner for empty args
- Fix issue when empty args resulted runner to think it should handle this scenario effectively hijacking interactive mode. - This bug were added by previous rework on #372
1 parent 0a074b3 commit 114dbec

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public class NonInteractiveShellRunner implements ShellRunner {
5858

5959
private Parser lineParser;
6060

61-
private Function<ApplicationArguments, List<String>> commandsFromInputArgs;
61+
private Function<ApplicationArguments, List<String>> commandsFromInputArgs = args -> args
62+
.getSourceArgs().length == 0 ? Collections.emptyList()
63+
: Collections.singletonList(String.join(" ", args.getSourceArgs()));
6264

6365
public NonInteractiveShellRunner(Shell shell, ShellContext shellContext) {
6466
this.shell = shell;
6567
this.shellContext = shellContext;
6668
this.lineParser = new DefaultParser();
67-
this.commandsFromInputArgs = (args) ->
68-
Collections.singletonList(String.join(" ", args.getSourceArgs()));
6969
}
7070

7171
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.jline;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import org.springframework.boot.DefaultApplicationArguments;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
public class NonInteractiveShellRunnerTests {
25+
26+
@Test
27+
public void testEmptyArgsDontRun() {
28+
NonInteractiveShellRunner runner = new NonInteractiveShellRunner(null, null);
29+
DefaultApplicationArguments args = new DefaultApplicationArguments();
30+
assertThat(runner.canRun(args)).isFalse();
31+
}
32+
33+
@Test
34+
public void testNonEmptyArgsRun() {
35+
NonInteractiveShellRunner runner = new NonInteractiveShellRunner(null, null);
36+
DefaultApplicationArguments args = new DefaultApplicationArguments("hi");
37+
assertThat(runner.canRun(args)).isTrue();
38+
}
39+
}

0 commit comments

Comments
 (0)