File: test_naming.py

package info (click to toggle)
pdf2htmlex 0.12%2Bds-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 972 kB
  • ctags: 854
  • sloc: cpp: 6,215; ansic: 406; python: 287; sh: 33; makefile: 12
file content (266 lines) | stat: -rw-r--r-- 10,574 bytes parent folder | download
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
#!/usr/bin/env python

import unittest
import os
import sys
import tempfile
import shutil
import subprocess

# 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')
# The location where the base css file, etc is stored in the build folder
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 = os.getenv('PDF2HTMLEX_EXECUTABLE', './pdf2htmlEX')


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
    """
    executable = os.path.abspath(PDF2HTMLEX_PATH)

    if os.getenv('PDF2HTMLEX_TESTNODATADIR', '').lower() in ['yes', 'true']:
        cmd = [executable]
    else:
        cmd = [executable, '--data-dir', os.path.abspath(DATA_DIR)]

    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
    """
    return os.path.abspath(os.path.join(TEST_DATA_DIR, filename))

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')
        ])
        self.assertEquals(files, sorted(['1-page.html', '1-page1.page']))

    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')
        ])
        self.assertEquals(files, sorted(['3-pages.html', '3-pages1.page', '3-pages2.page', '3-pages3.page']))

    def test_generate_split_pages_specify_name_single_page(self):
        files = execute_pdf2htmlex_and_get_files([
            '--split-pages', 1,
            '--page-filename', 'foo.xyz',
            path_to_test_file('1-page.pdf'),
        ])
        self.assertEquals(files, sorted(['1-page.html', 'foo1.xyz']))

    def test_generate_split_pages_specify_name_multiple_pages(self):
        files = execute_pdf2htmlex_and_get_files([
            '--split-pages', 1,
            '--page-filename', 'foo.xyz',
            path_to_test_file('3-pages.pdf'),
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'foo1.xyz', 'foo2.xyz', 'foo3.xyz']))

    def test_generate_split_pages_specify_name_formatter_multiple_pages(self):
        files = execute_pdf2htmlex_and_get_files([
            '--split-pages', 1,
            '--page-filename', 'fo%do.xyz',
            path_to_test_file('3-pages.pdf'),
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'fo1o.xyz', 'fo2o.xyz', 'fo3o.xyz']))

    def test_generate_split_pages_specify_name_formatter_with_padded_zeros_multiple_pages(self):
        files = execute_pdf2htmlex_and_get_files([
            '--split-pages', 1,
            '--page-filename', 'fo%03do.xyz',
            path_to_test_file('3-pages.pdf')
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'fo001o.xyz', 'fo002o.xyz', 'fo003o.xyz']))

    def test_generate_split_pages_specify_name_only_first_formatter_gets_taken(self):
        files = execute_pdf2htmlex_and_get_files([
            '--split-pages', 1,
            '--page-filename', 'f%do%do.xyz',
            path_to_test_file('3-pages.pdf')
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'f1o%do.xyz', 'f2o%do.xyz', 'f3o%do.xyz']))

    def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_s(self):
        files = execute_pdf2htmlex_and_get_files([
            '--split-pages', 1,
            '--page-filename', 'f%soo.xyz',
            path_to_test_file('3-pages.pdf')
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'f%soo1.xyz', 'f%soo2.xyz', 'f%soo3.xyz']))

    def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_p(self):
        files = execute_pdf2htmlex_and_get_files([
            '--split-pages', 1,
            '--page-filename', 'f%poo.xyz',
            path_to_test_file('3-pages.pdf'),
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'f%poo1.xyz', 'f%poo2.xyz', 'f%poo3.xyz']))


    def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_n(self):
        files = execute_pdf2htmlex_and_get_files([
            '--split-pages', 1,
            '--page-filename', 'f%noo.xyz',
            path_to_test_file('3-pages.pdf')
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'f%noo1.xyz', 'f%noo2.xyz', 'f%noo3.xyz']))

    def test_generate_split_pages_specify_name_only_percent_d_is_used_percent_percent(self):
        files = execute_pdf2htmlex_and_get_files([
            '--split-pages', 1,
            '--page-filename', 'f%%oo.xyz',
            path_to_test_file('3-pages.pdf')
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'f%%oo1.xyz', 'f%%oo2.xyz', 'f%%oo3.xyz']))

    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,
            '--page-filename', 'f%%o%do.xyz',
            path_to_test_file('3-pages.pdf')
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'f%%o1o.xyz', 'f%%o2o.xyz', 'f%%o3o.xyz']))

    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,
            '--page-filename', 'fo%do%%.xyz',
            path_to_test_file('3-pages.pdf')
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'fo1o%%.xyz', 'fo2o%%.xyz', 'fo3o%%.xyz']))

    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,
            '--page-filename', 'f%02%doo.xyz',
            path_to_test_file('3-pages.pdf'),
        ])
        self.assertEquals(files, sorted(['3-pages.html', 'f%021oo.xyz', 'f%022oo.xyz', 'f%023oo.xyz']))

    def test_generate_split_pages_specify_output_filename_no_formatter_no_extension(self):
        files = execute_pdf2htmlex_and_get_files([
            '--split-pages', 1,
            '--page-filename', 'foo',
            path_to_test_file('1-page.pdf'),
        ])
        self.assertEquals(files, sorted(['1-page.html', 'foo1']))

    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'])

if __name__=="__main__":
    executable = os.path.abspath(PDF2HTMLEX_PATH)
    if not os.path.isfile(executable) or not os.access(executable, os.X_OK):
        print >> sys.stderr, "Cannot locate pdf2htmlEX executable. Make sure source was built before running this test."
        exit(1)
    print >> sys.stderr, "Using %s as executable"%executable

    unittest.main()