def test_bad_options(): stdout = io.BytesIO() stderr = io.BytesIO() with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(hocr2djvused.main, ['', '--bad-option']) assert_equal(rc, errors.EXIT_FATAL) assert_not_equal(stderr.getvalue(), '') assert_equal(stdout.getvalue(), '')
def test_help(): stdout = io.BytesIO() stderr = io.BytesIO() with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(hocr2djvused.main, ['', '--help']) assert_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_not_equal(stdout.getvalue(), '')
def test_bad_page_id(): remove_logging_handlers('ocrodjvu.') here = os.path.dirname(__file__) here = os.path.abspath(here) path = os.path.join(here, '..', 'data', 'bad-page-id.djvu') stdout = io.BytesIO() stderr = io.BytesIO() with temporary.directory() as tmpdir: out_path = os.path.join(tmpdir, 'tmp.djvu') with interim(sys, stdout=stdout, stderr=stderr): with interim(ocrodjvu, system_encoding='ASCII'): rc = try_run(ocrodjvu.main, [ '', '--engine', '_dummy', '--save-bundled', out_path, path ]) assert_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_equal(stdout.getvalue(), '')
def test_help(): stdout = io.BytesIO() stderr = io.BytesIO() with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(djvu2hocr.main, ['', '--help']) assert_equal(rc, 0) assert_equal(stderr.getvalue(), '') assert_not_equal(stdout.getvalue(), '')
def test_bad_options(): stdout = io.BytesIO() stderr = io.BytesIO() with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(djvu2hocr.main, ['']) assert_equal(rc, errors.EXIT_FATAL) assert_not_equal(stderr.getvalue(), '') assert_equal(stdout.getvalue(), '')
def _test_list_languages(engine): stdout = io.BytesIO() stderr = io.BytesIO() with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(ocrodjvu.main, ['', '--engine', engine, '--list-languages']) assert_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_not_equal(stdout.getvalue(), '')
def _test_list_languages(engine): stdout = io.BytesIO() stderr = io.BytesIO() with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(ocrodjvu.main, ['', '--engine', engine, '--list-languages']) assert_equal(rc, 0) assert_equal(stderr.getvalue(), '') assert_not_equal(stdout.getvalue(), '')
def test_list_engines(): global engines stdout = io.BytesIO() stderr = io.BytesIO() with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(ocrodjvu.main, ['', '--list-engines']) assert_equal(rc, 0) assert_equal(stderr.getvalue(), '') engines = stdout.getvalue().splitlines()
def test_version(): # https://bugs.debian.org/573496 stdout = io.BytesIO() stderr = io.BytesIO() with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(hocr2djvused.main, ['', '--version']) assert_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_not_equal(stdout.getvalue(), '')
def test_version(): # https://bugs.debian.org/573496 stdout = io.BytesIO() stderr = io.BytesIO() with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(djvu2hocr.main, ['', '--version']) assert_equal(rc, 0) assert_equal(stderr.getvalue(), '') assert_not_equal(stdout.getvalue(), '')
def test_list_engines(): global engines stdout = io.BytesIO() stderr = io.BytesIO() with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(ocrodjvu.main, ['', '--list-engines']) assert_equal(stderr.getvalue(), '') assert_equal(rc, 0) engines = stdout.getvalue().splitlines()
def test_no_debian_pkg(self): def t(): with assert_raises(ImportError) as ecm: try: import nonexistent except ImportError as ex: enhance_import_error(ex, 'PyNonexistent', None, 'http://pynonexistent.example.net/') raise nonexistent.f() # quieten pyflakes assert_equal( str(ecm.exception), 'No module named nonexistent; ' 'please install the PyNonexistent package <http://pynonexistent.example.net/>' ) with interim(lib.utils, debian=False): t() with interim(lib.utils, debian=True): t()
def _test_recognize(self, lang1, lang2): def fake_subprocess(args, *rest, **kwrest): # Record arguments that were used and break immediately. assert_equal(args[0], self.engine.executable) assert_equal(args[1], '-l') assert_equal(args[2], lang1) raise EOFError with interim(lib.ipc, Subprocess=fake_subprocess): with assert_raises(EOFError): with self.engine.recognize(sys.stdin, lang1): pass
def _test_from_file(base_filename, index): base_filename = os.path.join(here, base_filename) test_filename = '{base}.test{i}'.format(base=base_filename, i=index) djvused_filename = base_filename + '.djvused' with open(test_filename, 'rb') as file: commandline = file.readline() expected_output = file.read() args = shlex.split(commandline) assert_equal(args[0], '#') with temporary.directory() as tmpdir: djvu_filename = os.path.join(tmpdir, 'empty.djvu') args += [djvu_filename] shutil.copy( os.path.join(os.path.dirname(__file__), '..', 'data', 'empty.djvu'), djvu_filename) ipc.Subprocess( ['djvused', '-f', djvused_filename, '-s', djvu_filename]).wait() xml_filename = os.path.join(tmpdir, 'output.html') with open(xml_filename, 'w+b') as xml_file: xmllint = ipc.Subprocess(['xmllint', '--format', '-'], stdin=ipc.PIPE, stdout=xml_file) try: with open(os.devnull, 'w') as null: with interim(sys, stdout=xmllint.stdin, stderr=null): with interim(djvu2hocr.logger, handlers=[]): rc = try_run(djvu2hocr.main, args) finally: xmllint.stdin.close() try: xmllint.wait() except ipc.CalledProcessError: # Raising the exception here is likely to hide the real # reason of the failure. pass assert_equal(rc, 0) xml_file.seek(0) output = xml_file.read() assert_multi_line_equal(expected_output, output)
def _rough_test_from_file(base_filename, args): args = ['#'] + shlex.split(args) if base_filename.endswith(('cuneiform0.7', 'cuneiform0.8')): # Add dummy page-size information args += ['--page-size=1000x1000'] base_filename = os.path.join(here, base_filename) html_filename = '{base}.html'.format(base=base_filename) with contextlib.closing(io.BytesIO()) as output_file: with open(html_filename, 'rb') as html_file: with interim(sys, stdin=html_file, stdout=output_file): rc = try_run(hocr2djvused.main, args) assert_equal(rc, 0) output = output_file.getvalue() assert_not_equal(output, '')
def _test_from_file(base_filename, index): base_filename = os.path.join(here, base_filename) test_filename = '{base}.test{i}'.format(base=base_filename, i=index) djvused_filename = base_filename + '.djvused' with open(test_filename, 'rb') as file: commandline = file.readline() expected_output = file.read() args = shlex.split(commandline) assert_equal(args[0], '#') with temporary.directory() as tmpdir: djvu_filename = os.path.join(tmpdir, 'empty.djvu') args += [djvu_filename] shutil.copy( os.path.join(os.path.dirname(__file__), '..', 'data', 'empty.djvu'), djvu_filename) ipc.Subprocess(['djvused', '-f', djvused_filename, '-s', djvu_filename]).wait() xml_filename = os.path.join(tmpdir, 'output.html') with open(xml_filename, 'w+b') as xml_file: xmllint = ipc.Subprocess(['xmllint', '--format', '-'], stdin=ipc.PIPE, stdout=xml_file) try: with open(os.devnull, 'w') as null: with interim(sys, stdout=xmllint.stdin, stderr=null): with interim(djvu2hocr.logger, handlers=[]): rc = try_run(djvu2hocr.main, args) finally: xmllint.stdin.close() try: xmllint.wait() except ipc.CalledProcessError: # Raising the exception here is likely to hide the real # reason of the failure. pass assert_equal(rc, 0) xml_file.seek(0) output = xml_file.read() assert_multi_line_equal(expected_output, output)
def test_nonascii_path(): require_locale_encoding('UTF-8') # djvused breaks otherwise remove_logging_handlers('ocrodjvu.') here = os.path.dirname(__file__) here = os.path.abspath(here) path = os.path.join(here, '..', 'data', 'empty.djvu') stdout = io.BytesIO() stderr = io.BytesIO() with temporary.directory() as tmpdir: tmp_path = os.path.join(tmpdir, 'тмп.djvu') os.symlink(path, tmp_path) with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(djvu2hocr.main, ['', tmp_path]) assert_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_not_equal(stdout.getvalue(), '')
def _test_ocr(engine, layers): if not distutils.spawn.find_executable(engine): raise SkipTest('{cmd} not found'.format(cmd=engine)) remove_logging_handlers('ocrodjvu.') here = os.path.dirname(__file__) here = os.path.abspath(here) path = os.path.join(here, '..', 'data', 'alice.djvu') stdout = io.BytesIO() stderr = io.BytesIO() with temporary.directory() as tmpdir: tmp_path = os.path.join(tmpdir, 'tmp.djvu') with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(ocrodjvu.main, ['', '--engine', engine, '--render', layers, '--save-bundled', tmp_path, path]) assert_multi_line_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_multi_line_equal(stdout.getvalue(), '')
def _test_from_file(base_filename, index, extra_args): base_filename = os.path.join(here, base_filename) test_filename = '{base}.test{i}'.format(base=base_filename, i=index) html_filename = '{base}.html'.format(base=base_filename) with open(test_filename, 'rb') as file: commandline = file.readline() expected_output = file.read() args = shlex.split(commandline) + shlex.split(extra_args) assert_equal(args[0], '#') with contextlib.closing(io.BytesIO()) as output_file: with open(html_filename, 'rb') as html_file: with interim(sys, stdin=html_file, stdout=output_file): rc = try_run(hocr2djvused.main, args) assert_equal(rc, 0) output = output_file.getvalue() assert_multi_line_equal(normalize_djvused(expected_output), normalize_djvused(output))
def _test_from_file(base_filename, index, extra_args): base_filename = os.path.join(here, base_filename) test_filename = '{base}.test{i}'.format(base=base_filename, i=index) html_filename = '{base}.html'.format(base=base_filename) with open(test_filename, 'rb') as file: commandline = file.readline() expected_output = file.read() args = shlex.split(commandline) + shlex.split(extra_args) assert_equal(args[0], '#') with contextlib.closing(io.BytesIO()) as output_file: with open(html_filename, 'rb') as html_file: with interim(sys, stdin=html_file, stdout=output_file): rc = try_run(hocr2djvused.main, args) assert_equal(rc, 0) output = output_file.getvalue() assert_multi_line_equal( normalize_djvused(expected_output), normalize_djvused(output) )
def _test_ocr(engine, layers): if not distutils.spawn.find_executable(engine): raise SkipTest('{cmd} not found'.format(cmd=engine)) remove_logging_handlers('ocrodjvu.') here = os.path.dirname(__file__) here = os.path.abspath(here) path = os.path.join(here, '..', 'data', 'alice.djvu') stdout = io.BytesIO() stderr = io.BytesIO() with temporary.directory() as tmpdir: tmp_path = os.path.join(tmpdir, 'tmp.djvu') with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(ocrodjvu.main, [ '', '--engine', engine, '--render', layers, '--save-bundled', tmp_path, path ]) assert_multi_line_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_multi_line_equal(stdout.getvalue(), '')