1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
import os
import sys
import subprocess
import pytest
from backports.shutil_get_terminal_size import get_terminal_size
def test_does_not_crash():
"""Check if get_terminal_size() returns a meaningful value.
There's no easy portable way to actually check the size of the
terminal, so let's check if it returns something sensible instead.
"""
size = get_terminal_size()
assert size.columns >= 0
assert size.lines >= 0
def test_os_environ_first(monkeypatch):
"Check if environment variables have precedence"
monkeypatch.setenv("COLUMNS", 777)
monkeypatch.setenv("LINES", 888)
size = get_terminal_size()
assert size.columns == 777
assert size.lines == 888
@pytest.mark.skipif("not os.isatty(sys.__stdout__.fileno())", reason="not on tty")
def test_stty_match(monkeypatch):
"""Check if stty returns the same results ignoring env
This test will fail if stdin and stdout are connected to
different terminals with different sizes. Nevertheless, such
situations should be pretty rare.
"""
try:
process = subprocess.Popen(["stty", "size"], stdout=subprocess.PIPE)
output, err = process.communicate()
if process.poll() != 0:
raise OSError
size = output.decode().split()
except (OSError, subprocess.CalledProcessError):
pytest.skip("stty invocation failed")
expected = (int(size[1]), int(size[0])) # reversed order
monkeypatch.delenv("LINES", raising=False)
monkeypatch.delenv("COLUMNS", raising=False)
actual = get_terminal_size()
assert expected == actual
|