UFOReader tests and changes as a result of the tests.

git-svn-id: http://svn.robofab.com/branches/ufo3k@294 b5fa9d6c-a76f-4ffd-b3cb-f825fc41095c
This commit is contained in:
Tal Leming 2011-09-26 22:16:08 +00:00
parent e4d321f759
commit 5574c2bc18
2 changed files with 246 additions and 27 deletions

View File

@ -114,6 +114,23 @@ class UFOReader(object):
else:
return True
def _readPlist(self, path):
"""
Read a property list. The errors that
could be raised during the reading of
a plist are unpredictable and/or too
large to list, so, a blind try: except:
is done. If an exception occurs, a
UFOLibError will be raised.
"""
originalPath = path
path = os.path.join(self._path, path)
try:
data = readPlist(path)
return data
except:
raise UFOLibError("The file %s could not be read." % originalPath)
def readBytesFromPath(self, path, encoding=None):
"""
Returns the bytes in the file at the given path.
@ -274,25 +291,18 @@ class UFOReader(object):
are available on disk.
"""
if self._formatVersion < 3:
return
return [(DEFAULT_LAYER_NAME, DEFAULT_GLYPHS_DIRNAME)]
# read the file on disk
path = os.path.join(self._path, LAYERCONTENTS_FILENAME)
if not os.path.exists(path):
raise UFOLibError("layercontents.plist is missing.")
contents = {}
order = []
bogusFileMessage = "layercontents.plist in not in the correct format."
if os.path.exists(path):
raw = self._readPlist(path)
valid, error = layerContentsValidator(raw, self._path)
contents = self._readPlist(path)
valid, error = layerContentsValidator(contents, self._path)
if not valid:
raise UFOLibError(error)
for entry in raw:
layerName, directoryName = entry
contents[layerName] = directoryName
order.insert(0, layerName)
self.layerContents = contents
self.layerOrder = order
return contents
def getLayerNames(self):
"""
@ -308,7 +318,7 @@ class UFOReader(object):
"""
layerContents = self._readLayerContents()
for layerName, layerDirectory in layerContents:
if layerDirectory == DEFAULT_DEFAULT_GLYPHS_DIRNAME:
if layerDirectory == DEFAULT_GLYPHS_DIRNAME:
return layerName
# this will already have been raised during __init__
raise UFOLibError("The default layer is not defined in layercontents.plist.")

View File

