xref: /aosp_15_r20/external/fonttools/Tests/cu2qu/cli_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesimport os
2*e1fe3e4aSElliott Hughes
3*e1fe3e4aSElliott Hughesimport pytest
4*e1fe3e4aSElliott Hughesimport py
5*e1fe3e4aSElliott Hughes
6*e1fe3e4aSElliott HughesufoLib2 = pytest.importorskip("ufoLib2")
7*e1fe3e4aSElliott Hughes
8*e1fe3e4aSElliott Hughesfrom fontTools.cu2qu.ufo import CURVE_TYPE_LIB_KEY
9*e1fe3e4aSElliott Hughesfrom fontTools.cu2qu.cli import main
10*e1fe3e4aSElliott Hughes
11*e1fe3e4aSElliott Hughes
12*e1fe3e4aSElliott HughesDATADIR = os.path.join(os.path.dirname(__file__), "data")
13*e1fe3e4aSElliott Hughes
14*e1fe3e4aSElliott HughesTEST_UFOS = [
15*e1fe3e4aSElliott Hughes    py.path.local(DATADIR).join("RobotoSubset-Regular.ufo"),
16*e1fe3e4aSElliott Hughes    py.path.local(DATADIR).join("RobotoSubset-Bold.ufo"),
17*e1fe3e4aSElliott Hughes]
18*e1fe3e4aSElliott Hughes
19*e1fe3e4aSElliott Hughes
20*e1fe3e4aSElliott Hughes@pytest.fixture
21*e1fe3e4aSElliott Hughesdef test_paths(tmpdir):
22*e1fe3e4aSElliott Hughes    result = []
23*e1fe3e4aSElliott Hughes    for path in TEST_UFOS:
24*e1fe3e4aSElliott Hughes        new_path = tmpdir / path.basename
25*e1fe3e4aSElliott Hughes        path.copy(new_path)
26*e1fe3e4aSElliott Hughes        result.append(new_path)
27*e1fe3e4aSElliott Hughes    return result
28*e1fe3e4aSElliott Hughes
29*e1fe3e4aSElliott Hughes
30*e1fe3e4aSElliott Hughesclass MainTest(object):
31*e1fe3e4aSElliott Hughes    @staticmethod
32*e1fe3e4aSElliott Hughes    def run_main(*args):
33*e1fe3e4aSElliott Hughes        main([str(p) for p in args if p])
34*e1fe3e4aSElliott Hughes
35*e1fe3e4aSElliott Hughes    def test_single_input_no_output(self, test_paths):
36*e1fe3e4aSElliott Hughes        ufo_path = test_paths[0]
37*e1fe3e4aSElliott Hughes
38*e1fe3e4aSElliott Hughes        self.run_main(ufo_path)
39*e1fe3e4aSElliott Hughes
40*e1fe3e4aSElliott Hughes        font = ufoLib2.Font.open(ufo_path)
41*e1fe3e4aSElliott Hughes        assert font.lib[CURVE_TYPE_LIB_KEY] == "quadratic"
42*e1fe3e4aSElliott Hughes
43*e1fe3e4aSElliott Hughes    def test_single_input_output_file(self, tmpdir):
44*e1fe3e4aSElliott Hughes        input_path = TEST_UFOS[0]
45*e1fe3e4aSElliott Hughes        output_path = tmpdir / input_path.basename
46*e1fe3e4aSElliott Hughes        self.run_main("-o", output_path, input_path)
47*e1fe3e4aSElliott Hughes
48*e1fe3e4aSElliott Hughes        assert output_path.check(dir=1)
49*e1fe3e4aSElliott Hughes
50*e1fe3e4aSElliott Hughes    def test_multiple_inputs_output_dir(self, tmpdir):
51*e1fe3e4aSElliott Hughes        output_dir = tmpdir / "output_dir"
52*e1fe3e4aSElliott Hughes        self.run_main("-d", output_dir, *TEST_UFOS)
53*e1fe3e4aSElliott Hughes
54*e1fe3e4aSElliott Hughes        assert output_dir.check(dir=1)
55*e1fe3e4aSElliott Hughes        outputs = set(p.basename for p in output_dir.listdir())
56*e1fe3e4aSElliott Hughes        assert "RobotoSubset-Regular.ufo" in outputs
57*e1fe3e4aSElliott Hughes        assert "RobotoSubset-Bold.ufo" in outputs
58*e1fe3e4aSElliott Hughes
59*e1fe3e4aSElliott Hughes    def test_interpolatable_inplace(self, test_paths):
60*e1fe3e4aSElliott Hughes        self.run_main("-i", *test_paths)
61*e1fe3e4aSElliott Hughes        self.run_main("-i", *test_paths)  # idempotent
62*e1fe3e4aSElliott Hughes
63*e1fe3e4aSElliott Hughes    @pytest.mark.parametrize("mode", ["", "-i"], ids=["normal", "interpolatable"])
64*e1fe3e4aSElliott Hughes    def test_copytree(self, mode, tmpdir):
65*e1fe3e4aSElliott Hughes        output_dir = tmpdir / "output_dir"
66*e1fe3e4aSElliott Hughes        self.run_main(mode, "-d", output_dir, *TEST_UFOS)
67*e1fe3e4aSElliott Hughes
68*e1fe3e4aSElliott Hughes        output_dir_2 = tmpdir / "output_dir_2"
69*e1fe3e4aSElliott Hughes        # no conversion when curves are already quadratic, just copy
70*e1fe3e4aSElliott Hughes        self.run_main(mode, "-d", output_dir_2, *output_dir.listdir())
71*e1fe3e4aSElliott Hughes        # running again overwrites existing with the copy
72*e1fe3e4aSElliott Hughes        self.run_main(mode, "-d", output_dir_2, *output_dir.listdir())
73*e1fe3e4aSElliott Hughes
74*e1fe3e4aSElliott Hughes    def test_multiprocessing(self, tmpdir, test_paths):
75*e1fe3e4aSElliott Hughes        self.run_main(*(test_paths + ["-j"]))
76*e1fe3e4aSElliott Hughes
77*e1fe3e4aSElliott Hughes    def test_keep_direction(self, test_paths):
78*e1fe3e4aSElliott Hughes        self.run_main("--keep-direction", *test_paths)
79*e1fe3e4aSElliott Hughes
80*e1fe3e4aSElliott Hughes    def test_conversion_error(self, test_paths):
81*e1fe3e4aSElliott Hughes        self.run_main("--conversion-error", 0.002, *test_paths)
82*e1fe3e4aSElliott Hughes
83*e1fe3e4aSElliott Hughes    def test_conversion_error_short(self, test_paths):
84*e1fe3e4aSElliott Hughes        self.run_main("-e", 0.003, test_paths[0])
85