Skip to content

Commit b018d30

Browse files
authored
test: add LineReader EOF tests for different terminal providers (#1961) (#1968)
1 parent b3260e4 commit b018d30

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) the original author(s).
3+
*
4+
* This software is distributable under the BSD license. See the terms of the
5+
* BSD license in the documentation provided with this software.
6+
*
7+
* https://opensource.org/licenses/BSD-3-Clause
8+
*/
9+
package org.jline.reader.impl;
10+
11+
import java.io.ByteArrayOutputStream;
12+
import java.io.PipedInputStream;
13+
import java.io.PipedOutputStream;
14+
import java.nio.charset.StandardCharsets;
15+
16+
import org.jline.reader.EndOfFileException;
17+
import org.jline.reader.LineReader;
18+
import org.jline.reader.LineReaderBuilder;
19+
import org.jline.terminal.Terminal;
20+
import org.jline.terminal.TerminalBuilder;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.Timeout;
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
26+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
27+
28+
class LineReaderEofTest {
29+
30+
@Test
31+
@Timeout(10)
32+
void eofWithExecProvider() throws Exception {
33+
checkEof(TerminalBuilder.PROP_PROVIDER_EXEC);
34+
}
35+
36+
@Test
37+
@Timeout(10)
38+
void eofWithDefaultProvider() throws Exception {
39+
checkEof(null);
40+
}
41+
42+
private void checkEof(String providerType) throws Exception {
43+
String savedProviders = System.getProperty(TerminalBuilder.PROP_PROVIDERS);
44+
try {
45+
if (providerType != null) {
46+
System.setProperty(TerminalBuilder.PROP_PROVIDERS, providerType);
47+
}
48+
49+
PipedInputStream in = new PipedInputStream();
50+
PipedOutputStream outIn = new PipedOutputStream(in);
51+
outIn.write("hello\n".getBytes(StandardCharsets.UTF_8));
52+
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
53+
54+
Terminal terminal;
55+
try {
56+
terminal = TerminalBuilder.builder().streams(in, out).build();
57+
} catch (Exception e) {
58+
assumeTrue(false, "Provider '" + providerType + "' not available: " + e.getMessage());
59+
return;
60+
}
61+
62+
try (terminal) {
63+
LineReader lr = LineReaderBuilder.builder().terminal(terminal).build();
64+
assertEquals("hello", lr.readLine());
65+
outIn.close();
66+
Thread.sleep(200);
67+
assertThrows(EndOfFileException.class, () -> lr.readLine());
68+
}
69+
} finally {
70+
if (savedProviders != null) {
71+
System.setProperty(TerminalBuilder.PROP_PROVIDERS, savedProviders);
72+
} else {
73+
System.clearProperty(TerminalBuilder.PROP_PROVIDERS);
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)