initial test script for outputs.py

This commit is contained in:
Martin A. Brown 2016-02-15 23:52:52 -08:00
parent 7b08ececf4
commit 82a0fce575
2 changed files with 59 additions and 7 deletions

51
tests/test_outputs.py Normal file
View File

@ -0,0 +1,51 @@
from __future__ import absolute_import, division, print_function
import os
import errno
import unittest
from tempfile import NamedTemporaryFile as ntf
from tempfile import mkdtemp, mkstemp
import shutil
import random
try:
from types import SimpleNamespace
except ImportError:
from utils import SimpleNamespace
# -- Test Data
import examples
# -- SUT
from tldp.outputs import OutputCollection, OutputDirectory
datadir = os.path.join(os.path.dirname(__file__), 'testdata')
def stem_and_ext(name):
stem, ext = os.path.splitext(os.path.basename(name))
assert ext != ''
return stem, ext
class TestOutputCollection(unittest.TestCase):
def setUp(self):
self.tempdir = mkdtemp(prefix='tldp-outputs-test-')
def tearDown(self):
shutil.rmtree(self.tempdir)
class TestMissingOutputCollection(TestOutputCollection):
def test_not_a_directory(self):
missing = os.path.join(self.tempdir, 'vanishing')
with self.assertRaises(IOError) as ecm:
OutputCollection(missing)
e = ecm.exception
self.assertEquals(errno.ENOENT, e.errno)
#
# -- end of file

View File

@ -5,6 +5,7 @@ from __future__ import absolute_import, division, print_function
import os
import errno
import collections
from .utils import logger
@ -40,6 +41,9 @@ class OutputNamingConvention(object):
class OutputDirectory(OutputNamingConvention):
def __repr__(self):
return '<%s:%s>' % (self.__class__.__name__, self.dirname)
def __init__(self, dirname):
self.dirname = os.path.abspath(dirname)
self.stem = os.path.basename(self.dirname)
@ -64,22 +68,21 @@ class OutputDirectory(OutputNamingConvention):
return True
class OutputTree(object):
class OutputCollection(collections.MutableMapping):
def __repr__(self):
return '<%s:(%s docs)>' % \
(self.__class__.__name__, len(self.docs))
return '<%s:(%s docs)>' % (self.__class__.__name__, len(self))
def __init__(self, dirname):
if not os.path.isdir(dirname):
logger.critical("Directory %s must already exist.", dirname)
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), dirname)
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), dirname)
for fname in os.listdir(dirname):
name = os.path.join(dirname, fname)
if not os.path.isdir(name):
logger.warning("Skipping non-directory %s (in %s)", name, dirname)
o = OutputDirectory(name)
assert not self.has_key(o.stem)
assert not o.stem in self
self[o.stem] = o
def __delitem__(self, key):
@ -97,7 +100,5 @@ class OutputTree(object):
def __len__(self):
return len(self.__dict__)
#
# -- end of file