Debian Codegraph @

test/test_writers/test_manpage.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # Author: engelbert gruber <[email protected]>
4 # Copyright: This module has been placed in the public domain.
5
6 """
7 Tests for manpage writer.
8 """
9
10 from pathlib import Path
11 import sys
12 import unittest
13 from io import StringIO
14
15 if __name__ == '__main__':
16 # prepend the "docutils root" to the Python library path
17 # so we import the local `docutils` package.
18 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
19
20 from docutils.core import publish_string
21 from docutils.writers import manpage
22
23 URI_tests = (
24 ("///abc.de", r"///\:abc\:.de"),
25 ("/abc.de/", r"/\:abc\:.de/"),
26 ("http://abc.de", r"http://\:abc\:.de"),
27 ("http://abc.de/fg", r"http://\:abc\:.de/fg"),
28 ("http://abc.de/fg?q=abc", r"http://\:abc\:.de/\:fg?\:q=abc"),
29 ("http://abc.de/fg/?q=abc", r"http://\:abc\:.de/\:fg/?\:q=abc"),
30 ("http://abc.de/fg/?q=abc&me#", r"http://\:abc\:.de/\:fg/?\:q=abc&\:me#"),
31 ("[email protected]", r"me@\:home\:.here"),
32 ("[email protected]..", r"me\:..dot@\:home\:.here.."),
33 )
34
35
36 class URIBreakpointsTestCase(unittest.TestCase):
37
38 def test_insert(self):
39 for t in URI_tests:
40 got = manpage.insert_URI_breakpoints(t[0])
41 self.assertEqual(t[1], got)
42
43
44 class WriterPublishTestCase(unittest.TestCase):
45
46 maxDiff = None
47
48 def test_publish(self):
49 for name, cases in totest.items():
50 for casenum, (case_input, case_expected) in enumerate(cases):
51 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
52 output = publish_string(
53 source=case_input,
54 writer=manpage.Writer(),
55 settings_overrides={
56 '_disable_config': True,
57 'strict_visitor': True,
58 }).decode()
59 self.assertEqual(case_expected, output)
60
61 def test_reference_macros(self):
62 for name, cases in totest_refs.items():
63 for casenum, (case_input, case_expected) in enumerate(cases):
64 with self.subTest(id=f'totest_refs[{name!r}][{casenum}]'):
65 output = publish_string(
66 source=case_input,
67 writer=manpage.Writer(),
68 settings_overrides={
69 '_disable_config': True,
70 'strict_visitor': True,
71 'text_references': False,
72 'output_encoding': "unicode",
73 })
74 self.assertEqual(case_expected, output)
75
76 def test_system_msgs(self):
77 for name, cases in totest_system_msgs.items():
78 for casenum, (case_input, case_expected, case_warning) in enumerate(cases):
79 with self.subTest(id=f'totest_system_msgs[{name!r}][{casenum}]'):
80 warnings = StringIO("")
81 output = publish_string(
82 source=case_input,
83 writer=manpage.Writer(),
84 settings_overrides={
85 '_disable_config': True,
86 'strict_visitor': True,
87 'report_level': 1,
88 'warning_stream': warnings,
89 'output_encoding': "unicode",
90 })
91 self.assertEqual(case_expected, output)
92 warnings.seek(0)
93 self.assertEqual(
94 case_warning,
95 warnings.readlines())
96
97
98 document_start = r""".\" Man page generated from reStructuredText
99 .\" by the Docutils 0.23 manpage writer.
100 .
101 """
102
103 indend_macros = r""".
104 .nr rst2man-indent-level 0
105 .
106 .de1 rstReportMargin
107 \\$1 \\n[an-margin]
108 level \\n[rst2man-indent-level]
109 level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
110 -
111 \\n[rst2man-indent0]
112 \\n[rst2man-indent1]
113 \\n[rst2man-indent2]
114 ..
115 .de1 INDENT
116 .\" .rstReportMargin pre:
117 . RS \\$1
118 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
119 . nr rst2man-indent-level +1
120 .\" .rstReportMargin post:
121 ..
122 .de UNINDENT
123 . RE
124 .\" indent \\n[an-margin]
125 .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
126 .nr rst2man-indent-level -1
127 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
128 .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
129 ..
130 """
131
132 totest_refs = {}
133
134 totest_refs['ext hyperlink'] = [
135 [r"""External hyperlinks, like Python_.
136
137 .. _Python: https://www.python.org/
138 """,
139 document_start + indend_macros + """.TH "" "" "" ""
140 .SH Name
141 \\- \n\
142 External hyperlinks, like \\c\n\
143 .UR \\%https://\\:www\\:.python\\:.org/
144 Python
145 .UE \\c
146 \\&.
147 .\\" End of generated man page.
148 """],
149 ]
150
151 totest_refs['emb hyperlink'] = [
152 [r"""embedded External hyperlinks, like `Python
153 <https://www.python.org/>`_.
154 """,
155 document_start + indend_macros + """.TH "" "" "" ""
156 .SH Name
157 \\- \n\
158 embedded External hyperlinks, like \\c\n\
159 .UR \\%https://\\:www\\:.python\\:.org/
160 Python
161 .UE \\c
162 \\&.
163 .\\" End of generated man page.
164 """],
165 ]
166
167 totest_refs['email'] = [
168 [r"""`Write to me`_ with your questions.
169
170 .. _Write to me: [email protected]
171
172 whatever.""",
173 document_start + indend_macros + """.TH "" "" "" ""
174 .SH Name
175 \\- \n\
176 .MT \\%jdoe@\\:example\\:.com
177 Write to me
178 .ME \\c
179 with your questions.
180 .sp
181 whatever.
182 .\\" End of generated man page.
183 """],
184 ]
185
186 totest_refs['email trailings'] = [
187 [r"""email: ([email protected]).
188
189 no spaces before and after the email.""",
190 document_start + indend_macros + """.TH "" "" "" ""
191 .SH Name
192 \\- \n\
193 email: (\\c
194 .MT \\%eee@\\:mmm\\:.al
195 .ME \\c
196 ).
197 .sp
198 no spaces before and after the email.
199 .\\" End of generated man page.
200 """],
201 ]
202
203 totest = {}
204
205 totest['blank'] = [
206 ["",
207 document_start + indend_macros + """.TH "" "" "" ""
208 .SH Name
209 \\- \n\
210 .\\" End of generated man page.
211 """],
212 [r"""Hello, world.
213 =============
214
215 .. WARNING::
216 This broke docutils-sphinx.
217
218 """,
219 document_start + indend_macros +
220 """.TH "Hello, world." "" "" ""
221 .SH Name
222 Hello, world. \\- \n\
223 .sp
224 \\fBWarning:\\fP
225 .INDENT 0.0
226 .INDENT 3.5
227 This broke docutils\\-sphinx.
228 .UNINDENT
229 .UNINDENT
230 .\\" End of generated man page.
231 """],
232 ]
233
234 totest['simple'] = [
235 ["""\
236 ========
237 simple
238 ========
239
240 ---------------
241 The way to go
242 ---------------
243
244 :Author: [email protected]
245 :Date: 2009-08-05
246 :Copyright: public domain
247 :Version: 0.1
248 :Manual section: 1
249 :Manual group: text processing
250 :Arbitrary field: some text
251
252 SYNOPSIS
253 ========
254
255 ::
256
257 K.I.S.S keep it simple.
258
259 DESCRIPTION
260 ===========
261
262 General rule of life.
263
264 OPTIONS
265 =======
266
267 --config=<file> Read configuration settings from <file>, if it exists.
268 --version, -V Show this program's version number and exit.
269 --help, -h Show this help message and exit.
270
271 OtHeR SECTION
272 =============
273
274 link to http://docutils.sourceforge.io
275
276 With mixed case.
277
278 .. Attention::
279
280 Admonition with title
281
282 * bullet list
283 * bull and list
284
285 .. admonition:: homegrown
286
287 something important
288
289 . period at line start.
290
291 and . in a line and at line start
292 .in a paragraph
293 """,
294 document_start + indend_macros +
295 """\
296 .TH "simple" "1" "2009-08-05" "0.1" "text processing"
297 .SH Name
298 simple \\- The way to go
299 .SH SYNOPSIS
300 .INDENT 0.0
301 .INDENT 3.5
302 .sp
303 .EX
304 K.I.S.S keep it simple.
305 .EE
306 .UNINDENT
307 .UNINDENT
308 .SH DESCRIPTION
309 .sp
310 General rule of life.
311 .SH OPTIONS
312 .INDENT 0.0
313 .TP
314 .BI \\-\\-config\\fB= <file>
315 Read configuration settings from <file>, if it exists.
316 .TP
317 .B \\-\\-version\\fP,\\fB \\-V
318 Show this program\\(aqs version number and exit.
319 .TP
320 .B \\-\\-help\\fP,\\fB \\-h
321 Show this help message and exit.
322 .UNINDENT
323 .SH OtHeR SECTION
324 .sp
325 link to \\%<http://\\:docutils\\:.sourceforge\\:.io>\n\
326 .sp
327 With mixed case.
328 .sp
329 \\fBAttention!:\\fP
330 .INDENT 0.0
331 .INDENT 3.5
332 Admonition with title
333 .INDENT 0.0
334 .IP \\(bu 2
335 bullet list
336 .IP \\(bu 2
337 bull and list
338 .UNINDENT
339 .UNINDENT
340 .UNINDENT
341 .INDENT 0.0
342 .INDENT 3.5
343 .IP "homegrown"
344 .sp
345 something important
346 .UNINDENT
347 .UNINDENT
348 .sp
349 \\&. period at line start.
350 .sp
351 and . in a line and at line start
352 \\&.in a paragraph
353 .SH Author
354 [email protected]
355
356 Arbitrary field: some text
357 .SH Copyright
358 public domain
359 .\\" End of generated man page.
360 """],
361 ["""\
362 Internal hyperlinks_ and targets_ are ignored.
363
364 .. _hyperlinks:
365
366 (Text content of hyperlinks and _`targets` is printed as normal text.)
367 """,
368 f"""{document_start}{indend_macros}.TH "" "" "" ""
369 .SH Name
370 \\- \n\
371 Internal hyperlinks and targets are ignored.
372 .sp
373 (Text content of hyperlinks and targets is printed as normal text.)
374 .\\" End of generated man page.
375 """],
376
377 ]
378
379 totest['table'] = [
380 ["""\
381 ====== =====
382 head and
383 ====== =====
384 1 2
385 abc so
386 ====== =====
387 """,
388 '''\
389 \'\\" t
390 ''' + document_start + indend_macros + '''.TH "" "" "" ""
391 .SH Name
392 \\- \n\
393 .INDENT 0.0
394 .INDENT 3.5
395 .TS
396 box center;
397 l|l.
398 T{
399 head
400 T}\tT{
401 and
402 T}
403 _
404 T{
405 1
406 T}\tT{
407 2
408 T}
409 _
410 T{
411 abc
412 T}\tT{
413 so
414 T}
415 .TE
416 .UNINDENT
417 .UNINDENT
418 .\\" End of generated man page.
419 ''']
420 ]
421
422 totest['optiongroup'] = [
423 ["""
424 optin group with dot as group item
425
426 $
427 bla bla bla
428
429 #
430 bla bla bla
431
432 .
433 bla bla bla
434
435 [
436 bla bla bla
437
438 ]
439 bla bla bla
440 """,
441 document_start + indend_macros + """.TH "" "" "" ""
442 .SH Name
443 \\- \n\
444 optin group with dot as group item
445 .INDENT 0.0
446 .TP
447 .B $
448 bla bla bla
449 .UNINDENT
450 .INDENT 0.0
451 .TP
452 .B #
453 bla bla bla
454 .UNINDENT
455 .INDENT 0.0
456 .TP
457 .B \\&.
458 bla bla bla
459 .UNINDENT
460 .INDENT 0.0
461 .TP
462 .B [
463 bla bla bla
464 .UNINDENT
465 .INDENT 0.0
466 .TP
467 .B ]
468 bla bla bla
469 .UNINDENT
470 .\\" End of generated man page.
471 """],
472 ]
473
474 totest['definitionlist'] = [
475 ["""
476 ====================
477 Definition List Test
478 ====================
479
480 :Abstract: Docinfo is required.
481
482 Section
483 =======
484
485 :term1:
486
487 Description of Term 1 Description of Term 1 Description of Term 1
488 Description of Term 1 Description of Term 1
489
490 Description of Term 1 Description of Term 1 Description of Term 1
491 Description of Term 1 Description of Term 1
492
493 """,
494 document_start + indend_macros + '''.TH "Definition List Test" "" "" ""
495 .SH Name
496 Definition List Test \\- \n\
497 ''' + '''.SS Abstract
498 .sp
499 Docinfo is required.
500 .SH Section
501 .INDENT 0.0
502 .TP
503 .B term1
504 Description of Term 1 Description of Term 1 Description of Term 1
505 Description of Term 1 Description of Term 1
506 .sp
507 Description of Term 1 Description of Term 1 Description of Term 1
508 Description of Term 1 Description of Term 1
509 .UNINDENT
510 .\\" End of generated man page.
511 '''],
512 ]
513
514 totest['cmdlineoptions'] = [
515 ["""optional arguments:
516 -h, --help show this help
517 --output FILE, -o FILE output filename
518 -i DEVICE, --input DEVICE input device
519 """,
520 document_start + indend_macros + """.TH "" "" "" ""
521 .SH Name
522 \\- \n\
523 .INDENT 0.0
524 .TP
525 .B optional arguments:
526 .INDENT 7.0
527 .TP
528 .B \\-h\\fP,\\fB \\-\\-help
529 show this help
530 .TP
531 .BI \\-\\-output \\ FILE\\fR,\\fB \\ \\-o \\ FILE
532 output filename
533 .TP
534 .BI \\-i \\ DEVICE\\fR,\\fB \\ \\-\\-input \\ DEVICE
535 input device
536 .UNINDENT
537 .UNINDENT
538 .\\" End of generated man page.
539 """],
540 ]
541
542 totest['citation'] = [
543 [""".. [docutils] blah blah blah
544 .. [citation2] Another Source
545 """,
546 document_start + indend_macros + """.TH "" "" "" ""
547 .SH Name
548 \\- \n\
549 .IP [docutils] 5
550 blah blah blah
551 .IP [citation2] 5
552 Another Source
553 .\\" End of generated man page.
554 """],
555 ]
556
557 totest['rubric'] = [
558 [""".. rubric:: some rubric
559
560 - followed by
561 - a list
562 """,
563 document_start + indend_macros + """.TH "" "" "" ""
564 .SH Name
565 \\- \n\
566 some rubric
567 .INDENT 0.0
568 .IP \\(bu 2
569 followed by
570 .IP \\(bu 2
571 a list
572 .UNINDENT
573 .\\" End of generated man page.
574 """],
575 ]
576
577 totest['double_quote'] = [
578 ["""in "defintion list"
579 double quotes must be escaped on macro invocations.
580
581 They are "escaped" anywhere.
582 """,
583 document_start + indend_macros + """.TH "" "" "" ""
584 .SH Name
585 \\- \n\
586 .INDENT 0.0
587 .TP
588 .B in \\(dqdefintion list\\(dq
589 double quotes must be escaped on macro invocations.
590 .UNINDENT
591 .sp
592 They are \\(dqescaped\\(dq anywhere.
593 .\\" End of generated man page.
594 """],
595 ]
596
597 totest['man_header'] = [
598 ["""
599 ============
600 page title
601 ============
602
603 in short
604 --------
605
606 :Manual section: 3
607 :Manual group: the books
608 :Version: 0.0
609 :Date: 3/Nov/2022
610
611 Test title, docinfo to man page header.
612 """,
613 document_start + indend_macros + r""".TH "page title" "3" "3/Nov/2022" "0.0" "the books"
614 .SH Name
615 page title \- in short
616 .sp
617 Test title, docinfo to man page header.
618 .\" End of generated man page.
619 """],
620 ]
621
622 # test defintion
623 # [ input, expect, expected_warning ]
624 totest_system_msgs = {}
625 # check we get an INFO not a WARNING
626 totest_system_msgs['image'] = [
627 ["""\
628 text
629
630 .. image:: gibsnich.png
631 :alt: an image of something
632
633 more text
634 """,
635 document_start + indend_macros + """.TH "" "" "" ""
636 .SH Name
637 \\- \n\
638 text
639 .sp
640 an image of something
641 .sp
642 more text
643 .\\" End of generated man page.
644 """,
645 ['<string>:3: (INFO/1) "image" not supported by "manpage" writer.\n']
646 ],
647
648 # check we get a WARNING if no alt text
649 ["""text
650
651 .. image:: gibsnich.png
652
653 more text
654 """,
655 document_start + indend_macros + """.TH "" "" "" ""
656 .SH Name
657 \\- \n\
658 text
659 .sp
660 image: gibsnich.png
661 .sp
662 more text
663 .\\" End of generated man page.
664 """,
665 ['<string>:3: (WARNING/2) "image" not supported by "manpage" writer.\n',
666 'Please provide an "alt" attribute with textual replacement.\n']
667 ]
668 ]
669
670
671 if __name__ == '__main__':
672 unittest.main()