mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
c.library.cli: extract check_library printing
- extract printing/formatting logic from calibre.library.cli.command_check_library - add tests asserting current behaviour
This commit is contained in:
parent
a517cec2cd
commit
fd70317171
@ -1362,7 +1362,19 @@ def command_check_library(args, dbpath):
|
|||||||
else:
|
else:
|
||||||
exts = [f.strip() for f in opts.exts.split(',') if f.strip()]
|
exts = [f.strip() for f in opts.exts.split(',') if f.strip()]
|
||||||
|
|
||||||
def print_one(checker, check):
|
|
||||||
|
if not LibraryDatabase.exists_at(dbpath):
|
||||||
|
prints('No library found at', dbpath, file=sys.stderr)
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
db = LibraryDatabase(dbpath)
|
||||||
|
checker = CheckLibrary(dbpath, db)
|
||||||
|
checker.scan_library(names, exts)
|
||||||
|
for check in checks:
|
||||||
|
_print_check_library_results(checker, check)
|
||||||
|
|
||||||
|
|
||||||
|
def _print_check_library_results(checker, check, opts):
|
||||||
attr = check[0]
|
attr = check[0]
|
||||||
list = getattr(checker, attr, None)
|
list = getattr(checker, attr, None)
|
||||||
if list is None:
|
if list is None:
|
||||||
@ -1375,16 +1387,6 @@ def command_check_library(args, dbpath):
|
|||||||
for i in list:
|
for i in list:
|
||||||
print ' %-40.40s - %-40.40s'%(i[0], i[1])
|
print ' %-40.40s - %-40.40s'%(i[0], i[1])
|
||||||
|
|
||||||
if not LibraryDatabase.exists_at(dbpath):
|
|
||||||
prints('No library found at', dbpath, file=sys.stderr)
|
|
||||||
raise SystemExit(1)
|
|
||||||
|
|
||||||
db = LibraryDatabase(dbpath)
|
|
||||||
checker = CheckLibrary(dbpath, db)
|
|
||||||
checker.scan_library(names, exts)
|
|
||||||
for check in checks:
|
|
||||||
print_one(checker, check)
|
|
||||||
|
|
||||||
|
|
||||||
def restore_database_option_parser():
|
def restore_database_option_parser():
|
||||||
parser = get_parser(_(
|
parser = get_parser(_(
|
||||||
|
101
src/calibre/library/test_cli.py
Normal file
101
src/calibre/library/test_cli.py
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
#!/usr/bin/env python2
|
||||||
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
|
from __future__ import (unicode_literals, division, absolute_import,
|
||||||
|
print_function)
|
||||||
|
|
||||||
|
__license__ = 'GPL v3'
|
||||||
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
|
'''
|
||||||
|
Test the CLI of the calibre database management tool
|
||||||
|
'''
|
||||||
|
import csv, sys
|
||||||
|
import unittest
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
|
from mock import Mock, patch
|
||||||
|
|
||||||
|
from calibre.library.check_library import CheckLibrary
|
||||||
|
from calibre.library.cli import _print_check_library_results
|
||||||
|
|
||||||
|
|
||||||
|
class PrintCheckLibraryResultsTest(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Asserts the format of the output to the CLI to avoid regressions
|
||||||
|
"""
|
||||||
|
check_machine_name = 'dummy_check'
|
||||||
|
check_human_name = 'Dummy Check'
|
||||||
|
check = (check_machine_name, check_human_name, True, False)
|
||||||
|
|
||||||
|
@patch('sys.stdout', new_callable=StringIO)
|
||||||
|
def test_prints_nothing_if_no_errors(self, mock_stdout):
|
||||||
|
checker = Mock(name='checker', spec=CheckLibrary)
|
||||||
|
setattr(checker, self.check_machine_name, None)
|
||||||
|
opts = Mock()
|
||||||
|
|
||||||
|
opts.csv = False
|
||||||
|
_print_check_library_results(checker, self.check, opts)
|
||||||
|
self.assertEqual(mock_stdout.getvalue(), '')
|
||||||
|
|
||||||
|
opts.csv = True
|
||||||
|
_print_check_library_results(checker, self.check, opts)
|
||||||
|
self.assertEqual(mock_stdout.getvalue(), '')
|
||||||
|
|
||||||
|
@patch('sys.stdout', new_callable=StringIO)
|
||||||
|
def test_human_readable_output(self, mock_stdout):
|
||||||
|
"""
|
||||||
|
Basic check of the human-readable output.
|
||||||
|
|
||||||
|
Does not test: the full line format, truncation
|
||||||
|
"""
|
||||||
|
checker = Mock(name='checker', speck=CheckLibrary)
|
||||||
|
data = [['first', 'second']]
|
||||||
|
opts = Mock()
|
||||||
|
opts.csv = False
|
||||||
|
setattr(checker, self.check_machine_name, data)
|
||||||
|
_print_check_library_results(checker, self.check, opts)
|
||||||
|
|
||||||
|
result = mock_stdout.getvalue().split('\n')
|
||||||
|
self.assertEqual(len(result), len(data)+2)
|
||||||
|
self.assertEqual(result[0], self.check_human_name)
|
||||||
|
|
||||||
|
result_first = result[1].split('-')[0].strip()
|
||||||
|
result_second = result[1].split('-')[1].strip()
|
||||||
|
|
||||||
|
self.assertEqual(result_first, 'first')
|
||||||
|
self.assertEqual(result_second, 'second')
|
||||||
|
|
||||||
|
self.assertEqual(result[-1], '')
|
||||||
|
|
||||||
|
@patch('sys.stdout', new_callable=StringIO)
|
||||||
|
def test_basic_csv_output(self, mock_stdout):
|
||||||
|
"""
|
||||||
|
Test simple csv output
|
||||||
|
"""
|
||||||
|
checker = Mock(name='checker', speck=CheckLibrary)
|
||||||
|
data = [['first', 'second']]
|
||||||
|
opts = Mock()
|
||||||
|
opts.csv = True
|
||||||
|
setattr(checker, self.check_machine_name, data)
|
||||||
|
_print_check_library_results(checker, self.check, opts)
|
||||||
|
|
||||||
|
result = mock_stdout.getvalue().split('\n')
|
||||||
|
parsed_result = [l for l in csv.reader(result) if l]
|
||||||
|
self.assertEqual(parsed_result, [[self.check_human_name, data[0][0], data[0][1]]])
|
||||||
|
|
||||||
|
@patch('sys.stdout', new_callable=StringIO)
|
||||||
|
def test_escaped_csv_output(self, mock_stdout):
|
||||||
|
"""
|
||||||
|
Test more complex csv output
|
||||||
|
"""
|
||||||
|
raise unittest.SkipTest('This test fails, as csv output does not currently escape')
|
||||||
|
checker = Mock(name='checker', speck=CheckLibrary)
|
||||||
|
data = [['I, Caesar', 'second']]
|
||||||
|
opts = Mock()
|
||||||
|
opts.csv = True
|
||||||
|
setattr(checker, self.check_machine_name, data)
|
||||||
|
_print_check_library_results(checker, self.check, opts)
|
||||||
|
|
||||||
|
result = mock_stdout.getvalue().split('\n')
|
||||||
|
parsed_result = [l for l in csv.reader(result) if l]
|
||||||
|
self.assertEqual(parsed_result, [[self.check_human_name, data[0][0], data[0][1]]])
|
Loading…
x
Reference in New Issue
Block a user