Ejemplo n.º 1
0
    def tw(
        self, request, tmp_path: Path
    ) -> Generator[terminalwriter.TerminalWriter, None, None]:
        if request.param == "path":
            p = tmp_path.joinpath("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
Ejemplo n.º 2
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"
Ejemplo n.º 3
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"
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def test_attr_hasmarkup() -> None:
    file = io.StringIO()
    tw = terminalwriter.TerminalWriter(file, will_be_displayed=False)
    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
Ejemplo n.º 6
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
Ejemplo n.º 7
0
def test_code_highlight(has_markup, code_highlight, expected, color_mapping):
    f = io.StringIO()
    tw = terminalwriter.TerminalWriter(f)
    tw.hasmarkup = has_markup
    tw.code_highlight = code_highlight
    tw._write_source(["assert 0"], lexer='py')

    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"], [" ", " "])
Ejemplo n.º 8
0
def test_terminalwriter_computes_width(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setattr(terminalwriter, "get_terminal_width", lambda: 42)
    tw = terminalwriter.TerminalWriter()
    assert tw.fullwidth == 42
Ejemplo n.º 9
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
Ejemplo n.º 10
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
Ejemplo n.º 11
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
Ejemplo n.º 12
0
 def test_update_with_newline(self) -> None:
     tw = terminalwriter.TerminalWriter()
     tw.write("hello\nworld")
     assert tw.width_of_current_line == 5
Ejemplo n.º 13
0
 def test_init(self) -> None:
     tw = terminalwriter.TerminalWriter()
     assert tw.width_of_current_line == 0