2013-03-17 05:08:06 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
2013-09-16 03:33:08 +00:00
|
|
|
# We assume that this file is put inside SRC_DIR/test
|
|
|
|
TEST_DIR = os.path.dirname(__file__)
|
|
|
|
# The location where our test PDFs are stored
|
|
|
|
TEST_DATA_DIR = os.path.join(TEST_DIR, 'test_data')
|
2013-03-17 05:08:06 +00:00
|
|
|
# The location where the base css file, etc is stored in the build folder
|
2013-09-16 03:33:08 +00:00
|
|
|
DATA_DIR = os.path.join(TEST_DIR, '../share')
|
|
|
|
|
|
|
|
# The script should be run in the directory containing the binary
|
|
|
|
# The location where the executable is generated by the build
|
|
|
|
PDF2HTMLEX_PATH = './pdf2htmlEX'
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def execute_pdf2htmlex_with_args(args):
|
|
|
|
"""
|
|
|
|
Execute the pdf2htmlEX with the specified arguments.
|
|
|
|
|
|
|
|
:type args: list of values
|
|
|
|
:param args: list of arguments to pass to executable. First part of each tuple is the argument, second part is the value.
|
|
|
|
|
|
|
|
:rtype: int
|
|
|
|
:return: The exit code of the command
|
|
|
|
"""
|
2013-09-16 03:33:08 +00:00
|
|
|
executable = os.path.abspath(PDF2HTMLEX_PATH)
|
2013-03-17 05:08:06 +00:00
|
|
|
|
2013-09-16 03:33:08 +00:00
|
|
|
cmd = [executable, '--data-dir', os.path.abspath(DATA_DIR)]
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
for val in args:
|
|
|
|
cmd.append(str(val))
|
|
|
|
|
|
|
|
return_code = subprocess.call(cmd)
|
|
|
|
|
|
|
|
if return_code != 0:
|
|
|
|
print >> sys.stderr, "Command return code %d: %s" % (return_code, ' '.join(cmd))
|
|
|
|
|
|
|
|
return return_code
|
|
|
|
|
|
|
|
def execute_pdf2htmlex_and_get_files(args):
|
|
|
|
"""
|
|
|
|
Execute the pdf2htmlEX with the specified arguments, and get the names of the output files. Will automatically create
|
|
|
|
a temporary directory for the output, pass that as the output dir to pdf2htmlEX, determine the files generated, and
|
|
|
|
clean up the temporary directory afterwards.
|
|
|
|
|
|
|
|
:type args: list of values
|
|
|
|
:param args: list of arguments to pass to executable. First part of each tuple is the argument, second part is the value.
|
|
|
|
|
|
|
|
:rtype: list of str
|
|
|
|
:return: List of the file names that were generated as output in alphabetical order. None if the command does not execute successfully.
|
|
|
|
"""
|
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
try:
|
|
|
|
if execute_pdf2htmlex_with_args(['--dest-dir', temp_dir] + args) != 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
files = os.listdir(temp_dir)
|
|
|
|
files.sort()
|
|
|
|
return files
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(path=temp_dir, ignore_errors=True)
|
|
|
|
|
|
|
|
def path_to_test_file(filename):
|
|
|
|
"""
|
|
|
|
Retrieve an absolute path to the specified test file.
|
|
|
|
|
|
|
|
:type filename:
|
|
|
|
:param filename: the name of the test file to get the path to
|
|
|
|
|
|
|
|
:rtype: str
|
|
|
|
:returns: the full path to the test file
|
|
|
|
"""
|
2013-09-16 03:33:08 +00:00
|
|
|
return os.path.abspath(os.path.join(TEST_DATA_DIR, filename))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
class OutputNamingTests(unittest.TestCase):
|
|
|
|
def test_generate_single_html_default_name_single_page_pdf(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
path_to_test_file('1-page.pdf')
|
|
|
|
])
|
|
|
|
self.assertEquals(files, ['1-page.html'])
|
|
|
|
|
|
|
|
def test_generate_single_html_default_name_multiple_page_pdf(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
path_to_test_file('2-pages.pdf')
|
|
|
|
])
|
|
|
|
self.assertEquals(files, ['2-pages.html'])
|
|
|
|
|
|
|
|
def test_generate_single_html_specify_name_single_page_pdf(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
path_to_test_file('1-page.pdf'),
|
|
|
|
'foo.html'
|
|
|
|
])
|
|
|
|
self.assertEquals(files, ['foo.html'])
|
|
|
|
|
|
|
|
def test_generate_single_html_specify_name_multiple_page_pdf(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
path_to_test_file('2-pages.pdf'),
|
|
|
|
'foo.html'
|
|
|
|
])
|
|
|
|
self.assertEquals(files, ['foo.html'])
|
|
|
|
|
|
|
|
def test_generate_split_pages_default_name_single_page(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
|
|
|
path_to_test_file('1-page.pdf')
|
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['1-page.html', '1-page1.page']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_default_name_multiple_pages(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
|
|
|
path_to_test_file('3-pages.pdf')
|
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', '3-pages1.page', '3-pages2.page', '3-pages3.page']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_specify_name_single_page(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'foo.xyz',
|
2013-03-17 05:08:06 +00:00
|
|
|
path_to_test_file('1-page.pdf'),
|
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['1-page.html', 'foo1.xyz']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_specify_name_multiple_pages(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'foo.xyz',
|
2013-03-17 05:08:06 +00:00
|
|
|
path_to_test_file('3-pages.pdf'),
|
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'foo1.xyz', 'foo2.xyz', 'foo3.xyz']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_specify_name_formatter_multiple_pages(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'fo%do.xyz',
|
2013-03-17 05:08:06 +00:00
|
|
|
path_to_test_file('3-pages.pdf'),
|
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'fo1o.xyz', 'fo2o.xyz', 'fo3o.xyz']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_specify_name_formatter_with_padded_zeros_multiple_pages(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'fo%03do.xyz',
|
|
|
|
path_to_test_file('3-pages.pdf')
|
2013-03-17 05:08:06 +00:00
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'fo001o.xyz', 'fo002o.xyz', 'fo003o.xyz']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_specify_name_only_first_formatter_gets_taken(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'f%do%do.xyz',
|
|
|
|
path_to_test_file('3-pages.pdf')
|
2013-03-17 05:08:06 +00:00
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'f1o%do.xyz', 'f2o%do.xyz', 'f3o%do.xyz']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_s(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'f%soo.xyz',
|
|
|
|
path_to_test_file('3-pages.pdf')
|
2013-03-17 05:08:06 +00:00
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'f%soo1.xyz', 'f%soo2.xyz', 'f%soo3.xyz']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_p(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'f%poo.xyz',
|
2013-03-17 05:08:06 +00:00
|
|
|
path_to_test_file('3-pages.pdf'),
|
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'f%poo1.xyz', 'f%poo2.xyz', 'f%poo3.xyz']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_n(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'f%noo.xyz',
|
|
|
|
path_to_test_file('3-pages.pdf')
|
2013-03-17 05:08:06 +00:00
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'f%noo1.xyz', 'f%noo2.xyz', 'f%noo3.xyz']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_percent(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'f%%oo.xyz',
|
|
|
|
path_to_test_file('3-pages.pdf')
|
2013-03-17 05:08:06 +00:00
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'f%%oo1.xyz', 'f%%oo2.xyz', 'f%%oo3.xyz']))
|
2013-03-17 05:08:06 +00:00
|
|
|
|
2013-03-18 06:39:02 +00:00
|
|
|
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_percent_with_actual_placeholder(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'f%%o%do.xyz',
|
|
|
|
path_to_test_file('3-pages.pdf')
|
2013-03-18 06:39:02 +00:00
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'f%%o1o.xyz', 'f%%o2o.xyz', 'f%%o3o.xyz']))
|
2013-03-18 06:39:02 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_percent_with_actual_placeholder(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'fo%do%%.xyz',
|
|
|
|
path_to_test_file('3-pages.pdf')
|
2013-03-18 06:39:02 +00:00
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'fo1o%%.xyz', 'fo2o%%.xyz', 'fo3o%%.xyz']))
|
2013-03-18 06:39:02 +00:00
|
|
|
|
2013-03-18 04:31:43 +00:00
|
|
|
def test_generate_split_pages_specify_name_only_formatter_starts_part_way_through_invalid_formatter(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'f%02%doo.xyz',
|
2013-03-18 04:31:43 +00:00
|
|
|
path_to_test_file('3-pages.pdf'),
|
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['3-pages.html', 'f%021oo.xyz', 'f%022oo.xyz', 'f%023oo.xyz']))
|
2013-03-18 04:31:43 +00:00
|
|
|
|
|
|
|
def test_generate_split_pages_specify_output_filename_no_formatter_no_extension(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
'--split-pages', 1,
|
2013-06-04 18:03:29 +00:00
|
|
|
'--page-filename', 'foo',
|
2013-03-18 04:31:43 +00:00
|
|
|
path_to_test_file('1-page.pdf'),
|
|
|
|
])
|
2013-06-04 18:03:29 +00:00
|
|
|
self.assertEquals(files, sorted(['1-page.html', 'foo1']))
|
2013-03-18 04:31:43 +00:00
|
|
|
|
2013-03-17 05:08:06 +00:00
|
|
|
def test_generate_single_html_name_specified_format_characters_percent_d(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
path_to_test_file('2-pages.pdf'),
|
|
|
|
'foo%d.html'
|
|
|
|
])
|
|
|
|
self.assertEquals(files, ['foo%d.html'])
|
|
|
|
|
|
|
|
def test_generate_single_html_name_specified_format_characters_percent_p(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
path_to_test_file('2-pages.pdf'),
|
|
|
|
'foo%p.html'
|
|
|
|
])
|
|
|
|
self.assertEquals(files, ['foo%p.html'])
|
|
|
|
|
|
|
|
def test_generate_single_html_name_specified_format_characters_percent_n(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
path_to_test_file('2-pages.pdf'),
|
|
|
|
'foo%n.html'
|
|
|
|
])
|
|
|
|
self.assertEquals(files, ['foo%n.html'])
|
|
|
|
|
|
|
|
def test_generate_single_html_name_specified_format_characters_percent_percent(self):
|
|
|
|
files = execute_pdf2htmlex_and_get_files([
|
|
|
|
path_to_test_file('2-pages.pdf'),
|
|
|
|
'foo%%.html'
|
|
|
|
])
|
|
|
|
self.assertEquals(files, ['foo%%.html'])
|
2013-03-18 04:31:43 +00:00
|
|
|
|
2013-03-17 05:08:06 +00:00
|
|
|
if __name__=="__main__":
|
2013-09-16 03:33:08 +00:00
|
|
|
executable = os.path.abspath(PDF2HTMLEX_PATH)
|
2013-06-06 17:15:14 +00:00
|
|
|
if not os.path.isfile(executable) or not os.access(executable, os.X_OK):
|
2013-03-17 05:08:06 +00:00
|
|
|
print >> sys.stderr, "Cannot locate pdf2htmlEX executable. Make sure source was built before running this test."
|
|
|
|
exit(1)
|
|
|
|
|
2014-07-13 23:59:30 +00:00
|
|
|
unittest.main()
|