2016-09-26 11:16:48 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
class Common(object):
|
2016-09-26 11:31:18 +00:00
|
|
|
"""
|
|
|
|
Variables and methods for common use in different tests.
|
|
|
|
"""
|
|
|
|
PDF2HTMLEX_PATH = "@PDF2HTMLEX_PATH@" # defined in CMakeLists.txt
|
2016-09-26 11:16:48 +00:00
|
|
|
if not os.path.isfile(PDF2HTMLEX_PATH) or not os.access(PDF2HTMLEX_PATH, os.X_OK):
|
|
|
|
print >> sys.stderr, "Cannot locate pdf2htmlEX executable, expected at ", PDF2HTMLEX_PATH,
|
|
|
|
". Make sure source was built before running this test."
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
SRC_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
TEST_DIR = os.path.join(SRC_DIR, 'test')
|
|
|
|
DATA_DIR = os.path.join(SRC_DIR, 'share')
|
|
|
|
|
|
|
|
GENERATING_MODE = bool(os.environ.get('P2H_TEST_GEN'))
|
|
|
|
|
2016-09-26 14:30:38 +00:00
|
|
|
# temporary directories defined in CMakeLists.txt:
|
|
|
|
TMPDIR = "@PDF2HTMLEX_TMPDIR@"
|
|
|
|
PNGDIR = "@PDF2HTMLEX_PNGDIR@"
|
|
|
|
DATDIR = "@PDF2HTMLEX_DATDIR@"
|
|
|
|
OUTDIR = "@PDF2HTMLEX_OUTDIR@"
|
2016-09-26 11:16:48 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# filter manifest
|
|
|
|
with open(os.path.join(self.DATA_DIR, 'manifest')) as inf:
|
2016-09-26 14:30:38 +00:00
|
|
|
with open(os.path.join(self.DATDIR, 'manifest'), 'w') as outf:
|
2016-09-26 11:16:48 +00:00
|
|
|
ignore = False
|
|
|
|
for line in inf:
|
|
|
|
if ignore:
|
|
|
|
if line.startswith('#TEST_IGNORE_END'):
|
|
|
|
ignore = False
|
|
|
|
elif line.startswith('#TEST_IGNORE_BEGIN'):
|
|
|
|
ignore = True
|
|
|
|
else:
|
|
|
|
outf.write(line)
|
|
|
|
|
|
|
|
# copy files
|
|
|
|
shutil.copy(os.path.join(self.DATA_DIR, 'base.min.css'),
|
2016-09-26 14:30:38 +00:00
|
|
|
os.path.join(self.DATDIR, 'base.min.css'))
|
2016-09-26 11:16:48 +00:00
|
|
|
shutil.copy(os.path.join(self.TEST_DIR, 'fancy.min.css'),
|
2016-09-26 14:30:38 +00:00
|
|
|
os.path.join(self.DATDIR, 'fancy.min.css'))
|
2016-09-26 11:16:48 +00:00
|
|
|
|
|
|
|
def run_pdf2htmlEX(self, args):
|
|
|
|
"""
|
|
|
|
Execute the pdf2htmlEX with the specified arguments.
|
|
|
|
|
|
|
|
:type args: list of values
|
|
|
|
:param args: list of arguments to pass to executable.
|
|
|
|
:return: an object of relevant info
|
|
|
|
"""
|
|
|
|
|
2016-09-26 14:30:38 +00:00
|
|
|
shutil.rmtree(self.TMPDIR, ignore_errors=False, onerror=None)
|
|
|
|
os.mkdir(self.TMPDIR)
|
|
|
|
|
2016-09-26 11:16:48 +00:00
|
|
|
args = [Common.PDF2HTMLEX_PATH,
|
2016-09-26 14:30:38 +00:00
|
|
|
'--data-dir', self.DATDIR,
|
|
|
|
'--dest-dir', self.TMPDIR
|
2016-09-26 11:16:48 +00:00
|
|
|
] + args
|
|
|
|
|
|
|
|
with open(os.devnull, 'w') as fnull:
|
|
|
|
return_code = subprocess.call(list(map(str, args)), stderr=fnull)
|
|
|
|
|
|
|
|
self.assertEquals(return_code, 0, 'cannot execute pdf2htmlEX')
|
|
|
|
|
2016-09-26 14:30:38 +00:00
|
|
|
files = os.listdir(self.TMPDIR)
|
|
|
|
for file in files:
|
|
|
|
shutil.copy(os.path.join(self.TMPDIR,file), self.OUTDIR)
|
2016-09-26 11:16:48 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'return_code' : return_code,
|
|
|
|
'output_files' : files
|
|
|
|
}
|