• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(
16    os.path.join(os.path.dirname(__file__), "testdata")
17)
18TEST_UFO3 = "TestFont1 (UFO3).ufo"
19TEST_UFOZ = "TestFont1 (UFO3).ufoz"
20
21
22@pytest.fixture(params=[TEST_UFO3, TEST_UFOZ])
23def testufo(request):
24    name = request.param
25    with fs.tempfs.TempFS() as tmp:
26        if TESTDATA.isdir(name):
27            fs.copy.copy_dir(TESTDATA, name, tmp, name)
28        else:
29            fs.copy.copy_file(TESTDATA, name, tmp, name)
30        yield tmp.getsyspath(name)
31
32
33@pytest.fixture
34def testufoz():
35    with fs.tempfs.TempFS() as tmp:
36        fs.copy.copy_file(TESTDATA, TEST_UFOZ, tmp, TEST_UFOZ)
37        yield tmp.getsyspath(TEST_UFOZ)
38
39
40class TestUFOZ:
41
42    def test_read(self, testufoz):
43        with UFOReader(testufoz) as reader:
44            assert reader.fileStructure == UFOFileStructure.ZIP
45            assert reader.formatVersion == 3
46
47    def test_write(self, testufoz):
48        with UFOWriter(testufoz, structure="zip") as writer:
49            writer.writeLib({"hello world": 123})
50        with UFOReader(testufoz) as reader:
51            assert reader.readLib() == {"hello world": 123}
52
53
54def test_pathlike(testufo):
55
56    class PathLike:
57
58        def __init__(self, s):
59            self._path = s
60
61        def __fspath__(self):
62            return tostr(self._path, sys.getfilesystemencoding())
63
64    path = PathLike(testufo)
65
66    with UFOReader(path) as reader:
67        assert reader._path == path.__fspath__()
68
69    with UFOWriter(path) as writer:
70        assert writer._path == path.__fspath__()
71
72
73def test_path_attribute_deprecated(testufo):
74    with UFOWriter(testufo) as writer:
75        with pytest.warns(DeprecationWarning, match="The 'path' attribute"):
76            writer.path
77
78
79@pytest.fixture
80def memufo():
81    m = fs.memoryfs.MemoryFS()
82    fs.copy.copy_dir(TESTDATA, TEST_UFO3, m, "/")
83    return m
84
85
86class TestMemoryFS:
87
88    def test_init_reader(self, memufo):
89        with UFOReader(memufo) as reader:
90            assert reader.formatVersion == 3
91            assert reader.fileStructure == UFOFileStructure.PACKAGE
92
93    def test_init_writer(self):
94        m = fs.memoryfs.MemoryFS()
95        with UFOWriter(m) as writer:
96            assert m.exists("metainfo.plist")
97            assert writer._path == "<memfs>"
98