@ -3476,22 +3476,231 @@ class WriteFontInfoVersion3TestCase(unittest.TestCase):
self.tearDownUFO()
# -------------------
# layercontents.plist
# -------------------
# ------
# layers
# ------
# bogus layercontents file structure and data
# no layercontents file
# layercontents that doesn't match the data on disk?
# no glyph sets
# no default glyph set, but others
# glyph sets with duplicate names
# default with no name
## going into a file with a name
# default with a name
## going into a file without a name
# layer name used twice
# directory used twice
class UFO3ReadLayersTestCase(unittest.TestCase):
def setUp(self):
self.tempDir = tempfile.mktemp()
self.tempDir = "/users/tal/desktop/layer read test"
os.mkdir(self.tempDir)
self.ufoPath = os.path.join(self.tempDir, "test.ufo")
def tearDown(self):
shutil.rmtree(self.tempDir)
def makeUFO(self, metaInfo=None, layerContents=None):
self.clearUFO()
if not os.path.exists(self.ufoPath):
os.mkdir(self.ufoPath)
# metainfo.plist
if metaInfo is None:
metaInfo = dict(creator="test", formatVersion=3)
path = os.path.join(self.ufoPath, "metainfo.plist")
writePlist(metaInfo, path)
# layers
if layerContents is None:
layerContents = [
("public.foreground", "glyphs"),
("layer 1", "glyphs.layer1"),
("layer 2", "glyphs.layer2"),
]
if layerContents:
path = os.path.join(self.ufoPath, "layercontents.plist")
writePlist(layerContents, path)
else:
layerContents = [("", "glyphs")]
for name, directory in layerContents:
glyphsPath = os.path.join(self.ufoPath, directory)
os.mkdir(glyphsPath)
contents = dict(a="a.glif")
path = os.path.join(glyphsPath, "contents.plist")
writePlist(contents, path)
path = os.path.join(glyphsPath, "a.glif")
f = open(path, "w")
f.write(" ")
f.close()
def clearUFO(self):
if os.path.exists(self.ufoPath):
shutil.rmtree(self.ufoPath)
# valid
def testValidRead(self):
# UFO 1
self.makeUFO(
metaInfo=dict(creator="test", formatVersion=1),
layerContents=dict()
)
reader = UFOReader(self.ufoPath)
reader.getGlyphSet()
# UFO 2
self.makeUFO(
metaInfo=dict(creator="test", formatVersion=2),
layerContents=dict()
)
reader = UFOReader(self.ufoPath)
reader.getGlyphSet()
# UFO 3
self.makeUFO()
reader = UFOReader(self.ufoPath)
reader.getGlyphSet()
# missing layer contents
def testMissingLayerContents(self):
self.makeUFO()
path = os.path.join(self.ufoPath, "layercontents.plist")
os.remove(path)
reader = UFOReader(self.ufoPath)
self.assertRaises(UFOLibError, reader.getGlyphSet)
# layer contents invalid format
def testInvalidLayerContentsFormat(self):
# bogus
self.makeUFO()
path = os.path.join(self.ufoPath, "layercontents.plist")
os.remove(path)
f = open(path, "w")
f.write("test")
f.close()
reader = UFOReader(self.ufoPath)
self.assertRaises(UFOLibError, reader.getGlyphSet)
# dict
self.makeUFO()
path = os.path.join(self.ufoPath, "layercontents.plist")
os.remove(path)
layerContents = {
"public.foreground" : "glyphs",
"layer 1" : "glyphs.layer1",
"layer 2" : "glyphs.layer2",
}
writePlist(layerContents, path)
reader = UFOReader(self.ufoPath)
self.assertRaises(UFOLibError, reader.getGlyphSet)
# layer contents invalid name format
def testInvalidLayerContentsNameFormat(self):
self.makeUFO()
path = os.path.join(self.ufoPath, "layercontents.plist")
os.remove(path)
layerContents = [
(1, "glyphs"),
("layer 1", "glyphs.layer1"),
("layer 2", "glyphs.layer2")
]
writePlist(layerContents, path)
reader = UFOReader(self.ufoPath)
self.assertRaises(UFOLibError, reader.getGlyphSet)
# layer contents invalid directory format
def testInvalidLayerContentsDirectoryFormat(self):
self.makeUFO()
path = os.path.join(self.ufoPath, "layercontents.plist")
os.remove(path)
layerContents = [
("public.foregound", "glyphs"),
("layer 1", 1),
("layer 2", "glyphs.layer2")
]
writePlist(layerContents, path)
reader = UFOReader(self.ufoPath)
self.assertRaises(UFOLibError, reader.getGlyphSet)
# directory listed in contents not on disk
def testLayerContentsHasMissingDirectory(self):
self.makeUFO()
path = os.path.join(self.ufoPath, "layercontents.plist")
os.remove(path)
layerContents = [
("public.foregound", "glyphs"),
("layer 1", "glyphs.doesnotexist"),
("layer 2", "glyphs.layer2")
]
writePlist(layerContents, path)
reader = UFOReader(self.ufoPath)
self.assertRaises(UFOLibError, reader.getGlyphSet)
# # directory on disk not listed in contents
# XXX should this raise an error?
#
# def testLayerContentsHasMissingDirectory(self):
# self.makeUFO()
# path = os.path.join(self.ufoPath, "layercontents.plist")
# os.remove(path)
# layerContents = [
# ("public.foregound", "glyphs"),
# ("layer 1", "glyphs.layer2")
# ]
# writePlist(layerContents, path)
# reader = UFOReader(self.ufoPath)
# self.assertRaises(UFOLibError, reader.getGlyphSet)
# no default layer on disk
def testMissingDefaultLayer(self):
self.makeUFO()
path = os.path.join(self.ufoPath, "layercontents.plist")
os.remove(path)
layerContents = [
("layer 1", "glyphs.layer1"),
("layer 2", "glyphs.layer2")
]
writePlist(layerContents, path)
reader = UFOReader(self.ufoPath)
self.assertRaises(UFOLibError, reader.getGlyphSet)
# duplicate layer name
def testDuplicateLayerName(self):
self.makeUFO()
path = os.path.join(self.ufoPath, "layercontents.plist")
os.remove(path)
layerContents = [
("public.foregound", "glyphs"),
("layer 1", "glyphs.layer1"),
("layer 1", "glyphs.layer2")
]
writePlist(layerContents, path)
reader = UFOReader(self.ufoPath)
self.assertRaises(UFOLibError, reader.getGlyphSet)
# directory referenced by two layer names
def testDuplicateLayerDirectory(self):
self.makeUFO()
path = os.path.join(self.ufoPath, "layercontents.plist")
os.remove(path)
layerContents = [
("public.foregound", "glyphs"),
("layer 1", "glyphs.layer1"),
("layer 2", "glyphs.layer1")
]
writePlist(layerContents, path)
reader = UFOReader(self.ufoPath)
self.assertRaises(UFOLibError, reader.getGlyphSet)
# default with a name
def testDuplicateLayerDirectory(self):
self.makeUFO()
path = os.path.join(self.ufoPath, "layercontents.plist")
os.remove(path)
layerContents = [
("custom name", "glyphs"),
("layer 1", "glyphs.layer1"),
("layer 2", "glyphs.layer2")
]
writePlist(layerContents, path)
reader = UFOReader(self.ufoPath)
reader.getGlyphSet()
# -----
# /data