File: test_cli.py

package info (click to toggle)
psautohint 2.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,148 kB
  • sloc: ansic: 11,179; python: 5,556; makefile: 6; sh: 1
file content (346 lines) | stat: -rw-r--r-- 10,203 bytes parent folder | download | duplicates (2)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import glob
import os
from os.path import basename
import subprocess
import pytest

from psautohint import FontParseError
from psautohint.__main__ import main as psautohint
from psautohint.__main__ import stemhist

from .differ import main as differ
from . import make_temp_copy, DATA_DIR


# font.otf, font.cff, font.ufo
FONTS = glob.glob("%s/dummy/font.[ocu][tf][fo]" % DATA_DIR)


def autohint(args):
    return psautohint(["--all", "--test"] + args)


@pytest.mark.parametrize("path", FONTS)
def test_basic(path, tmpdir):
    # the input font is modified in-place, make a temp copy first
    autohint([make_temp_copy(tmpdir, path)])


@pytest.mark.parametrize("path", FONTS)
def test_outpath(path, tmpdir):
    out = str(tmpdir / basename(path)) + ".out"

    autohint([path, '-o', out])


def test_multi_outpath(tmpdir):
    """Test handling multiple output paths."""
    paths = sorted(glob.glob("%s/dummy/mm0/*.ufo" % DATA_DIR))
    # the reference font is modified in-place, make a temp copy first
    reference = make_temp_copy(tmpdir, paths[0])
    inpaths = paths[1:]
    outpaths = [str(tmpdir / basename(p)) for p in inpaths]

    autohint(inpaths + ['-o'] + outpaths + ['-r', reference])


def test_multi_outpath_unequal(tmpdir):
    """Test that we exit if output paths don't match number of input paths."""
    paths = sorted(glob.glob("%s/dummy/mm0/*.ufo" % DATA_DIR))
    # the reference font is modified in-place, make a temp copy first
    reference = make_temp_copy(tmpdir, paths[0])
    inpaths = paths[1:]
    outpaths = [str(tmpdir / basename(p)) for p in inpaths][1:]

    with pytest.raises(SystemExit):
        autohint(inpaths + ['-o'] + outpaths + ['-r', reference])


def test_multi_different_formats(tmpdir):
    """Test that we exit if input paths are of different formats."""
    base = "%s/dummy/mm0" % DATA_DIR
    paths = sorted(glob.glob(base + "/*.ufo"))
    otfs = sorted(glob.glob(base + "/*.otf"))
    # the reference font is modified in-place, make a temp copy first
    reference = make_temp_copy(tmpdir, otfs[0])
    inpaths = paths[1:]
    outpaths = [str(tmpdir / basename(p)) for p in inpaths]

    with pytest.raises(SystemExit):
        autohint(inpaths + ['-o'] + outpaths + ['-r', reference])


def test_multi_reference_is_input(tmpdir):
    """Test that we exit if reference font is also one of the input paths."""
    paths = sorted(glob.glob("%s/dummy/mm0/*.ufo" % DATA_DIR))
    # the reference font is modified in-place, make a temp copy first
    reference = make_temp_copy(tmpdir, paths[0])
    inpaths = [reference] + paths[1:]
    outpaths = [str(tmpdir / basename(p)) for p in inpaths]

    with pytest.raises(SystemExit):
        autohint(inpaths + ['-o'] + outpaths + ['-r', reference])


def test_multi_reference_is_duplicated(tmpdir):
    """Test that we exit if one of the input paths is duplicated."""
    paths = sorted(glob.glob("%s/dummy/mm0/*.ufo" % DATA_DIR))
    # the reference font is modified in-place, make a temp copy first
    reference = make_temp_copy(tmpdir, paths[0])
    inpaths = paths[1:] + [paths[1]]
    outpaths = [str(tmpdir / basename(p)) for p in inpaths]

    with pytest.raises(SystemExit):
        autohint(inpaths + ['-o'] + outpaths + ['-r', reference])


tx_found = False
try:
    subprocess.check_call(["tx", "-h"])
    tx_found = True
except (subprocess.CalledProcessError, OSError):
    pass


