Skip to content

Parse commented lines like an empty lines #1168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ public Input readInput() {
if (line == null) {
return null;
} else {
ParsedLine parsedLine = parser.parse(sb.toString(), sb.toString().length());
// gh-277: if it's a commented line then skip as it is equal to NO_INPUT
ParsedLine parsedLine;
if (isCommentedLine(line)) {
parsedLine = parser.parse("", -1, Parser.ParseContext.COMPLETE);
} else {
parsedLine = parser.parse(sb.toString(), sb.toString().length());
}
return new ParsedLineInput(parsedLine);
}
}
Expand All @@ -75,4 +81,8 @@ public Input readInput() {
public void close() throws IOException {
reader.close();
}

private boolean isCommentedLine(String line) {
return line.matches("\\s*//.*");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.springframework.shell.jline;

import org.jline.reader.EOFError;
import org.jline.reader.impl.DefaultParser;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.Reader;
import java.io.StringReader;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

class FileInputProviderTests {
private final ExtendedDefaultParser springParser = new ExtendedDefaultParser();
private final DefaultParser jlineParser = new DefaultParser();
private FileInputProvider fileInputProvider;

static Stream<Arguments> regularLinesUnclosedQuotes() {
return Stream.of(
Arguments.of("Regular line with unclosed 'quote"),
Arguments.of("Regular line with unclosed \"quote")
);
}

static Stream<Arguments> commentsUnclosedQuotes() {
return Stream.of(
Arguments.of("//Commented line with unclosed 'quote"),
Arguments.of("//Commented line with unclosed \"quote")
);
}

@ParameterizedTest
@MethodSource("regularLinesUnclosedQuotes")
void shouldThrowOnUnclosedQuoteDefaultParser(String line) {
jlineParser.setEofOnUnclosedQuote(true);
Reader reader = new StringReader(line);
fileInputProvider = new FileInputProvider(reader, jlineParser);
Exception exception = assertThrows(EOFError.class, () -> {
fileInputProvider.readInput();
});
String expectedExceptionMessage = "Missing closing quote";
String actualExceptionMessage = exception.getMessage();
assertTrue(actualExceptionMessage.contains(expectedExceptionMessage));
}

@ParameterizedTest
@MethodSource("regularLinesUnclosedQuotes")
void shouldThrowOnUnclosedQuoteExtendedParser(String line) {
springParser.setEofOnUnclosedQuote(true);
Reader reader = new StringReader(line);
fileInputProvider = new FileInputProvider(reader, springParser);
Exception exception = assertThrows(EOFError.class, () -> {
fileInputProvider.readInput();
});
String expectedExceptionMessage = "Missing closing quote";
String actualExceptionMessage = exception.getMessage();
assertTrue(actualExceptionMessage.contains(expectedExceptionMessage));
}

@ParameterizedTest
@MethodSource("commentsUnclosedQuotes")
void shoulNotThrowOnUnclosedQuoteDefaultParser(String line) {
jlineParser.setEofOnUnclosedQuote(true);
Reader reader = new StringReader(line);
fileInputProvider = new FileInputProvider(reader, jlineParser);
assertDoesNotThrow(() -> {
fileInputProvider.readInput();
});
}

@ParameterizedTest
@MethodSource("commentsUnclosedQuotes")
void shouldNotThrowOnUnclosedQuoteExtendedParser(String line) {
springParser.setEofOnUnclosedQuote(true);
Reader reader = new StringReader(line);
fileInputProvider = new FileInputProvider(reader, springParser);
assertDoesNotThrow(() -> {
fileInputProvider.readInput();
});
}
}