| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import itertools |
| 14 |
import unittest |
| 15 |
import sys |
| 16 |
|
| 17 |
from autopage.tests import isolation |
| 18 |
|
| 19 |
import typing |
| 20 |
|
| 21 |
import autopage |
| 22 |
|
| 23 |
|
| 24 |
MAX_LINES_PER_PAGE = isolation.LINES - 1 |
| 25 |
|
| 26 |
|
| 27 |
class finite: |
| 28 |
def __init__(self, num_lines: int, **kwargs: typing.Any): |
| 29 |
self.num_lines = num_lines |
| 30 |
self.kwargs = kwargs |
| 31 |
|
| 32 |
def __call__(self) -> int: |
| 33 |
ap = autopage.AutoPager(pager_command=autopage.command.Less(), |
| 34 |
**self.kwargs) |
| 35 |
with ap as out: |
| 36 |
for i in range(self.num_lines): |
| 37 |
print(i, file=out) |
| 38 |
return ap.exit_code() |
| 39 |
|
| 40 |
|
| 41 |
def infinite() -> int: |
| 42 |
ap = autopage.AutoPager(pager_command=autopage.command.Less()) |
| 43 |
try: |
| 44 |
with ap as out: |
| 45 |
for i in itertools.count(): |
| 46 |
print(i, file=out) |
| 47 |
except KeyboardInterrupt: |
| 48 |
pass |
| 49 |
return ap.exit_code() |
| 50 |
|
| 51 |
|
| 52 |
def from_stdin() -> int: |
| 53 |
ap = autopage.AutoPager(pager_command=autopage.command.Less(), |
| 54 |
line_buffering=autopage.line_buffer_from_input()) |
| 55 |
with ap as out: |
| 56 |
try: |
| 57 |
for line in sys.stdin: |
| 58 |
print(line.rstrip(), file=out) |
| 59 |
except KeyboardInterrupt: |
| 60 |
pass |
| 61 |
return ap.exit_code() |
| 62 |
|
| 63 |
|
| 64 |
def with_exception() -> int: |
| 65 |
class MyException(Exception): |
| 66 |
pass |
| 67 |
|
| 68 |
ap = autopage.AutoPager(pager_command=autopage.command.Less()) |
| 69 |
try: |
| 70 |
with ap as out: |
| 71 |
for i in range(50): |
| 72 |
print(i, file=out) |
| 73 |
raise MyException() |
| 74 |
except MyException: |
| 75 |
pass |
| 76 |
return ap.exit_code() |
| 77 |
|
| 78 |
|
| 79 |
def with_stderr_output() -> int: |
| 80 |
ap = autopage.AutoPager(pager_command=autopage.command.Less()) |
| 81 |
with ap as out: |
| 82 |
for i in range(50): |
| 83 |
print(i, file=out) |
| 84 |
print("Hello world", file=sys.stderr) |
| 85 |
return ap.exit_code() |
| 86 |
|
| 87 |
|
| 88 |
class InvokePagerTest(unittest.TestCase): |
| 89 |
def test_page_to_end(self) -> None: |
| 90 |
num_lines = 100 |
| 91 |
with isolation.isolate(finite(num_lines)) as env: |
| 92 |
pager = isolation.PagerControl(env) |
| 93 |
|
| 94 |
lines = num_lines |
| 95 |
while lines > 0: |
| 96 |
expected = min(lines, MAX_LINES_PER_PAGE) |
| 97 |
self.assertEqual(expected, pager.advance()) |
| 98 |
lines -= expected |
| 99 |
self.assertEqual(0, pager.advance()) |
| 100 |
self.assertEqual(0, pager.advance()) |
| 101 |
self.assertEqual(0, pager.quit()) |
| 102 |
|
| 103 |
self.assertEqual(num_lines, pager.total_lines()) |
| 104 |
self.assertFalse(env.error_output()) |
| 105 |
self.assertEqual(0, env.exit_code()) |
| 106 |
|
| 107 |
def test_page_to_middle(self) -> None: |
| 108 |
num_lines = 100 |
| 109 |
with isolation.isolate(finite(num_lines)) as env: |
| 110 |
pager = isolation.PagerControl(env) |
| 111 |
|
| 112 |
self.assertEqual(MAX_LINES_PER_PAGE, pager.advance()) |
| 113 |
self.assertEqual(MAX_LINES_PER_PAGE, pager.advance()) |
| 114 |
self.assertEqual(MAX_LINES_PER_PAGE, pager.quit()) |
| 115 |
self.assertFalse(env.error_output()) |
| 116 |
self.assertEqual(0, env.exit_code()) |
| 117 |
|
| 118 |
def test_exit_pager_early(self) -> None: |
| 119 |
with isolation.isolate(infinite) as env: |
| 120 |
pager = isolation.PagerControl(env) |
| 121 |
|
| 122 |
self.assertEqual(MAX_LINES_PER_PAGE, pager.advance()) |
| 123 |
self.assertEqual(MAX_LINES_PER_PAGE, pager.quit()) |
| 124 |
self.assertFalse(env.error_output()) |
| 125 |
self.assertEqual(141, env.exit_code()) |
| 126 |
|
| 127 |
def test_interrupt_early(self) -> None: |
| 128 |
with isolation.isolate(infinite) as env: |
| 129 |
pager = isolation.PagerControl(env) |
| 130 |
|
| 131 |
self.assertEqual(MAX_LINES_PER_PAGE, pager.advance()) |
| 132 |
env.interrupt() |
| 133 |
while pager.advance(): |
| 134 |
continue |
| 135 |
pager.quit() |
| 136 |
self.assertGreater(pager.total_lines(), MAX_LINES_PER_PAGE) |
| 137 |
self.assertFalse(env.error_output()) |
| 138 |
self.assertEqual(130, env.exit_code()) |
| 139 |
|
| 140 |
def test_interrupt_early_quit(self) -> None: |
| 141 |
with isolation.isolate(infinite) as env: |
| 142 |
pager = isolation.PagerControl(env) |
| 143 |
|
| 144 |
self.assertEqual(MAX_LINES_PER_PAGE, pager.advance()) |
| 145 |
env.interrupt() |
| 146 |
pager.quit() |
| 147 |
self.assertGreater(pager.total_lines(), MAX_LINES_PER_PAGE) |
| 148 |
self.assertFalse(env.error_output()) |
| 149 |
self.assertEqual(130, env.exit_code()) |
| 150 |
|
| 151 |
def test_interrupt_in_middle_after_complete(self) -> None: |
| 152 |
num_lines = 100 |
| 153 |
with isolation.isolate(finite(num_lines)) as env: |
| 154 |
pager = isolation.PagerControl(env) |
| 155 |
|
| 156 |
self.assertEqual(MAX_LINES_PER_PAGE, pager.advance()) |
| 157 |
|
| 158 |
for i in range(100): |
| 159 |
env.interrupt() |
| 160 |
|
| 161 |
self.assertEqual(MAX_LINES_PER_PAGE, pager.quit()) |
| 162 |
self.assertFalse(env.error_output()) |
| 163 |
self.assertEqual(0, env.exit_code()) |
| 164 |
|
| 165 |
def test_interrupt_at_end_after_complete(self) -> None: |
| 166 |
num_lines = 100 |
| 167 |
with isolation.isolate(finite(num_lines)) as env: |
| 168 |
pager = isolation.PagerControl(env) |
| 169 |
|
| 170 |
while pager.advance(): |
| 171 |
continue |
| 172 |
|
| 173 |
self.assertEqual(num_lines, pager.total_lines()) |
| 174 |
|
| 175 |
for i in range(100): |
| 176 |
env.interrupt() |
| 177 |
|
| 178 |
self.assertEqual(0, pager.quit()) |
| 179 |
self.assertFalse(env.error_output()) |
| 180 |
self.assertEqual(0, env.exit_code()) |
| 181 |
|
| 182 |
def test_short_output(self) -> None: |
| 183 |
num_lines = 10 |
| 184 |
with isolation.isolate(finite(num_lines)) as env: |
| 185 |
pager = isolation.PagerControl(env) |
| 186 |
|
| 187 |
for i, l in enumerate(pager.read_lines(num_lines)): |
| 188 |
self.assertEqual(str(i), l.rstrip()) |
| 189 |
self.assertFalse(env.error_output()) |
| 190 |
self.assertEqual(0, env.exit_code()) |
| 191 |
|
| 192 |
def test_short_output_reset(self) -> None: |
| 193 |
num_lines = 10 |
| 194 |
with isolation.isolate(finite(num_lines, reset_on_exit=True)) as env: |
| 195 |
pager = isolation.PagerControl(env) |
| 196 |
|
| 197 |
self.assertEqual(num_lines, pager.quit()) |
| 198 |
self.assertFalse(env.error_output()) |
| 199 |
self.assertEqual(0, env.exit_code()) |
| 200 |
|
| 201 |
def test_short_streaming_output(self) -> None: |
| 202 |
num_lines = 10 |
| 203 |
with isolation.isolate(from_stdin, stdin_pipe=True) as env: |
| 204 |
pager = isolation.PagerControl(env) |
| 205 |
|
| 206 |
with env.stdin_pipe() as in_pipe: |
| 207 |
for i in range(num_lines): |
| 208 |
print(i, file=in_pipe) |
| 209 |
|
| 210 |
for i, l in enumerate(pager.read_lines(num_lines)): |
| 211 |
self.assertEqual(i, int(l)) |
| 212 |
|
| 213 |
env.interrupt() |
| 214 |
self.assertEqual(0, pager.quit()) |
| 215 |
self.assertFalse(env.error_output()) |
| 216 |
self.assertEqual(0, env.exit_code()) |
| 217 |
|
| 218 |
def test_exception(self) -> None: |
| 219 |
num_lines = 50 |
| 220 |
with isolation.isolate(with_exception) as env: |
| 221 |
pager = isolation.PagerControl(env) |
| 222 |
|
| 223 |
lines = num_lines |
| 224 |
while lines > 0: |
| 225 |
expected = min(lines, MAX_LINES_PER_PAGE) |
| 226 |
self.assertEqual(expected, pager.advance()) |
| 227 |
lines -= expected |
| 228 |
self.assertEqual(0, pager.advance()) |
| 229 |
self.assertEqual(0, pager.advance()) |
| 230 |
self.assertEqual(0, pager.quit()) |
| 231 |
|
| 232 |
self.assertEqual(num_lines, pager.total_lines()) |
| 233 |
self.assertFalse(env.error_output()) |
| 234 |
self.assertEqual(1, env.exit_code()) |
| 235 |
|
| 236 |
def test_stderr_output(self) -> None: |
| 237 |
num_lines = 50 |
| 238 |
with isolation.isolate(with_stderr_output) as env: |
| 239 |
pager = isolation.PagerControl(env) |
| 240 |
|
| 241 |
lines = num_lines |
| 242 |
while lines > 0: |
| 243 |
expected = min(lines, MAX_LINES_PER_PAGE) |
| 244 |
self.assertEqual(expected, pager.advance()) |
| 245 |
lines -= expected |
| 246 |
self.assertEqual(0, pager.advance()) |
| 247 |
self.assertEqual(0, pager.advance()) |
| 248 |
self.assertEqual(0, pager.quit()) |
| 249 |
|
| 250 |
self.assertEqual(num_lines, pager.total_lines()) |
| 251 |
self.assertEqual('Hello world\n', env.error_output()) |
| 252 |
self.assertEqual(0, env.exit_code()) |
| 253 |
|
| 254 |
|
| 255 |
class NoPagerTest(unittest.TestCase): |
| 256 |
def test_pipe_output_to_end(self) -> None: |
| 257 |
num_lines = 100 |
| 258 |
with isolation.isolate(finite(num_lines), |
| 259 |
stdout_pipe=True) as env: |
| 260 |
with env.stdout_pipe() as out: |
| 261 |
output = out.readlines() |
| 262 |
|
| 263 |
self.assertEqual(num_lines, len(output)) |
| 264 |
self.assertFalse(env.error_output()) |
| 265 |
self.assertEqual(0, env.exit_code()) |
| 266 |
|
| 267 |
def test_exit_early(self) -> None: |
| 268 |
with isolation.isolate(infinite, stdout_pipe=True) as env: |
| 269 |
with env.stdout_pipe() as out: |
| 270 |
for i in range(500): |
| 271 |
out.readline() |
| 272 |
self.assertFalse(env.error_output()) |
| 273 |
self.assertEqual(141, env.exit_code()) |
| 274 |
|
| 275 |
def test_exit_early_buffered(self) -> None: |
| 276 |
num_lines = 10 |
| 277 |
with isolation.isolate(from_stdin, |
| 278 |
stdin_pipe=True, stdout_pipe=True) as env: |
| 279 |
with env.stdin_pipe() as in_pipe: |
| 280 |
for i in range(num_lines): |
| 281 |
print(i, file=in_pipe) |
| 282 |
with env.stdout_pipe(): |
| 283 |
|
| 284 |
pass |
| 285 |
self.assertFalse(env.error_output()) |
| 286 |
self.assertEqual(141, env.exit_code()) |
| 287 |
|
| 288 |
def test_interrupt_early(self) -> None: |
| 289 |
with isolation.isolate(infinite, stdout_pipe=True) as env: |
| 290 |
env.interrupt() |
| 291 |
with env.stdout_pipe() as out: |
| 292 |
output = out.readlines() |
| 293 |
self.assertGreater(len(output), 0) |
| 294 |
self.assertFalse(env.error_output()) |
| 295 |
self.assertEqual(130, env.exit_code()) |
| 296 |
|
| 297 |
def test_short_streaming_output(self) -> None: |
| 298 |
num_lines = 10 |
| 299 |
with isolation.isolate(from_stdin, |
| 300 |
stdin_pipe=True, stdout_pipe=True) as env: |
| 301 |
with env.stdin_pipe() as in_pipe: |
| 302 |
for i in range(num_lines): |
| 303 |
print(i, file=in_pipe) |
| 304 |
|
| 305 |
with env.stdout_pipe() as out: |
| 306 |
for i in range(num_lines): |
| 307 |
self.assertEqual(i, int(out.readline())) |
| 308 |
env.interrupt() |
| 309 |
self.assertEqual(0, len(out.readlines())) |
| 310 |
|
| 311 |
self.assertFalse(env.error_output()) |
| 312 |
self.assertEqual(0, env.exit_code()) |