From 82a0fce575f272a381b33ccdb14449dcf7494852 Mon Sep 17 00:00:00 2001 From: "Martin A. Brown" Date: Mon, 15 Feb 2016 23:52:52 -0800 Subject: [PATCH] initial test script for outputs.py --- tests/test_outputs.py | 51 +++++++++++++++++++++++++++++++++++++++++++ tldp/outputs.py | 15 +++++++------ 2 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 tests/test_outputs.py diff --git a/tests/test_outputs.py b/tests/test_outputs.py new file mode 100644 index 0000000..44b3ecf --- /dev/null +++ b/tests/test_outputs.py @@ -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 diff --git a/tldp/outputs.py b/tldp/outputs.py index 51182d1..4fa92bc 100644 --- a/tldp/outputs.py +++ b/tldp/outputs.py @@ -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