Skip to content

Commit

Permalink
LineReader#readLine() should never return null, fixes #265
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed May 13, 2018
1 parent c5f68dd commit 69471c7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public String readLine(String prompt, String rightPrompt, MaskingCallback maskin
}
Binding o = readBinding(getKeys(), local);
if (o == null) {
return null;
throw new EndOfFileException();
}
Log.trace("Binding: ", o);
if (buf.length() == 0 && getLastBinding().charAt(0) == originalAttributes.getControlChar(ControlChar.VEOF)) {
Expand Down
28 changes: 20 additions & 8 deletions reader/src/test/java/org/jline/reader/impl/HistorySearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.ByteArrayInputStream;

import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.impl.history.DefaultHistory;
import org.junit.Test;
Expand All @@ -10,6 +11,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class HistorySearchTest extends ReaderTestSupport {

Expand Down Expand Up @@ -103,10 +105,13 @@ public void testSearchHistoryWithNoMatches() throws Exception {
public void testAbortingSearchRetainsCurrentBufferAndPrintsDetails() throws Exception {
DefaultHistory history = setupHistory();

String readLineResult;
in.setIn(new ByteArrayInputStream(translate("f^Rf^G").getBytes()));
readLineResult = reader.readLine();
assertEquals(null, readLineResult);
try {
reader.readLine();
fail("Expected an EndOfFileException to be thrown");
} catch (EndOfFileException e) {
// expected
}
assertEquals("f", reader.getBuffer().toString());
assertEquals(3, history.size());
}
Expand All @@ -121,8 +126,12 @@ public void testAbortingAfterSearchingPreviousLinesGivesBlank() throws Exception
assertEquals("f", readLineResult);
assertEquals(4, history.size());

readLineResult = reader.readLine();
assertEquals(null, readLineResult);
try {
reader.readLine();
fail("Expected an EndOfFileException to be thrown");
} catch (EndOfFileException e) {
// expected
}
assertEquals("", reader.getBuffer().toString());
assertEquals(4, history.size());
}
Expand All @@ -132,9 +141,12 @@ public void testSearchOnEmptyHistory() throws Exception {
DefaultHistory history = setupHistory();
history.purge();

String readLineResult;
in.setIn(new ByteArrayInputStream(translate("^Sa").getBytes()));
readLineResult = reader.readLine();
assertEquals(null, readLineResult);
try {
reader.readLine();
fail("Expected an EndOfFileException to be thrown");
} catch (EndOfFileException e) {
// expected
}
}
}
23 changes: 17 additions & 6 deletions reader/src/test/java/org/jline/reader/impl/ReaderTestSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.jline.keymap.KeyMap;
import org.jline.reader.Candidate;
import org.jline.reader.EndOfFileException;
import org.jline.terminal.Size;
import org.jline.terminal.Terminal;
import org.jline.terminal.impl.DumbTerminal;
Expand Down Expand Up @@ -103,9 +104,16 @@ protected void assertBuffer(final String expected, final TestBuffer buffer, fina
//String line;
//while ((line = reader.readLine((String) null)) != null) {
//System.err.println("Read line: " + line);
while ((reader.readLine(null, null, mask, null)) != null) {
try {
while (true) {
reader.readLine(null, null, mask, null);
}
} catch (EndOfFileException e) {
// noop
}
// while ((reader.readLine(null, null, mask, null)) != null) {
// noop
// }

assertEquals(expected, reader.getBuffer().toString());
}
Expand Down Expand Up @@ -137,13 +145,16 @@ protected void assertLine(final String expected, final TestBuffer buffer,

in.setIn(new ByteArrayInputStream(buffer.getBytes()));

String line;
String prevLine = null;
while ((line = reader.readLine(null, null, mask, null)) != null) {
prevLine = line;
String line = null;
try {
while (true) {
line = reader.readLine(null, null, mask, null);
}
} catch (EndOfFileException e) {
// ignore
}

assertEquals(expected, prevLine);
assertEquals(expected, line);
}

private String getKeyForAction(final String key) {
Expand Down

0 comments on commit 69471c7

Please sign in to comment.