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