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