xref: /aosp_15_r20/external/fonttools/Tests/ufoLib/UFOZ_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1from fontTools.ufoLib import UFOReader, UFOWriter, UFOFileStructure
2from fontTools.ufoLib.errors import UFOLibError, GlifLibError
3from fontTools.misc import plistlib
4from fontTools.misc.textTools import tostr
5import sys
6import os
7import fs.osfs
8import fs.tempfs
9import fs.memoryfs
10import fs.copy
11import pytest
12import warnings
13
14
15TESTDATA = fs.osfs.OSFS(os.path.join(os.path.dirname(__file__), "testdata"))
16TEST_UFO3 = "TestFont1 (UFO3).ufo"
17TEST_UFOZ = "TestFont1 (UFO3).ufoz"
18
19
20@pytest.fixture(params=[TEST_UFO3, TEST_UFOZ])
21def testufo(request):
22    name = request.param
23    with fs.tempfs.TempFS() as tmp:
24        if TESTDATA.isdir(name):
25            fs.copy.copy_dir(TESTDATA, name, tmp, name)
26        else:
27            fs.copy.copy_file(TESTDATA, name, tmp, name)
28        yield tmp.getsyspath(name)
29
30
31@pytest.fixture
32def testufoz():
33    with fs.tempfs.TempFS() as tmp:
34        fs.copy.copy_file(TESTDATA, TEST_UFOZ, tmp, TEST_UFOZ)
35        yield tmp.getsyspath(TEST_UFOZ)
36
37
38class TestUFOZ:
39    def test_read(self, testufoz):
40        with UFOReader(testufoz) as reader:
41            assert reader.fileStructure == UFOFileStructure.ZIP
42            assert reader.formatVersion == 3
43
44    def test_write(self, testufoz):
45        with UFOWriter(testufoz, structure="zip") as writer:
46            writer.writeLib({"hello world": 123})
47        with UFOReader(testufoz) as reader:
48            assert reader.readLib() == {"hello world": 123}
49
50
51def test_pathlike(testufo):
52    class PathLike:
53        def __init__(self, s):
54            self._path = s
55
56        def __fspath__(self):
57            return tostr(self._path, sys.getfilesystemencoding())
58
59    path = PathLike(testufo)
60
61    with UFOReader(path) as reader:
62        assert reader._path == path.__fspath__()
63
64    with UFOWriter(path) as writer:
65        assert writer._path == path.__fspath__()
66
67
68def test_path_attribute_deprecated(testufo):
69    with UFOWriter(testufo) as writer:
70        with pytest.warns(DeprecationWarning, match="The 'path' attribute"):
71            writer.path
72
73
74@pytest.fixture
75def memufo():
76    m = fs.memoryfs.MemoryFS()
77    fs.copy.copy_dir(TESTDATA, TEST_UFO3, m, "/")
78    return m
79
80
81class TestMemoryFS:
82    def test_init_reader(self, memufo):
83        with UFOReader(memufo) as reader:
84            assert reader.formatVersion == 3
85            assert reader.fileStructure == UFOFileStructure.PACKAGE
86
87    def test_init_writer(self):
88        m = fs.memoryfs.MemoryFS()
89        with UFOWriter(m) as writer:
90            assert m.exists("metainfo.plist")
91            assert writer._path == "<memfs>"
92