def tw(
        self, request, tmpdir
    ) -> Generator[terminalwriter.TerminalWriter, None, None]:
        if request.param == "path":
            p = tmpdir.join("tmpfile")
            f = open(str(p), "w+", encoding="utf8")
            tw = terminalwriter.TerminalWriter(f)

            def getlines():
                f.flush()
                with open(str(p), encoding="utf8") as fp:
                    return fp.readlines()

        elif request.param == "stringio":
            f = io.StringIO()
            tw = terminalwriter.TerminalWriter(f)

            def getlines():
                f.seek(0)
                return f.readlines()

        tw.getlines = getlines  # type: ignore
        tw.getvalue = lambda: "".join(getlines())  # type: ignore

        with f:
            yield tw
Exemple #2
0
def test_terminalwriter_not_unicode() -> None:
    """If the file doesn't support Unicode, the string is unicode-escaped (#7475)."""
    buffer = io.BytesIO()
    file = io.TextIOWrapper(buffer, encoding="cp1252")
    tw = terminalwriter.TerminalWriter(file)
    tw.write("hello 🌀 wôrld אבג", flush=True)
    assert buffer.getvalue() == br"hello \U0001f300 w\xf4rld \u05d0\u05d1\u05d2"
Exemple #3
0
def assert_color_not_set():
    f = io.StringIO()
    f.isatty = lambda: True  # type: ignore
    tw = terminalwriter.TerminalWriter(file=f)
    assert not tw.hasmarkup
    tw.line("hello", bold=True)
    s = f.getvalue()
    assert s == "hello\n"
def test_should_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setitem(os.environ, "PY_COLORS", "0")
    f = io.StringIO()
    f.isatty = lambda: True  # type: ignore
    tw = terminalwriter.TerminalWriter(file=f)
    assert not tw.hasmarkup
    tw.line("hello", bold=True)
    s = f.getvalue()
    assert s == "hello\n"
Exemple #5
0
def assert_color_set():
    file = io.StringIO()
    tw = terminalwriter.TerminalWriter(file)
    assert tw.hasmarkup
    tw.line("hello", bold=True)
    s = file.getvalue()
    assert len(s) > len("hello\n")
    assert "\x1b[1m" in s
    assert "\x1b[0m" in s
def test_should_do_markup_PY_COLORS_eq_1(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setitem(os.environ, "PY_COLORS", "1")
    file = io.StringIO()
    tw = terminalwriter.TerminalWriter(file)
    assert tw.hasmarkup
    tw.line("hello", bold=True)
    s = file.getvalue()
    assert len(s) > len("hello\n")
    assert "\x1b[1m" in s
    assert "\x1b[0m" in s
def test_attr_hasmarkup() -> None:
    file = io.StringIO()
    tw = terminalwriter.TerminalWriter(file)
    assert not tw.hasmarkup
    tw.hasmarkup = True
    tw.line("hello", bold=True)
    s = file.getvalue()
    assert len(s) > len("hello\n")
    assert "\x1b[1m" in s
    assert "\x1b[0m" in s
Exemple #8
0
def test_code_highlight(has_markup, expected, color_mapping):
    f = io.StringIO()
    tw = terminalwriter.TerminalWriter(f)
    tw.hasmarkup = has_markup
    tw._write_source(["assert 0"])
    assert f.getvalue().splitlines(keepends=True) == color_mapping.format([expected])

    with pytest.raises(
        ValueError,
        match=re.escape("indents size (2) should have same size as lines (1)"),
    ):
        tw._write_source(["assert 0"], [" ", " "])
def test_terminalwriter_dumb_term_no_markup(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setattr(os, "environ", {"TERM": "dumb", "PATH": ""})

    class MyFile:
        closed = False

        def isatty(self):
            return True

    with monkeypatch.context() as m:
        m.setattr(sys, "stdout", MyFile())
        assert sys.stdout.isatty()
        tw = terminalwriter.TerminalWriter()
        assert not tw.hasmarkup
Exemple #10
0
def test_terminalwriter_computes_width(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setattr(terminalwriter, "get_terminal_width", lambda: 42)
    tw = terminalwriter.TerminalWriter()
    assert tw.fullwidth == 42
Exemple #11
0
def test_terminalwriter_width_bogus(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setattr(shutil, "get_terminal_size", mock.Mock(return_value=(10, 10)))
    monkeypatch.delenv("COLUMNS", raising=False)
    tw = terminalwriter.TerminalWriter()
    assert tw.fullwidth == 80
Exemple #12
0
 def test_combining(self) -> None:
     tw = terminalwriter.TerminalWriter()
     text = "café food"
     assert len(text) == 10
     tw.write(text)
     assert tw.width_of_current_line == 9
Exemple #13
0
 def test_composed(self) -> None:
     tw = terminalwriter.TerminalWriter()
     text = "café food"
     assert len(text) == 9
     tw.write(text)
     assert tw.width_of_current_line == 9
Exemple #14
0
 def test_update_with_wide_text(self) -> None:
     tw = terminalwriter.TerminalWriter()
     tw.write("乇乂ㄒ尺卂 ㄒ卄丨匚匚")
     assert tw.width_of_current_line == 21  # 5*2 + 1 + 5*2
Exemple #15
0
 def test_update_with_newline(self) -> None:
     tw = terminalwriter.TerminalWriter()
     tw.write("hello\nworld")
     assert tw.width_of_current_line == 5
Exemple #16
0
 def test_init(self) -> None:
     tw = terminalwriter.TerminalWriter()
     assert tw.width_of_current_line == 0