File: test_ansi2html.py

package info (click to toggle)
python-ansi2html 1.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 372 kB
  • sloc: python: 1,361; makefile: 3
file content (502 lines) | stat: -rw-r--r-- 19,918 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#  This file is part of ansi2html
#  Convert ANSI (terminal) colours and attributes to HTML
#  Copyright (C) 2012  Ralph Bean <rbean@redhat.com>
#  Copyright (C) 2012  Kuno Woudt <kuno@frob.nl>
#  Copyright (C) 2012  Martin Zimmermann <info@posativ.org>
#  Copyright (C) 2013  Sebastian Pipping <sebastian@pipping.org>
#
#  Inspired by and developed off of the work by pixelbeat and blackjack.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 3 of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import sys
import textwrap
from io import StringIO
from os.path import abspath, dirname, join
from subprocess import PIPE, Popen, run
from typing import List
from unittest.mock import patch

from ansi2html import Ansi2HTMLConverter
from ansi2html.converter import (
    ANSI_BLINK_FAST,
    ANSI_BLINK_OFF,
    ANSI_BLINK_SLOW,
    ANSI_INTENSITY_INCREASED,
    ANSI_INTENSITY_NORMAL,
    ANSI_INTENSITY_REDUCED,
    ANSI_NEGATIVE_OFF,
    ANSI_NEGATIVE_ON,
    ANSI_VISIBILITY_OFF,
    ANSI_VISIBILITY_ON,
    main,
)
from ansi2html.util import read_to_unicode

_here = dirname(abspath(__file__))


