From 114dbeccaabb632b26bbd061a94d0baafc4bc466 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Tue, 22 Feb 2022 09:47:20 +0000 Subject: [PATCH] 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 --- .../jline/NonInteractiveShellRunner.java | 6 +-- .../jline/NonInteractiveShellRunnerTests.java | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 spring-shell-core/src/test/java/org/springframework/shell/jline/NonInteractiveShellRunnerTests.java diff --git a/spring-shell-core/src/main/java/org/springframework/shell/jline/NonInteractiveShellRunner.java b/spring-shell-core/src/main/java/org/springframework/shell/jline/NonInteractiveShellRunner.java index 0738dba8..20cccf58 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/jline/NonInteractiveShellRunner.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/jline/NonInteractiveShellRunner.java @@ -58,14 +58,14 @@ public class NonInteractiveShellRunner implements ShellRunner { private Parser lineParser; - private Function> commandsFromInputArgs; + private Function> commandsFromInputArgs = args -> args + .getSourceArgs().length == 0 ? Collections.emptyList() + : Collections.singletonList(String.join(" ", args.getSourceArgs())); public NonInteractiveShellRunner(Shell shell, ShellContext shellContext) { this.shell = shell; this.shellContext = shellContext; this.lineParser = new DefaultParser(); - this.commandsFromInputArgs = (args) -> - Collections.singletonList(String.join(" ", args.getSourceArgs())); } /** diff --git a/spring-shell-core/src/test/java/org/springframework/shell/jline/NonInteractiveShellRunnerTests.java b/spring-shell-core/src/test/java/org/springframework/shell/jline/NonInteractiveShellRunnerTests.java new file mode 100644 index 00000000..8f04ab36 --- /dev/null +++ b/spring-shell-core/src/test/java/org/springframework/shell/jline/NonInteractiveShellRunnerTests.java @@ -0,0 +1,39 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.jline; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.DefaultApplicationArguments; + +import static org.assertj.core.api.Assertions.assertThat; + +public class NonInteractiveShellRunnerTests { + + @Test + public void testEmptyArgsDontRun() { + NonInteractiveShellRunner runner = new NonInteractiveShellRunner(null, null); + DefaultApplicationArguments args = new DefaultApplicationArguments(); + assertThat(runner.canRun(args)).isFalse(); + } + + @Test + public void testNonEmptyArgsRun() { + NonInteractiveShellRunner runner = new NonInteractiveShellRunner(null, null); + DefaultApplicationArguments args = new DefaultApplicationArguments("hi"); + assertThat(runner.canRun(args)).isTrue(); + } +}