@pytest.mark.parametrize("path", glob.glob("%s/dummy/font.p*" % DATA_DIR))
@pytest.mark.skipif(tx_found, reason="'tx' is found")
def test_type1_raises(path, tmpdir):
    out = str(tmpdir / basename(path)) + ".out"

    with pytest.raises(SystemExit):
        autohint([path, '-o', out])


@pytest.mark.parametrize("path", glob.glob("%s/dummy/font.p*" % DATA_DIR))
@pytest.mark.skipif(tx_found is False, reason="'tx' is missing")
def test_type1_supported(path, tmpdir):
    out = str(tmpdir / basename(path)) + ".out"

    autohint([path, '-o', out])


@pytest.mark.parametrize("glyphs", [
    'a,b,c',      # Glyph List
    'a-z',        # Glyph range
    'FOO,BAR,a',  # Some glyphs in the list do not exist.
])
def test_glyph_list(glyphs, tmpdir):
    path = "%s/dummy/font.ufo" % DATA_DIR
    out = str(tmpdir / basename(path)) + ".out"

    autohint([path, '-o', out, '-g', glyphs])


@pytest.mark.parametrize("glyphs", [
    '/0,/1,/2',
    '/0-/10',
    'cid0,cid1,cid2',
    'cid0-cid10',
])
def test_cid_glyph_list(glyphs, tmpdir):
    path = "%s/source-code-pro/CID/font.otf" % DATA_DIR
    out = str(tmpdir / basename(path)) + ".out"

    autohint([path, '-o', out, '-g', glyphs])


@pytest.mark.parametrize("glyphs", [
    'a,b,c',
    'a-z',
])
def test_exclude_glyph_list(glyphs, tmpdir):
    path = "%s/dummy/font.ufo" % DATA_DIR
    out = str(tmpdir / basename(path)) + ".out"

    autohint([path, '-o', out, '-x', glyphs])


@pytest.mark.parametrize("glyphs", [
    'FOO,BAR',
    'FOO-BAR',
    'FOO-a',
    'a-BAR',
])
def test_missing_glyph_list(glyphs, tmpdir):
    path = "%s/dummy/font.ufo" % DATA_DIR
    out = str(tmpdir / basename(path)) + ".out"

    with pytest.raises(FontParseError):
        autohint([path, '--traceback', '-o', out, '-g', glyphs])


@pytest.mark.parametrize("path", ["%s/dummy/fontinfo" % DATA_DIR, DATA_DIR])
def test_unsupported_format(path):
    with pytest.raises(SystemExit):
        autohint([path])


def test_missing_cff_table1(tmpdir):
    path = "%s/dummy/nocff.otf" % DATA_DIR
    out = str(tmpdir / basename(path)) + ".out"

    assert autohint([path, '-o', out]) == 1


def test_missing_cff_table2(tmpdir):
    path = "%s/dummy/nocff.otf" % DATA_DIR
    out = str(tmpdir / basename(path)) + ".out"

    with pytest.raises(FontParseError):
        autohint([path, '-o', out, '--traceback'])


@pytest.mark.parametrize("option,argument", [
    ("--exclude-glyphs-file", "glyphs.txt"),
    ("--fontinfo-file", "fontinfo"),
    ("--glyphs-file", "glyphs.txt"),
])
@pytest.mark.parametrize("path", ["font.ufo", "font.otf"])
def test_option(path, option, argument, tmpdir):
    path = "%s/dummy/%s" % (DATA_DIR, path)
    out = str(tmpdir / basename(path)) + ".out"

    argument = "%s/dummy/%s" % (DATA_DIR, argument)

    autohint([path, '-o', out, option, argument])


@pytest.mark.parametrize("option", [
    "--allow-changes",
    "--decimal",
    "--no-flex",
    "--no-hint-sub",
    "--no-zones-stems",
    "--print-list-fddict",
    "--report-only",
    "--verbose",
    "--write-to-default-layer",
    "-vv",
])
@pytest.mark.parametrize("path", ["font.ufo", "font.otf"])
def test_argumentless_option(path, option, tmpdir):
    path = "%s/dummy/%s" % (DATA_DIR, path)
    out = str(tmpdir / basename(path)) + ".out"

    autohint([path, '-o', out, option])