class TestAnsi2HTML:
    maxDiff = None

    def test_linkify(self) -> None:
        ansi = "http://threebean.org#foobar"
        target = '<a href="http://threebean.org#foobar">http://threebean.org#foobar</a>'
        html = Ansi2HTMLConverter(linkify=True).convert(ansi)
        assert target in html

    def test_not_linkify(self) -> None:
        ansi = "http://threebean.org"
        target = '<a href="http://threebean.org">http://threebean.org</a>'
        html = Ansi2HTMLConverter().convert(ansi)
        assert target not in html

    def test_osc_link(self) -> None:
        ansi = "[\x1b[01;35m\x1b[K\x1b]8;;https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wtype-limits\x07-Wtype-limits\x1b]8;;\x07\x1b[m\x1b[K]\n"
        target = '[<span class="ansi1 ansi35"><a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wtype-limits">-Wtype-limits</a></span>]'
        html = Ansi2HTMLConverter().convert(ansi)
        assert target in html

    def test_osc_link_latex(self) -> None:
        ansi = "[\x1b[01;35m\x1b[K\x1b]8;;https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wtype-limits\x07-Wtype-limits\x1b]8;;\x07\x1b[m\x1b[K]\n"
        target = "[\\textcolor{ansi1 ansi35}{\\href{https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wtype-limits}{-Wtype-limits}}]"
        html = Ansi2HTMLConverter(latex=True).convert(ansi)
        assert target in html

    def test_conversion(self) -> None:
        for input_filename, expected_output_filename in (
            ("ansicolor.txt", "ansicolor.html"),
            ("ansicolor_eix.txt", "ansicolor_eix.html"),
        ):
            with open(join(_here, input_filename), "rb") as input:
                test_data = "".join(read_to_unicode(input))

            with open(join(_here, expected_output_filename), "rb") as output:
                expected_data = [e.rstrip("\n") for e in read_to_unicode(output)]

            html = (
                Ansi2HTMLConverter()
                .convert(test_data, ensure_trailing_newline=True)
                .split("\n")
            )
            if html and html[-1] == "":
                html = html[:-1]

            assert "\n".join(html) == "\n".join(expected_data)

    @patch("sys.argv", new_callable=lambda: ["ansi2html"])
    @patch("sys.stdout", new_callable=StringIO)
    def test_conversion_as_command(
        self, mock_stdout: StringIO, mock_argv: List[str]
    ) -> None:
        with open(join(_here, "ansicolor.txt"), "rb") as input:
            test_data = "".join(read_to_unicode(input))

        with open(join(_here, "ansicolor.html"), "rb") as output:
            expected_data = "".join(read_to_unicode(output))

        def f() -> StringIO:
            return StringIO(test_data)

        with patch("sys.stdin", new_callable=f):
            main()

        html = mock_stdout.getvalue()

        assert html == expected_data

    def test_unicode(self) -> None:
        """Ensure that the converter returns unicode(py2)/str(py3) objs."""

        with open(join(_here, "ansicolor.txt"), "rb") as input:
            test_data = "".join(read_to_unicode(input))

        html = Ansi2HTMLConverter().convert(test_data).split("\n")

        for chunk in html:
            assert isinstance(chunk, str)

    @patch("sys.argv", new_callable=lambda: ["ansi2html", "--inline"])
    @patch("sys.stdout", new_callable=StringIO)
    def test_inline_as_command(
        self, mock_stdout: StringIO, mock_argv: List[str]
    ) -> None:
        test_input = textwrap.dedent(
            """
        this is
        a test
        """
        )

        with patch("sys.stdin", new_callable=lambda: StringIO(test_input)):
            main()

        ms_val = mock_stdout.getvalue()
        assert ms_val == test_input, "%r != %r" % (ms_val, test_input)

    @patch("sys.argv", new_callable=lambda: ["ansi2html", "--partial"])
    @patch("sys.stdout", new_callable=StringIO)
    def test_partial_as_command(
        self, mock_stdout: StringIO, mock_argv: List[str]
    ) -> None:
        rainbow = "\x1b[1m\x1b[40m\x1b[31mr\x1b[32ma\x1b[33mi\x1b[34mn\x1b[35mb\x1b[36mo\x1b[37mw\x1b[0m\n"
        with patch("sys.stdin", new_callable=lambda: StringIO(rainbow)):
            main()

        html = mock_stdout.getvalue().strip()

        expected = (
            '<span class="ansi1"></span>'
            + '<span class="ansi1 ansi40"></span>'
            + '<span class="ansi1 ansi31 ansi40">r</span>'
            + '<span class="ansi1 ansi32 ansi40">a</span>'
            + '<span class="ansi1 ansi33 ansi40">i</span>'
            + '<span class="ansi1 ansi34 ansi40">n</span>'
            + '<span class="ansi1 ansi35 ansi40">b</span>'
            + '<span class="ansi1 ansi36 ansi40">o</span>'
            + '<span class="ansi1 ansi37 ansi40">w</span>'
        )
        assert isinstance(html, str)
        assert isinstance(expected, str)
        assert expected == html

    @patch("sys.argv", new_callable=lambda: ["ansi2html", "--headers"])
    @patch("sys.stdout", new_callable=StringIO)
    def test_headers_as_command(
        self, mock_stdout: StringIO, mock_argv: List[str]
    ) -> None:
        main()

        with open(join(_here, "produce_headers.txt"), "rb") as produce_headers:
            expected = read_to_unicode(produce_headers)

        html = mock_stdout.getvalue()

        assert isinstance(html, str)
        assert html == "".join(expected)

    def test_partial(self) -> None:
        rainbow = "\x1b[1m\x1b[40m\x1b[31mr\x1b[32ma\x1b[33mi\x1b[34mn\x1b[35mb\x1b[36mo\x1b[37mw\x1b[0m\n"

        html = Ansi2HTMLConverter().convert(rainbow, full=False).strip()
        expected = (
            '<span class="ansi1"></span>'
            + '<span class="ansi1 ansi40"></span>'
            + '<span class="ansi1 ansi31 ansi40">r</span>'
            + '<span class="ansi1 ansi32 ansi40">a</span>'
            + '<span class="ansi1 ansi33 ansi40">i</span>'
            + '<span class="ansi1 ansi34 ansi40">n</span>'
            + '<span class="ansi1 ansi35 ansi40">b</span>'
            + '<span class="ansi1 ansi36 ansi40">o</span>'
            + '<span class="ansi1 ansi37 ansi40">w</span>'
        )
        assert expected == html

    def test_inline(self) -> None:
        rainbow = "\x1b[1m\x1b[40m\x1b[31mr\x1b[32ma\x1b[33mi\x1b[34mn\x1b[35mb\x1b[36mo\x1b[37mw\x1b[0m"

        html = Ansi2HTMLConverter(inline=True).convert(rainbow, full=False)
        expected = (
            '<span style="font-weight: bold"></span>'
            + '<span style="font-weight: bold; background-color: #000316"></span>'
            + '<span style="font-weight: bold; color: #aa0000; background-color: #000316">r</span>'
            + '<span style="font-weight: bold; color: #00aa00; background-color: #000316">a</span>'
            + '<span style="font-weight: bold; color: #aa5500; background-color: #000316">i</span>'
            + '<span style="font-weight: bold; color: #0000aa; background-color: #000316">n</span>'
            + '<span style="font-weight: bold; color: #E850A8; background-color: #000316">b</span>'
            + '<span style="font-weight: bold; color: #00aaaa; background-color: #000316">o</span>'
            + '<span style="font-weight: bold; color: #F5F1DE; background-color: #000316">w</span>'
        )

        assert expected == html

    def test_produce_headers(self) -> None:
        conv = Ansi2HTMLConverter()
        headers = conv.produce_headers()

        inputfile = join(_here, "produce_headers.txt")
        with open(inputfile, "rb") as produce_headers:
            expected_data = read_to_unicode(produce_headers)

        assert headers == "".join(expected_data)

    def test_escaped_implicit(self) -> None:
        test = "<p>awesome</p>"
        expected = "&lt;p&gt;awesome&lt;/p&gt;"
        html = Ansi2HTMLConverter().convert(test, full=False)
        assert expected == html

    def test_escaped_explicit(self) -> None:
        test = "<p>awesome</p>"
        expected = "&lt;p&gt;awesome&lt;/p&gt;"
        html = Ansi2HTMLConverter(escaped=True).convert(test, full=False)
        assert expected == html

    def test_unescaped(self) -> None:
        test = "<p>awesome</p>"
        expected = "<p>awesome</p>"
        html = Ansi2HTMLConverter(escaped=False).convert(test, full=False)
        assert expected == html

    def test_markup_lines(self) -> None:
        test = "  wat  \n "
        expected = '<span id="line-0">  wat  </span>\n<span id="line-1"> </span>'
        html = Ansi2HTMLConverter(markup_lines=True).convert(test, full=False)
        assert expected == html

    def test_no_markup_lines(self) -> None:
        test = "  wat  \n "
        expected = test
        html = Ansi2HTMLConverter().convert(test, full=False)
        assert expected == html

    def test_issue_25(self) -> None:
        sample = "\x1b[0;38;5;238;48;5;231mTEXT\x1b[0m"

        html = Ansi2HTMLConverter(inline=False).convert(sample, full=False)
        expected = '<span class="ansi38-238 ansi48-231">TEXT</span>'

        assert expected == html

    def test_italic(self) -> None:
        sample = "\x1b[3mITALIC\x1b[0m"

        html = Ansi2HTMLConverter(inline=True).convert(sample, full=False)
        expected = '<span style="font-style: italic">ITALIC</span>'

        assert expected == html

    def test_hidden_text(self) -> None:
        sample = "\x1b[%dmHIDDEN\x1b[%dmVISIBLE\x1b[0m" % (
            ANSI_VISIBILITY_OFF,
            ANSI_VISIBILITY_ON,
        )

        html = Ansi2HTMLConverter(inline=True).convert(sample, full=False)
        expected = '<span style="visibility: hidden">HIDDEN</span>VISIBLE'

        assert expected == html

    def test_lighter_text(self) -> None:
        sample = "NORMAL\x1b[%dmLIGHTER\x1b[%dmBOLD\x1b[%dmNORMAL" % (
            ANSI_INTENSITY_REDUCED,
            ANSI_INTENSITY_INCREASED,
            ANSI_INTENSITY_NORMAL,
        )

        html = Ansi2HTMLConverter(inline=True).convert(sample, full=False)
        expected = 'NORMAL<span style="font-weight: lighter">LIGHTER</span><span style="font-weight: bold">BOLD</span>NORMAL'

        assert expected == html

    def test_blinking_text(self) -> None:
        sample = "\x1b[%dm555\x1b[%dm666\x1b[%dmNOBLINK\x1b[0m" % (
            ANSI_BLINK_SLOW,
            ANSI_BLINK_FAST,
            ANSI_BLINK_OFF,
        )

        html = Ansi2HTMLConverter(inline=True).convert(sample, full=False)
        expected = '<span style="text-decoration: blink">555</span><span style="text-decoration: blink">666</span>NOBLINK'
        assert expected == html

        html = Ansi2HTMLConverter(inline=False).convert(sample, full=False)
        expected = '<span class="ansi5">555</span><span class="ansi6">666</span>NOBLINK'
        assert expected == html

    def test_inverse_text(self) -> None:
        sample = "NORMAL\x1b[%dmINVERSE\x1b[%dmNORMAL\x1b[0m" % (
            ANSI_NEGATIVE_ON,
            ANSI_NEGATIVE_OFF,
        )
        html = Ansi2HTMLConverter(inline=False).convert(sample, full=False)
        expected = (
            'NORMAL<span class="inv_background inv_foreground">INVERSE</span>NORMAL'
        )
        assert expected == html

        sample = "NORMAL\x1b[%dm303030\x1b[%dm!30!30!30\x1b[%dm303030\x1b[0m" % (
            30,
            ANSI_NEGATIVE_ON,
            ANSI_NEGATIVE_OFF,
        )
        html = Ansi2HTMLConverter(inline=False).convert(sample, full=False)
        expected = 'NORMAL<span class="ansi30">303030</span><span class="inv30 inv_foreground">!30!30!30</span><span class="ansi30">303030</span>'
        assert expected == html

        sample = (
            "NORMAL\x1b[%dm313131\x1b[%dm!31!31!31\x1b[%dm!31!43\x1b[%dm31+43\x1b[0mNORMAL"
            % (31, ANSI_NEGATIVE_ON, 43, ANSI_NEGATIVE_OFF)
        )
        html = Ansi2HTMLConverter(inline=False).convert(sample, full=False)
        expected = 'NORMAL<span class="ansi31">313131</span><span class="inv31 inv_foreground">!31!31!31</span><span class="inv31 inv43">!31!43</span><span class="ansi31 ansi43">31+43</span>NORMAL'
        assert expected == html

    def test_cross_line_state(self) -> None:  # covers issue 36, too
        sample = "\x1b[31mRED\nSTILL RED"
        html = Ansi2HTMLConverter(inline=True).convert(
            sample, full=False, ensure_trailing_newline=False
        )
        expected = '<span style="color: #aa0000">RED\nSTILL RED</span>'
        assert expected == html

        sample = "\x1b[31mRED\nSTILL RED\n"
        html = Ansi2HTMLConverter(inline=True).convert(
            sample, full=False, ensure_trailing_newline=False
        )
        expected = '<span style="color: #aa0000">RED\nSTILL RED\n</span>'
        assert expected == html

        sample = "\x1b[31mRED\nSTILL RED"
        html = Ansi2HTMLConverter(inline=True).convert(
            sample, full=False, ensure_trailing_newline=True
        )
        expected = '<span style="color: #aa0000">RED\nSTILL RED</span>\n'
        assert expected == html

        sample = "\x1b[31mRED\nSTILL RED\n"
        html = Ansi2HTMLConverter(inline=True).convert(
            sample, full=False, ensure_trailing_newline=True
        )
        expected = '<span style="color: #aa0000">RED\nSTILL RED\n</span>\n'
        assert expected == html

        sample = "\x1b[31mRED\nSTILL RED\x1b[m\n"
        html = Ansi2HTMLConverter(inline=True).convert(
            sample, full=False, ensure_trailing_newline=True
        )
        expected = '<span style="color: #aa0000">RED\nSTILL RED</span>\n'
        assert expected == html

    def test_scheme(self) -> None:  # covers issue 36, too
        sample = "\x1b[33mYELLOW/BROWN"
        # ansi2html scheme is brown #aa5500
        html = Ansi2HTMLConverter(inline=True).convert(
            sample, full=False, ensure_trailing_newline=False
        )
        expected = '<span style="color: #aa5500">YELLOW/BROWN</span>'
        assert expected == html

        # xterm scheme is yellow #cdcd00
        html = Ansi2HTMLConverter(inline=True, scheme="xterm").convert(
            sample, full=False, ensure_trailing_newline=False
        )
        expected = '<span style="color: #cdcd00">YELLOW/BROWN</span>'
        assert expected == html

    def test_latex_inline(self) -> None:
        ansi = "\x1b[33mYELLOW/BROWN"
        target = "\\textcolor[HTML]{aa5500}{YELLOW/BROWN}"
        latex = Ansi2HTMLConverter(latex=True, inline=True).convert(ansi)
        assert target in latex

    def test_latex_title(self) -> None:
        ansi = ""
        title = "testing"
        target = "\\title{%s}" % title
        latex = Ansi2HTMLConverter(latex=True, inline=True, title=title).convert(ansi)
        assert target in latex

    def test_latex_linkify(self) -> None:
        ansi = "http://python.org/"
        target = "\\url{%s}" % ansi
        latex = Ansi2HTMLConverter(latex=True, inline=True, linkify=True).convert(ansi)
        assert target in latex

    def test_truecolor(self) -> None:
        ansi = (
            "\u001b[38;2;255;102;102m 1 \u001b[0m "
            + "\u001b[38;2;0;0;255m 2 \u001b[0m "
            + "\u001b[48;2;65;105;225m 3 \u001b[0m"
        )
        target = (
            '<span class="ansi38-255102102"> 1 </span> '
            + '<span class="ansi38-000000255"> 2 </span> '
            + '<span class="ansi48-065105225"> 3 </span>'
        )
        html = Ansi2HTMLConverter().convert(ansi)
        assert target in html

    def test_truecolor_inline(self) -> None:
        ansi = (
            "\u001b[38;2;255;102;102m 1 \u001b[0m "
            + "\u001b[38;2;0;0;255m 2 \u001b[0m "
            + "\u001b[48;2;65;105;225m 3 \u001b[0m"
        )
        target = (
            '<span style="color: #FF6666"> 1 </span> '
            + '<span style="color: #0000FF"> 2 </span> '
            + '<span style="background-color: #4169E1"> 3 </span>'
        )
        html = Ansi2HTMLConverter(inline=True).convert(ansi)
        assert target in html

    def test_truecolor_malformed(self) -> None:
        ansi = "\u001b[38;2;255;102m malformed \u001b[0m "
        #                         ^ e.g. ";102" missed
        target = '<span class="ansi2 ansi102"> malformed </span> '
        html = Ansi2HTMLConverter().convert(ansi)
        assert target in html

    def test_256_color_malformed(self) -> None:
        ansi = "\u001b[38;5m malformed \u001b[0m "
        #                 ^ e.g. ";255" missed
        target = '<span class="ansi5"> malformed </span> '
        html = Ansi2HTMLConverter().convert(ansi)
        assert target in html

    def test_x_bit_color_malformed(self) -> None:
        ansi = "\u001b[38m malformed \u001b[0m "
        #               ^ e.g. ";5;255m" missed
        target = '<span class="ansi38"> malformed </span> '
        html = Ansi2HTMLConverter().convert(ansi)
        assert target in html

    def test_command_script(self) -> None:
        result = run(["ansi2html_py", "--version"], check=True)
        assert result.returncode == 0

    def test_command_input_output_encoding(self) -> None:
        input_encoding = "utf-16"
        input_bytes = "regular \033[31mred\033[0m regular".encode(input_encoding)
        output_encoding = "utf-32"
        output_bytes_expected = (
            'regular <span style="color: #aa0000">red</span> regular\n'.encode(
                output_encoding
            )
        )

        with Popen(
            [
                "ansi2html_py",
                "--inline",
                f"--input-encoding={input_encoding}",
                f"--output-encoding={output_encoding}",
            ],
            stdin=PIPE,
            stdout=PIPE,
        ) as process:
            assert process.stdin  # for mypy
            assert process.stdout  # for mypy
            process.stdin.write(input_bytes)
            process.stdin.close()
            stdout_bytes_actual = process.stdout.read()

        assert stdout_bytes_actual == output_bytes_expected
        assert process.returncode == 0

    def test_command_module(self) -> None:
        result = run([sys.executable, "-m", "ansi2html", "--version"], check=True)
        assert result.returncode == 0