-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·323 lines (291 loc) · 10.5 KB
/
test.py
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
#!/usr/bin/python
'''Tests for mdtopdf.'''
import io
import os
import sys
import unittest
from typing import Callable
from mdtopdf import cli, __version__
USAGE_LINE: str = (
'usage: cli [-h] [-v] [-o OUTPUT_FILE] [-c COLORSCHEME] '
'[--list-colorschemes] [INPUT_FILE]\n'
)
class TestCli(unittest.TestCase):
'''Test the cli function.'''
@classmethod
def setUpClass(cls) -> None:
cls.clean()
cls.addClassCleanup(cls.clean)
@classmethod
def clean(cls) -> None:
'''Delete all files created during testing.'''
files = ['test_out_short.pdf', 'test_out_long.pdf', 'README.pdf',
'no-css.pdf']
for file in files:
try:
os.remove(file)
except FileNotFoundError:
pass
def assert_command(
self,
msg: str,
command: Callable[[list[str]], int],
args: list[str],
returncode: int = 0,
stdout: str = '',
stderr: str = '',
) -> None:
'''Fail if the returncode or the output of the command don't match.
Args:
msg: The message print on failure.
command: The command to test.
args: The arguments to pass to the command.
returncode: The expected returncode for the command.
stdout: The expected stdout.
stderr: The expected stderr.
'''
sys_stdout = sys.stdout
sys_stderr = sys.stderr
fake_stdout = io.StringIO()
fake_stderr = io.StringIO()
sys.stdout = fake_stdout
sys.stderr = fake_stderr
command_returncode: int
try:
command_returncode = command(args)
except SystemExit as system_exit:
assert isinstance(system_exit.code, int)
command_returncode = system_exit.code
sys.stdout = sys_stdout
sys.stderr = sys_stderr
self.assertEqual(command_returncode, returncode, f'{msg}: returncode')
self.assertEqual(stdout, fake_stdout.getvalue(), f'{msg}: stdout')
self.assertEqual(stderr, fake_stderr.getvalue(), f'{msg}: stderr')
def test_help(self) -> None:
'''Test help flags.'''
# pylint: disable=line-too-long
stdout: str = (
'usage: cli [-h] [-v] [-o OUTPUT_FILE] [-c COLORSCHEME] [--list-colorschemes] [INPUT_FILE]\n'
'\n'
'Convert markdown file to pdf.\n'
'\n'
'positional arguments:\n'
' INPUT_FILE the input file to be converted\n'
'\n'
'options:\n'
' -h, --help show this help message and exit\n'
' -v, --version show program\'s version number and exit\n'
' -o OUTPUT_FILE, --output OUTPUT_FILE the output file where the result will be saved\n'
' -c COLORSCHEME, --colorscheme COLORSCHEME the colorscheme used to color code blocks (default: github-dark)\n'
' --list-colorschemes list all the available colorschemes and exit\n'
)
# pylint: enable=line-too-long
self.assert_command(
'test help short flag',
cli,
['cli', '-h'],
returncode=0,
stdout=stdout
)
self.assert_command(
'test help long flag',
cli,
['cli', '--help'],
returncode=0,
stdout=stdout
)
def test_version(self) -> None:
'''Test version flags.'''
stdout = f'cli {__version__}\n'
self.assert_command(
'test version short flag',
cli,
['cli', '-v'],
returncode=0,
stdout=stdout
)
self.assert_command(
'test version long flag',
cli,
['cli', '--version'],
returncode=0,
stdout=stdout
)
def test_no_args(self) -> None:
'''Test giving no arguments.'''
self.assert_command(
'test giving no arguments',
cli,
['cli'],
returncode=1,
stderr=USAGE_LINE + \
'cli: error: the following arguments are required: INPUT_FILE\n'
)
def test_too_many_args(self) -> None:
'''Test giving too many arguments.'''
self.assert_command(
'test giving too many arguments',
cli,
['cli', 'file1', 'file2', 'file3'],
returncode=1,
stderr=USAGE_LINE + \
'cli: error: unrecognized arguments: file2 file3\n'
)
def test_no_chromium(self) -> None:
'''Test when no chromium executable is in the path.'''
path = os.environ['PATH']
os.environ['PATH'] = ''
def restore_path() -> None:
os.environ['PATH'] = path
self.addCleanup(restore_path)
self.assert_command(
'test no chromium',
cli,
['cli', 'README.md'],
returncode=1,
stderr='cli: error: could not find any chromium executable\n'
)
def test_unknow_argument(self) -> None:
'''Test giving unknow argument.'''
self.assert_command(
'test giving unknow argument',
cli,
['cli', '--unknow-arg'],
returncode=1,
stderr=USAGE_LINE + \
'cli: error: unrecognized arguments: --unknow-arg\n'
)
def test_convert_file(self) -> None:
'''Test convert a file.'''
self.assert_command(
'test convert file',
cli,
['cli', 'README.md']
)
self.assertTrue(os.path.isfile('README.pdf'))
def test_output_file(self) -> None:
'''Test output file arguments.'''
self.assert_command(
'test output file short',
cli,
['cli', 'README.md', '-o', 'test_out_short.pdf']
)
self.assertTrue(os.path.isfile('test_out_short.pdf'))
self.assert_command(
'test output file long',
cli,
['cli', 'README.md', '--output', 'test_out_long.pdf']
)
self.assertTrue(os.path.isfile('test_out_long.pdf'))
def test_no_ouput_file(self) -> None:
'''Test giving no argument for ouput file option.'''
stderr: str = USAGE_LINE + \
'cli: error: argument -o/--output: expected one argument\n'
self.assert_command(
'test giving no output file short',
cli,
['cli', 'file', '-o'],
returncode=1,
stderr=stderr
)
self.assert_command(
'test giving no output file long',
cli,
['cli', 'file', '--output'],
returncode=1,
stderr=stderr
)
def test_inexistent_file(self) -> None:
'''Test inexistent file.'''
self.assert_command(
'test inexistent file',
cli,
['cli', 'file'],
returncode=1,
stderr="cli: error: can't open 'file': No such file or directory\n"
)
def test_directory(self) -> None:
'''Test converting a directory.'''
dirname: str = 'test_dir'
os.mkdir(dirname)
self.addCleanup(lambda: os.rmdir(dirname))
self.assert_command(
'test converting a directory',
cli,
['cli', dirname],
returncode=1,
stderr=f'cli: error: can\'t open \'{dirname}\': Is a directory\n'
)
def test_save_to_directory(self) -> None:
'''Test saving the pdf into a directory.'''
dirname: str = 'test_dir'
os.mkdir(dirname)
self.addCleanup(lambda: os.rmdir(dirname))
self.assert_command(
'test saving the pdf into a directory',
cli,
['cli', 'README.md', '-o', dirname],
returncode=1,
stderr=f'cli: error: can\'t save \'{dirname}\': Is a directory\n'
)
def test_change_colorscheme(self) -> None:
'''Test changing the colorscheme.'''
self.assert_command(
'test changing the colorscheme short',
cli,
['cli', 'README.md', '-c', 'one-dark']
)
self.assert_command(
'test changing the colorscheme long',
cli,
['cli', 'README.md', '--colorscheme', 'one-dark']
)
def test_unknow_colorscheme(self) -> None:
'''Test giving an unknow colorscheme.'''
stderr: str = (
USAGE_LINE + 'cli: error: argument -c/--colorscheme: '
'invalid choice: \'unknow colorscheme\'\n'
)
self.assert_command(
'test giving an unknow colorscheme long',
cli,
['cli', 'README.md', '-c', 'unknow colorscheme'],
returncode=1,
stderr=stderr
)
self.assert_command(
'test giving an unknow colorscheme long',
cli,
['cli', 'README.md', '--colorscheme', 'unknow colorscheme'],
returncode=1,
stderr=stderr
)
def test_no_colorscheme(self) -> None:
'''Test giving no colorscheme.'''
stderr: str = USAGE_LINE + \
'cli: error: argument -c/--colorscheme: expected one argument\n'
self.assert_command(
'test giving no colorscheme short',
cli,
['cli', 'README.md', '-c'],
returncode=1,
stderr=stderr
)
self.assert_command(
'test giving no colorscheme long',
cli,
['cli', 'README.md', '--colorscheme'],
returncode=1,
stderr=stderr
)
def test_list_colorschemes(self) -> None:
'''Test listing available colorschemes.'''
self.assert_command(
'test listing available colorschemes',
cli,
['cli', '--list-colorschemes'],
returncode=0,
stdout='abap\nalgol\nalgol_nu\narduino\nautumn\nbw\nborland\ncoffee\ncolorful\ndefault\ndracula\nemacs\nfriendly_grayscale\nfriendly\nfruity\ngithub-dark\ngruvbox-dark\ngruvbox-light\nigor\ninkpot\nlightbulb\nlilypond\nlovelace\nmanni\nmaterial\nmonokai\nmurphy\nnative\nnord-darker\nnord\none-dark\nparaiso-dark\nparaiso-light\npastie\nperldoc\nrainbow_dash\nrrt\nsas\nsolarized-dark\nsolarized-light\nstaroffice\nstata-dark\nstata-light\ntango\ntrac\nvim\nvs\nxcode\nzenburn\n' # pylint: disable=line-too-long
)
if __name__ == '__main__':
unittest.main()