def test_print_fddict(capsys):
    dummypath = os.path.join(DATA_DIR, "dummy")
    fontpath = os.path.join(dummypath, "font.otf")
    fipath = os.path.join(dummypath, "fontinfo_fdd")
    exp_path = os.path.join(dummypath, 'print_dflt_fddict_expected.txt')

    with open(exp_path, 'r') as exp_f:
        expected = exp_f.read()
        with pytest.raises(SystemExit) as wrapped_exc:
            autohint([fontpath,
                      '--print-dflt-fddict',
                      '--fontinfo-file',
                      fipath])
            str(wrapped_exc) == expected


@pytest.mark.parametrize("option", [
    "--doc-fddict",
    "--help",
    "--info",
    "--version",
])
def test_doc_option(option):
    with pytest.raises(SystemExit) as e:
        autohint([option])
    assert e.type == SystemExit
    assert e.value.code == 0


def test_no_fddict(tmpdir):
    path = "%s/dummy/mm0/font0.ufo" % DATA_DIR
    out = str(tmpdir / basename(path)) + ".out"

    autohint([path, '-o', out, "--print-list-fddict"])


@pytest.mark.parametrize("path", [
    "%s/dummy/font.ufo" % DATA_DIR,
    "%s/dummy/font.otf" % DATA_DIR,
    "%s/dummy/font.cff" % DATA_DIR,
])
def test_overwrite_font(path, tmpdir):
    out = str(tmpdir / basename(path)) + ".out"

    autohint([path, '-o', out, '-g', 'a,b,c'])
    autohint([path, '-o', out, '-g', 'a,b,c'])


def test_invalid_input_path(tmpdir):
    path = str(tmpdir / "foo") + ".otf"
    with pytest.raises(SystemExit):
        autohint([path])


def test_invalid_save_path(tmpdir):
    path = "%s/dummy/font.otf" % DATA_DIR
    out = str(tmpdir / basename(path) / "foo") + ".out"
    with pytest.raises(SystemExit):
        autohint([path, '-o', out])


@pytest.mark.parametrize("args", [
    pytest.param(['-z'], id="report_zones"),
    pytest.param([], id="report_stems"),
    pytest.param(['-a'], id="report_stems,all_stems"),
    pytest.param(['-g', 'a-z,A-Z,zero-nine'], id="report_stems,glyphs"),
])
def test_stemhist(args, tmpdir):
    path = "%s/dummy/font.otf" % DATA_DIR
    out = str(tmpdir / basename(path))

    stemhist([path, '-o', out] + args)

    if '-z' in args:
        suffixes = ['.top.txt', '.bot.txt']
    else:
        suffixes = ['.hstm.txt', '.vstm.txt']

    for suffix in suffixes:
        exp_suffix = suffix
        if '-a' in args:
            exp_suffix = '.all' + suffix
        if '-g' in args:
            g = args[args.index('-g') + 1]
            exp_suffix = '.' + g + exp_suffix
        assert differ([path + exp_suffix, out + suffix, '-l', '1'])


@pytest.mark.parametrize("path", FONTS)
def test_outpath_order(path, tmpdir):
    """ e.g. psautohint -o outfile infile"""
    out = str(tmpdir / basename(path)) + ".out"

    autohint(['-o', out, path])


def test_multi_order(tmpdir):
    """ e.g. psautohint -o outfile1 outfile2 infile1 infile2"""
    in1 = "%s/dummy/font.ufo" % DATA_DIR
    in2 = "%s/dummy/big_glyph.ufo" % DATA_DIR
    out1 = str(tmpdir / basename(in1)) + ".out"
    out2 = str(tmpdir / basename(in2)) + ".out"

    autohint(['-o', out1, out2, in1, in2])


def test_multi_order_unequal(tmpdir):
    """ e.g. psautohint -o outfile1 outfile2 infile"""
    in1 = "%s/dummy/font.ufo" % DATA_DIR
    out1 = str(tmpdir / basename(in1)) + ".out"
    out2 = str(tmpdir / basename(in1)) + "X.out"

    with pytest.raises(SystemExit):
        autohint(['-o', out1, out2, in1])


def test_lack_of_input_raises(tmpdir):
    with pytest.raises(SystemExit):
        autohint(['--report-only'])