|
| 1 | +/* |
| 2 | + * Copyright 2026-present 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.core; |
| 17 | + |
| 18 | +import java.io.PrintWriter; |
| 19 | +import java.io.StringWriter; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.shell.core.command.AbstractCommand; |
| 27 | +import org.springframework.shell.core.command.Command; |
| 28 | +import org.springframework.shell.core.command.CommandContext; |
| 29 | +import org.springframework.shell.core.command.CommandParser; |
| 30 | +import org.springframework.shell.core.command.CommandRegistry; |
| 31 | +import org.springframework.shell.core.command.ExitStatus; |
| 32 | +import org.springframework.shell.core.command.ParsedInput; |
| 33 | +import org.springframework.shell.core.command.availability.Availability; |
| 34 | +import org.springframework.shell.core.command.availability.AvailabilityProvider; |
| 35 | + |
| 36 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 37 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests for {@link InteractiveShellRunner}. |
| 42 | + * |
| 43 | + * @author Mahmoud Ben Hassine |
| 44 | + */ |
| 45 | +class InteractiveShellRunnerTests { |
| 46 | + |
| 47 | + @Test |
| 48 | + void testUnavailableCommandDoesNotAlsoPrintGenericErrorMessage() throws Exception { |
| 49 | + // given |
| 50 | + Command command = new AbstractCommand("test", "A test command") { |
| 51 | + |
| 52 | + @Override |
| 53 | + public AvailabilityProvider getAvailabilityProvider() { |
| 54 | + return AvailabilityProvider.of(Availability.unavailable("you are not logged in")); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public ExitStatus doExecute(CommandContext commandContext) { |
| 59 | + return ExitStatus.OK; |
| 60 | + } |
| 61 | + }; |
| 62 | + CommandRegistry commandRegistry = new CommandRegistry(Set.of(command)); |
| 63 | + CommandParser commandParser = input -> ParsedInput.builder().commandName(input).build(); |
| 64 | + List<String> lines = new ArrayList<>(List.of("test")); |
| 65 | + InputProvider inputProvider = () -> lines.isEmpty() ? null : lines.remove(0); |
| 66 | + StringWriter outputWriter = new StringWriter(); |
| 67 | + List<String> printedMessages = new ArrayList<>(); |
| 68 | + InteractiveShellRunner shellRunner = new InteractiveShellRunner(inputProvider, commandParser, commandRegistry) { |
| 69 | + |
| 70 | + @Override |
| 71 | + public void print(String message) { |
| 72 | + printedMessages.add(message); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void flush() { |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public PrintWriter getWriter() { |
| 81 | + return new PrintWriter(outputWriter); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public InputReader getReader() { |
| 86 | + return mock(InputReader.class); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + // when |
| 91 | + shellRunner.run(new String[0]); |
| 92 | + |
| 93 | + // then |
| 94 | + assertEquals("Command 'test' exists but is not currently available because you are not logged in", |
| 95 | + outputWriter.toString().trim()); |
| 96 | + assertFalse(printedMessages.stream().anyMatch(message -> message.startsWith("Error while executing command"))); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments