1*e1fe3e4aSElliott Hughesimport logging 2*e1fe3e4aSElliott Hughesimport shutil 3*e1fe3e4aSElliott Hughes 4*e1fe3e4aSElliott Hughesfrom fontTools.misc import plistlib 5*e1fe3e4aSElliott Hughesfrom fontTools.ufoLib import UFOReader, UFOWriter, UFOFormatVersion 6*e1fe3e4aSElliott Hughesfrom fontTools.ufoLib.errors import UFOLibError, UnsupportedUFOFormat 7*e1fe3e4aSElliott Hughesimport pytest 8*e1fe3e4aSElliott Hughes 9*e1fe3e4aSElliott Hughes 10*e1fe3e4aSElliott Hughes@pytest.fixture 11*e1fe3e4aSElliott Hughesdef ufo_path(tmp_path): 12*e1fe3e4aSElliott Hughes ufodir = tmp_path / "TestFont.ufo" 13*e1fe3e4aSElliott Hughes ufodir.mkdir() 14*e1fe3e4aSElliott Hughes with (ufodir / "metainfo.plist").open("wb") as f: 15*e1fe3e4aSElliott Hughes plistlib.dump({"creator": "pytest", "formatVersion": 3}, f) 16*e1fe3e4aSElliott Hughes (ufodir / "glyphs").mkdir() 17*e1fe3e4aSElliott Hughes with (ufodir / "layercontents.plist").open("wb") as f: 18*e1fe3e4aSElliott Hughes plistlib.dump([("public.default", "glyphs")], f) 19*e1fe3e4aSElliott Hughes return ufodir 20*e1fe3e4aSElliott Hughes 21*e1fe3e4aSElliott Hughes 22*e1fe3e4aSElliott Hughesdef test_formatVersion_deprecated(ufo_path): 23*e1fe3e4aSElliott Hughes reader = UFOReader(ufo_path) 24*e1fe3e4aSElliott Hughes 25*e1fe3e4aSElliott Hughes with pytest.warns(DeprecationWarning) as warnings: 26*e1fe3e4aSElliott Hughes assert reader.formatVersion == 3 27*e1fe3e4aSElliott Hughes 28*e1fe3e4aSElliott Hughes assert len(warnings) == 1 29*e1fe3e4aSElliott Hughes assert "is deprecated; use the 'formatVersionTuple'" in warnings[0].message.args[0] 30*e1fe3e4aSElliott Hughes 31*e1fe3e4aSElliott Hughes 32*e1fe3e4aSElliott Hughesdef test_formatVersionTuple(ufo_path): 33*e1fe3e4aSElliott Hughes reader = UFOReader(ufo_path) 34*e1fe3e4aSElliott Hughes 35*e1fe3e4aSElliott Hughes assert reader.formatVersionTuple == (3, 0) 36*e1fe3e4aSElliott Hughes assert reader.formatVersionTuple.major == 3 37*e1fe3e4aSElliott Hughes assert reader.formatVersionTuple.minor == 0 38*e1fe3e4aSElliott Hughes assert str(reader.formatVersionTuple) == "3.0" 39*e1fe3e4aSElliott Hughes 40*e1fe3e4aSElliott Hughes 41*e1fe3e4aSElliott Hughesdef test_readMetaInfo_errors(ufo_path): 42*e1fe3e4aSElliott Hughes (ufo_path / "metainfo.plist").unlink() 43*e1fe3e4aSElliott Hughes with pytest.raises(UFOLibError, match="'metainfo.plist' is missing"): 44*e1fe3e4aSElliott Hughes UFOReader(ufo_path) 45*e1fe3e4aSElliott Hughes 46*e1fe3e4aSElliott Hughes (ufo_path / "metainfo.plist").write_bytes(plistlib.dumps({})) 47*e1fe3e4aSElliott Hughes with pytest.raises(UFOLibError, match="Missing required formatVersion"): 48*e1fe3e4aSElliott Hughes UFOReader(ufo_path) 49*e1fe3e4aSElliott Hughes 50*e1fe3e4aSElliott Hughes (ufo_path / "metainfo.plist").write_bytes(plistlib.dumps([])) 51*e1fe3e4aSElliott Hughes with pytest.raises(UFOLibError, match="metainfo.plist is not properly formatted"): 52*e1fe3e4aSElliott Hughes UFOReader(ufo_path) 53*e1fe3e4aSElliott Hughes 54*e1fe3e4aSElliott Hughes 55*e1fe3e4aSElliott Hughesdef test_readMetaInfo_unsupported_format_version(ufo_path, caplog): 56*e1fe3e4aSElliott Hughes metainfo = {"formatVersion": 10, "formatVersionMinor": 15} 57*e1fe3e4aSElliott Hughes (ufo_path / "metainfo.plist").write_bytes(plistlib.dumps(metainfo)) 58*e1fe3e4aSElliott Hughes 59*e1fe3e4aSElliott Hughes with pytest.raises(UnsupportedUFOFormat): 60*e1fe3e4aSElliott Hughes UFOReader(ufo_path) # validate=True by default 61*e1fe3e4aSElliott Hughes 62*e1fe3e4aSElliott Hughes with pytest.raises(UnsupportedUFOFormat): 63*e1fe3e4aSElliott Hughes UFOReader(ufo_path, validate=True) 64*e1fe3e4aSElliott Hughes 65*e1fe3e4aSElliott Hughes caplog.clear() 66*e1fe3e4aSElliott Hughes with caplog.at_level(logging.WARNING, logger="fontTools.ufoLib"): 67*e1fe3e4aSElliott Hughes UFOReader(ufo_path, validate=False) 68*e1fe3e4aSElliott Hughes 69*e1fe3e4aSElliott Hughes assert len(caplog.records) == 1 70*e1fe3e4aSElliott Hughes assert "Unsupported UFO format" in caplog.text 71*e1fe3e4aSElliott Hughes assert "Assuming the latest supported version" in caplog.text 72*e1fe3e4aSElliott Hughes 73*e1fe3e4aSElliott Hughes 74*e1fe3e4aSElliott Hughesdef test_UFOWriter_formatVersion(tmp_path): 75*e1fe3e4aSElliott Hughes ufo_path = tmp_path / "TestFont.ufo" 76*e1fe3e4aSElliott Hughes with UFOWriter(ufo_path, formatVersion=3) as writer: 77*e1fe3e4aSElliott Hughes assert writer.formatVersionTuple == (3, 0) 78*e1fe3e4aSElliott Hughes 79*e1fe3e4aSElliott Hughes shutil.rmtree(str(ufo_path)) 80*e1fe3e4aSElliott Hughes with UFOWriter(ufo_path, formatVersion=(2, 0)) as writer: 81*e1fe3e4aSElliott Hughes assert writer.formatVersionTuple == (2, 0) 82*e1fe3e4aSElliott Hughes 83*e1fe3e4aSElliott Hughes 84*e1fe3e4aSElliott Hughesdef test_UFOWriter_formatVersion_default_latest(tmp_path): 85*e1fe3e4aSElliott Hughes writer = UFOWriter(tmp_path / "TestFont.ufo") 86*e1fe3e4aSElliott Hughes assert writer.formatVersionTuple == UFOFormatVersion.default() 87*e1fe3e4aSElliott Hughes 88*e1fe3e4aSElliott Hughes 89*e1fe3e4aSElliott Hughesdef test_UFOWriter_unsupported_format_version(tmp_path): 90*e1fe3e4aSElliott Hughes with pytest.raises(UnsupportedUFOFormat): 91*e1fe3e4aSElliott Hughes UFOWriter(tmp_path, formatVersion=(123, 456)) 92*e1fe3e4aSElliott Hughes 93*e1fe3e4aSElliott Hughes 94*e1fe3e4aSElliott Hughesdef test_UFOWriter_previous_higher_format_version(ufo_path): 95*e1fe3e4aSElliott Hughes with pytest.raises( 96*e1fe3e4aSElliott Hughes UnsupportedUFOFormat, match="UFO located at this path is a higher version" 97*e1fe3e4aSElliott Hughes ): 98*e1fe3e4aSElliott Hughes UFOWriter(ufo_path, formatVersion=(2, 0)) 99