-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
cli_tests.py
499 lines (455 loc) · 20.3 KB
/
cli_tests.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
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
"""
Unit tests for the command-line interface.
"""
import io
import logging
import os
import re
import subprocess
import sys
from contextlib import redirect_stdout
from datetime import datetime
from unittest.mock import patch
import pytest
from courlan import UrlStore
from trafilatura import cli, cli_utils, settings, spider
from trafilatura.downloads import add_to_compressed_dict, fetch_url
from trafilatura.filters import LANGID_FLAG
from trafilatura.settings import args_to_extractor
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
RESOURCES_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources')
def test_parser():
'''test argument parsing for the command-line interface'''
testargs = ['', '-fvv', '--xmltei', '--no-tables', '-u', 'https://www.example.org']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
assert args.fast is True
assert args.verbose == 2
assert args.no_tables is False
assert args.xmltei is True
assert args.URL == 'https://www.example.org'
args = cli.map_args(args)
assert args.output_format == 'xmltei'
testargs = ['', '-out', 'csv', '--no-tables', '-u', 'https://www.example.org']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
assert args.fast is False
assert args.verbose == 0
assert args.output_format == 'csv'
assert args.no_tables is False
# test args mapping
testargs = ['', '--markdown']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
args = cli.map_args(args)
assert args.output_format == 'markdown'
testargs = ['', '--xml', '--no-comments', '--precision', '--recall']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
args = cli.map_args(args)
assert args.output_format == 'xml' and args.no_comments is False
# combination possible (?)
assert args.precision is True and args.recall is True
args.xml, args.csv = False, True
args = cli.map_args(args)
assert args.output_format == 'csv'
args.csv, args.json = False, True
args = cli.map_args(args)
assert args.output_format == 'json'
testargs = ['', '--only-with-metadata']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
args = cli.map_args(args)
assert args.only_with_metadata is True
# process_args
args.input_dir = '/dev/null'
args.verbose = 1
args.blacklist = os.path.join(RESOURCES_DIR, 'list-discard.txt')
cli.process_args(args)
assert len(args.blacklist) == 3
# filter
testargs = ['', '-i', 'resources/list-discard.txt', '--url-filter', 'test1', 'test2']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
assert args.input_file == 'resources/list-discard.txt'
assert args.url_filter == ['test1', 'test2']
args.input_file = os.path.join(RESOURCES_DIR, 'list-discard.txt')
args.blacklist = os.path.join(RESOURCES_DIR, 'list-discard.txt')
f = io.StringIO()
with redirect_stdout(f):
cli.process_args(args)
assert len(f.getvalue()) == 0
# input directory
testargs = ['', '--input-dir', 'resources/test/']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
f = io.StringIO()
with redirect_stdout(f):
cli.process_args(args)
assert len(f.getvalue()) == 0
# version
testargs = ['', '--version']
with pytest.raises(SystemExit) as e, redirect_stdout(f):
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
assert e.type == SystemExit
assert e.value.code == 0
assert re.match(r'Trafilatura [0-9]\.[0-9]\.[0-9] - Python [0-9]\.[0-9]+\.[0-9]', f.getvalue())
# test future deprecations
testargs = ['', '--inputfile', 'test.txt', '--with-metadata', '--nocomments', '--notables', '--hash-as-name']
with patch.object(sys, 'argv', testargs):
args = cli.map_args(cli.parse_args(testargs))
assert args.no_comments is False and args.no_tables is False and args.only_with_metadata and args.input_file == 'test.txt'
testargs = ['', '--inputdir', 'test1', '--outputdir', 'test2', '-vv']
with patch.object(sys, 'argv', testargs):
args = cli.map_args(cli.parse_args(testargs))
assert args.input_dir and args.output_dir
def test_climain():
'''test arguments and main CLI entrypoint'''
# exit status required: 0
# Windows platforms
if os.name == 'nt':
trafilatura_bin = os.path.join(sys.prefix, "Scripts", "trafilatura")
# other platforms
else:
trafilatura_bin = 'trafilatura'
# help display
assert subprocess.run([trafilatura_bin, '--help']).returncode == 0
# piped input
empty_input = b'<html><body></body></html>'
assert subprocess.run([trafilatura_bin], input=empty_input).returncode == 0
# input directory walking and processing
env = os.environ.copy()
if os.name == 'nt':
# Force encoding to utf-8 for Windows (seem to be a problem only in GitHub Actions)
env['PYTHONIOENCODING'] = 'utf-8'
assert subprocess.run([trafilatura_bin, '--input-dir', RESOURCES_DIR], env=env).returncode == 0
def test_input_type():
'''test input type errors'''
testfile = 'docs/trafilatura-demo.gif'
testargs = ['', '-u', 'http']
with patch.object(sys, 'argv', testargs):
assert cli.main() is None
testargs = ['', '-v']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
with open(testfile, 'rb') as f:
teststring = f.read(1024)
assert cli.examine(teststring, args) is None
testfile = 'docs/usage.rst'
with open(testfile, 'r', encoding="utf-8") as f:
teststring = f.read()
assert cli.examine(teststring, args) is None
# test file list
assert 10 <= len(list(cli_utils.generate_filelist(RESOURCES_DIR))) <= 20
def test_sysoutput():
'''test command-line output with respect to CLI arguments'''
testargs = ['', '--csv', '-o', '/root/forbidden/']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
filepath, destdir = cli_utils.determine_output_path(args, args.output_dir, '')
assert len(filepath) >= 10 and filepath.endswith('.csv')
assert destdir == '/root/forbidden/'
# doesn't work the same on Windows
if os.name != 'nt':
assert cli_utils.check_outputdir_status(args.output_dir) is False
else:
assert cli_utils.check_outputdir_status(args.output_dir) is True
testargs = ['', '--xml', '-o', '/tmp/you-touch-my-tralala']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
assert cli_utils.check_outputdir_status(args.output_dir) is True
# test fileslug for name
filepath, destdir = cli_utils.determine_output_path(args, args.output_dir, '', new_filename='AAZZ')
assert filepath.endswith('AAZZ.xml')
# test json output
args2 = args
args2.xml, args2.json = False, True
args2 = cli.map_args(args2)
filepath2, destdir2 = cli_utils.determine_output_path(args, args.output_dir, '', new_filename='AAZZ')
assert filepath2.endswith('AAZZ.json')
assert "you-touch-my-tralala" in destdir2
# test directory counter
# doesn't work the same on Windows
if os.name != 'nt':
assert cli_utils.determine_counter_dir('testdir', 0) == 'testdir/1'
else:
assert cli_utils.determine_counter_dir('testdir', 0) == 'testdir\\1'
# test file writing
testargs = ['', '--csv', '-o', '/dev/null/', '-b', '/dev/null/']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
result = 'DADIDA'
cli_utils.write_result(result, args)
# process with backup directory and no counter
options = args_to_extractor(args)
assert cli_utils.process_result('DADIDA', args, None, options) is None
# test keeping dir structure
testargs = ['', '-i', 'myinputdir/', '-o', 'test/', '--keep-dirs']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
filepath, destdir = cli_utils.determine_output_path(args, 'testfile.txt', '')
assert filepath == 'test/testfile.txt'
# test hash as output file name
assert args.keep_dirs is True
args.keep_dirs = False
filepath, destdir = cli_utils.determine_output_path(args, 'testfile.txt', '')
assert filepath == 'test/uOHdo6wKo4IK0pkL.txt'
def test_download():
'''test page download and command-line interface'''
testargs = ['', '-v']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
assert cli.examine(None, args) is None
assert cli.examine(' ', args) is None
assert cli.examine('0'*int(10e7), args) is None
#url = 'https://httpbun.org/status/200'
#teststring = fetch_url(url)
#assert teststring is None # too small
#assert cli.examine(teststring, args, url) is None
#url = 'https://httpbun.org/links/2/2'
#teststring = fetch_url(url)
#assert teststring is not None
#assert cli.examine(teststring, args, url) is None
url = 'https://httpbun.com/html'
teststring = fetch_url(url)
assert teststring is not None
assert cli.examine(teststring, args, url) is not None
# test exit code for faulty URLs
testargs = ['', '-u', 'https://1234.yz/']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
with pytest.raises(SystemExit) as e:
cli.process_args(args)
assert e.type == SystemExit and e.value.code == 1
# @patch('trafilatura.settings.MAX_FILES_PER_DIRECTORY', 1)
def test_cli_pipeline():
'''test command-line processing pipeline'''
# straight command-line input
#testargs = ['', '<html><body>Text</body></html>']
#with patch.object(sys, 'argv', testargs):
# args = cli.parse_args(testargs)
#f = io.StringIO()
#with redirect_stdout(f):
# cli.process_args(args)
#assert len(f.getvalue()) == 0
# Force encoding to utf-8 for Windows in future processes spawned by multiprocessing.Pool
os.environ['PYTHONIOENCODING'] = "utf-8"
# test URL listing
testargs = ['', '--list']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
assert cli_utils.url_processing_pipeline(args, UrlStore()) is False
# test inputlist + blacklist
testargs = ['', '-i', os.path.join(RESOURCES_DIR, 'list-process.txt')]
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
my_urls = cli_utils.load_input_urls(args)
assert my_urls is not None and len(my_urls) == 3
testargs = ['', '-i', os.path.join(RESOURCES_DIR, 'list-process.txt'), '--blacklist', os.path.join(RESOURCES_DIR, 'list-discard.txt'), '--archived']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
assert args.blacklist is not None
# test backoff between domain requests
url_store = add_to_compressed_dict(my_urls, args.blacklist, None, None)
reftime = datetime.now()
cli_utils.url_processing_pipeline(args, url_store)
delta = (datetime.now() - reftime).total_seconds()
assert delta > 2
# test blacklist and empty dict
args.blacklist = cli_utils.load_blacklist(args.blacklist)
assert len(args.blacklist) == 3
url_store = add_to_compressed_dict(my_urls, args.blacklist, None, None)
cli_utils.url_processing_pipeline(args, url_store)
# test backup
testargs = ['', '--backup-dir', '/tmp/']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
cli_utils.archive_html('00Test', args)
# test date-based exclusion
testargs = ['', '-out', 'xml', '--only-with-metadata']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
with open(os.path.join(RESOURCES_DIR, 'httpbin_sample.html'), 'r', encoding="utf-8") as f:
teststring = f.read()
assert cli.examine(teststring, args) is None
testargs = ['', '-out', 'xml', '--only-with-metadata', '--precision']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
with open(os.path.join(RESOURCES_DIR, 'httpbin_sample.html'), 'r', encoding="utf-8") as f:
teststring = f.read()
assert cli.examine(teststring, args) is None
# test JSON output
testargs = ['', '-out', 'json', '--recall']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
with open(os.path.join(RESOURCES_DIR, 'httpbin_sample.html'), 'r', encoding="utf-8") as f:
teststring = f.read()
assert cli.examine(teststring, args) is not None
# sitemaps: tested in --explore
testargs = ['', '--sitemap', 'https://sitemaps.org/sitemap.xml', '--list', '--parallel', '1']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
f = io.StringIO()
with redirect_stdout(f):
cli.process_args(args)
assert f.getvalue().strip().endswith("https://www.sitemaps.org/zh_TW/terms.html")
# CLI options
testargs = ['', '--links', '--images']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
with open(os.path.join(RESOURCES_DIR, 'http_sample.html'), 'r', encoding="utf-8") as f:
teststring = f.read()
result = cli.examine(teststring, args)
assert '[link](testlink.html)' in result and 'test.jpg' in result
def test_file_processing():
"Test file processing pipeline on actual directories."
# dry-run file processing pipeline
testargs = ['', '--parallel', '1', '--input-dir', '/dev/null']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
cli_utils.file_processing_pipeline(args)
# file processing pipeline on resources/
args.input_dir = RESOURCES_DIR
cli_utils.file_processing_pipeline(args)
# test manually
options = args_to_extractor(args)
for f in cli_utils.generate_filelist(args.input_dir):
cli_utils.file_processing(f, args, options=options)
def test_cli_config_file():
"Test if the configuration file is loaded correctly from the CLI."
testargs = ['', '--input-dir', '/dev/null', '--config-file', 'newsettings.cfg']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
with open(os.path.join(RESOURCES_DIR, 'httpbin_sample.html'), 'r', encoding="utf-8") as f:
teststring = f.read()
args.config_file = os.path.join(RESOURCES_DIR, args.config_file)
options = args_to_extractor(args)
assert cli.examine(teststring, args, options=options) is None
def test_input_filtering():
'''test internal functions to filter urls'''
testargs = ['']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
# load dictionary
args.input_file = os.path.join(RESOURCES_DIR, 'list-process.txt')
url_store = cli.load_input_dict(args)
assert len(url_store.find_known_urls('https://httpbin.org')) == 3
args.input_file = os.path.join(RESOURCES_DIR, 'list-process.txt')
args.blacklist = {'httpbin.org/status/404'}
url_store = cli.load_input_dict(args)
assert len(url_store.find_known_urls('https://httpbin.org')) == 2
# deduplication and filtering
inputlist = ['https://example.org/1', 'https://example.org/2', 'https://example.org/2', 'https://example.org/3', 'https://example.org/4', 'https://example.org/5', 'https://example.org/6']
args.blacklist = {'example.org/1', 'example.org/3', 'example.org/5'}
url_store = add_to_compressed_dict(inputlist, blacklist=args.blacklist)
assert url_store.find_known_urls('https://example.org') == ['https://example.org/2', 'https://example.org/4', 'https://example.org/6']
# URL in blacklist
args.input_file = os.path.join(RESOURCES_DIR, 'list-process.txt')
my_urls = cli_utils.load_input_urls(args)
my_blacklist = cli_utils.load_blacklist(os.path.join(RESOURCES_DIR, 'list-discard.txt'))
url_store = add_to_compressed_dict(my_urls, blacklist=my_blacklist)
assert len(url_store.dump_urls()) == 0
# other method
args.input_file = os.path.join(RESOURCES_DIR, 'list-process.txt')
args.blacklist = os.path.join(RESOURCES_DIR, 'list-discard.txt')
args.blacklist = cli_utils.load_blacklist(args.blacklist)
url_store = cli_utils.load_input_dict(args)
assert len(url_store.dump_urls()) == 0
# URL filter
args.input_file = os.path.join(RESOURCES_DIR, 'list-process.txt')
my_urls = cli_utils.load_input_urls(args)
url_store = add_to_compressed_dict(my_urls, blacklist=None, url_filter=['status'], url_store=None)
assert len(url_store.urldict) == 1
url_store = add_to_compressed_dict(my_urls, blacklist=None, url_filter=['teststring'], url_store=None)
assert len(url_store.urldict) == 0
url_store = add_to_compressed_dict(my_urls, blacklist=None, url_filter=['status', 'teststring'], url_store=None)
assert len(url_store.urldict) == 1
# malformed URLs
url_store = add_to_compressed_dict(['123345', 'https://www.example.org/1'])
assert len(url_store.urldict) == 1
# double URLs
args.input_file = os.path.join(RESOURCES_DIR, 'redundant-urls.txt')
my_urls = cli_utils.load_input_urls(args)
url_store = add_to_compressed_dict(my_urls)
assert len(url_store.find_known_urls('https://example.org')) == 1
# filter before exploration
input_store = add_to_compressed_dict(["https://example.org/1", "https://sitemaps.org/test"])
input_urls = ["https://example.org", "http://sitemaps.org/", "https://test.info/"]
url_store = cli_utils.build_exploration_dict(input_store, input_urls, args)
assert url_store.get_known_domains() == ["https://test.info"]
def test_crawling():
"Test crawling and exploration functions."
testargs = ['', '--crawl', '']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
cli_utils.cli_crawler(args)
testargs = ['', '--crawl', ' ']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
cli_utils.cli_crawler(args)
testargs = ['', '--crawl', 'https://httpbun.com/html']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
f = io.StringIO()
with redirect_stdout(f):
cli_utils.cli_crawler(args)
assert f.getvalue() == 'https://httpbun.com/html\n'
spider.URL_STORE = UrlStore(compressed=False, strict=False)
# links permitted
testargs = ['', '--crawl', 'https://httpbun.com/links/1/1', '--list', '--parallel', '1']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
f = io.StringIO()
with redirect_stdout(f):
cli_utils.cli_crawler(args)
# possibly a bug on Github actions, should be 2 URLs
assert f.getvalue() in ('https://httpbun.com/links/1/1\nhttps://httpbun.com/links/1/0\n', 'https://httpbun.com/links/1/1\n')
spider.URL_STORE = UrlStore(compressed=False, strict=False)
# 0 links permitted
args.crawl = 'https://httpbun.com/links/4/4'
f = io.StringIO()
with redirect_stdout(f):
cli_utils.cli_crawler(args, n=0)
## should be 6 (5 URLs as output), possibly a bug on Actions CI/CD
assert len(f.getvalue().split('\n')) in (2, 6)
spider.URL_STORE = UrlStore(compressed=False, strict=False)
# Exploration (Sitemap + Crawl)
testargs = ['', '--explore', 'https://httpbun.com/html', '--list']
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
f = io.StringIO()
with redirect_stdout(f):
cli.process_args(args)
assert f.getvalue().strip() == 'https://httpbun.com/html'
def test_probing():
"Test webpage probing functions."
url = 'https://example.org/'
conf = os.path.join(RESOURCES_DIR, 'zerolength.cfg')
testargs = ['', '--probe', url, '--target-language', 'de', '--config-file', conf]
with patch.object(sys, 'argv', testargs):
args = cli.parse_args(testargs)
f = io.StringIO()
with redirect_stdout(f):
cli.process_args(args)
if LANGID_FLAG:
assert f.getvalue().strip() == ''
args.target_language = 'en'
f2 = io.StringIO()
with redirect_stdout(f2):
cli.process_args(args)
assert f2.getvalue().strip() == url
else:
assert f.getvalue().strip() == url
if __name__ == '__main__':
test_parser()
test_climain()
test_input_type()
test_input_filtering()
test_sysoutput()
test_cli_pipeline()
test_file_processing()
test_cli_config_file()
test_crawling()
test_download()
test_probing()