1
|
|
|
import argparse |
2
|
|
|
import datetime |
3
|
|
|
import unittest |
4
|
|
|
from distutils.errors import DistutilsOptionError |
5
|
|
|
|
6
|
|
|
from setuptools.dist import Distribution |
7
|
|
|
|
8
|
|
|
from coalib.misc.BuildManPage import BuildManPage, ManPageFormatter |
9
|
|
|
from coalib.misc.ContextManagers import make_temp |
10
|
|
|
|
11
|
|
|
app_name = "name" |
12
|
|
|
app_description = ("short description " * 2).strip() |
13
|
|
|
app_long_description = ("long description " * 80).strip() |
14
|
|
|
section_name = "sect" |
15
|
|
|
section_text = ("section text " * 5).strip() |
16
|
|
|
sections = {section_name: section_text} |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def test_arg_parser(formatter_class=argparse.RawDescriptionHelpFormatter): |
|
|
|
|
20
|
|
|
arg_parser = argparse.ArgumentParser(formatter_class=formatter_class, |
21
|
|
|
prog=app_name, |
22
|
|
|
description=app_description) |
23
|
|
|
arg_parser.add_argument('arg1') |
24
|
|
|
arg_parser.add_argument('-a') |
25
|
|
|
|
26
|
|
|
return arg_parser |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class ManPageFormatterTest(unittest.TestCase): |
|
|
|
|
30
|
|
|
|
31
|
|
|
def test_format_functions(self): |
32
|
|
|
uut = ManPageFormatter(app_name) |
|
|
|
|
33
|
|
|
self.assertEqual(ManPageFormatter._markup("a-b"), "a\\-b") |
34
|
|
|
self.assertEqual(ManPageFormatter._underline("test"), "\\fItest\\fR") |
35
|
|
|
self.assertEqual(ManPageFormatter._bold("test"), "\\fBtest\\fR") |
36
|
|
|
self.assertEqual(ManPageFormatter._bold("\\fBtest"), "\\fBtest\\fR") |
37
|
|
|
self.assertEqual(ManPageFormatter._bold("test\\fR"), "\\fBtest\\fR") |
38
|
|
|
|
39
|
|
|
def test_mk_title(self): |
40
|
|
|
uut = ManPageFormatter(app_name, parser=test_arg_parser()) |
|
|
|
|
41
|
|
|
today = datetime.date.today().strftime('%Y\\-%m\\-%d') |
42
|
|
|
self.assertEqual(uut._mk_title(), |
43
|
|
|
'.TH {0} {1} {2}\n'.format(app_name, 1, today)) |
|
|
|
|
44
|
|
|
|
45
|
|
|
def test_mk_name(self): |
46
|
|
|
uut = ManPageFormatter(app_name, parser=test_arg_parser()) |
|
|
|
|
47
|
|
|
self.assertEqual(uut._mk_name(), |
48
|
|
|
".SH NAME\n{}\n".format(app_name)) |
49
|
|
|
|
50
|
|
|
def test_mk_synopsis(self): |
51
|
|
|
uut = ManPageFormatter(app_name, parser=test_arg_parser()) |
|
|
|
|
52
|
|
|
self.assertEqual( |
53
|
|
|
uut._mk_synopsis(), |
54
|
|
|
".SH SYNOPSIS\n \\fB{}\\fR [-h] [-a A] arg1\n\n\n".format( |
55
|
|
|
app_name)) |
56
|
|
|
|
57
|
|
|
def test_mk_description(self): |
58
|
|
|
uut = ManPageFormatter(app_name, |
|
|
|
|
59
|
|
|
parser=test_arg_parser()) |
60
|
|
|
self.assertEqual(uut._mk_description(), "") |
61
|
|
|
uut = ManPageFormatter(app_name, |
62
|
|
|
parser=test_arg_parser(), |
63
|
|
|
long_desc=app_long_description) |
|
|
|
|
64
|
|
|
self.assertEqual(uut._mk_description(), |
65
|
|
|
".SH DESCRIPTION\n{}\n".format(app_long_description)) |
66
|
|
|
|
67
|
|
|
def test_mk_options(self): |
68
|
|
|
uut = ManPageFormatter(app_name, parser=test_arg_parser()) |
|
|
|
|
69
|
|
|
self.assertEqual(uut._mk_options(), |
70
|
|
|
".SH OPTIONS\n" |
71
|
|
|
" arg1\n\n" |
72
|
|
|
" -h, --help show this help message and exit\n" |
73
|
|
|
" -a A\n") |
74
|
|
|
|
75
|
|
|
def test_mk_footer(self): |
76
|
|
|
uut = ManPageFormatter(app_name, ext_sections=sections) |
|
|
|
|
77
|
|
|
self.assertEqual(uut._mk_footer(), |
78
|
|
|
".SH {0}\n" |
79
|
|
|
" {1}".format(section_name.upper(), section_text)) |
|
|
|
|
80
|
|
|
uut = ManPageFormatter(app_name, ext_sections=None) |
81
|
|
|
self.assertEqual(uut._mk_footer(), "") |
82
|
|
|
|
83
|
|
|
def test_formatter(self): |
84
|
|
|
parser = test_arg_parser(ManPageFormatter) |
|
|
|
|
85
|
|
|
self.assertEqual( |
86
|
|
|
parser.format_help(), |
87
|
|
|
"usage: {0} [-h] [-a A] arg1\n\n{1}\n\n" |
88
|
|
|
"positional arguments:\n" |
89
|
|
|
" arg1\n\n" |
90
|
|
|
"optional arguments:\n" |
91
|
|
|
" \\fB-h\\fR, \\fB--help\\fR\n" |
92
|
|
|
" show this help message and exit\n" |
93
|
|
|
" \\fB-a\\fR \\fIA\\fR\n" |
94
|
|
|
.format(app_name, app_description)) |
|
|
|
|
95
|
|
|
|
96
|
|
|
parser = ManPageFormatter(app_name, |
97
|
|
|
parser=argparse.ArgumentParser( |
98
|
|
|
prog=app_name)) |
99
|
|
|
today = datetime.date.today().strftime('%Y\\-%m\\-%d') |
100
|
|
|
self.assertEqual(parser.format_man_page(), |
101
|
|
|
".TH {0} 1 {1}\n" |
102
|
|
|
".SH NAME\n" |
103
|
|
|
"{0}\n" |
104
|
|
|
".SH SYNOPSIS\n" |
105
|
|
|
" \\fBname\\fR [-h]\n\n\n" |
106
|
|
|
".SH OPTIONS\n" |
107
|
|
|
" -h, --help show this help message and exit\n" |
108
|
|
|
.format(app_name, today)) |
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
class BuildManPageTest(unittest.TestCase): |
|
|
|
|
112
|
|
|
|
113
|
|
|
def test_finalize_options(self): |
114
|
|
|
dist = Distribution() |
115
|
|
|
uut = BuildManPage(dist) |
|
|
|
|
116
|
|
|
self.assertRaises(DistutilsOptionError, uut.finalize_options) |
|
|
|
|
117
|
|
|
with make_temp() as uut.output: |
118
|
|
|
self.assertRaises(DistutilsOptionError, uut.finalize_options) |
119
|
|
|
uut.parser = "tests.misc.BuildManPageTest:test_arg_parser" |
120
|
|
|
|
121
|
|
|
uut.finalize_options() |
122
|
|
|
self.assertIsInstance(uut._parser, argparse.ArgumentParser) |
|
|
|
|
123
|
|
|
|
124
|
|
|
uut.run() |
125
|
|
|
with open(uut.output) as file: |
|
|
|
|
126
|
|
|
result = file.read(1000) |
127
|
|
|
|
128
|
|
|
today = datetime.date.today().strftime('%Y\\-%m\\-%d') |
129
|
|
|
self.assertEqual(result, |
|
|
|
|
130
|
|
|
""".TH {0} 1 {1} |
131
|
|
|
.SH NAME |
132
|
|
|
{0} |
133
|
|
|
.SH SYNOPSIS |
134
|
|
|
\\fB{0}\\fR [-h] [-a A] arg1 |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
.SH DESCRIPTION |
138
|
|
|
UNKNOWN |
139
|
|
|
.SH OPTIONS |
140
|
|
|
arg1 |
141
|
|
|
|
142
|
|
|
\\fB-h\\fR, \\fB--help\\fR |
143
|
|
|
show this help message and exit |
144
|
|
|
\\fB-a\\fR \\fIA\\fR |
145
|
|
|
.SH LICENSE |
146
|
|
|
UNKNOWN |
147
|
|
|
.SH MAINTAINER(S) |
148
|
|
|
UNKNOWN |
149
|
|
|
.SH SEE ALSO |
150
|
|
|
Online documentation: UNKNOWN""".format(app_name, today)) |
|
|
|
|
151
|
|
|
|