From a1d457a3ae912425603cf2fe940e19f82dd89995 Mon Sep 17 00:00:00 2001 From: "Martin A. Brown" Date: Sat, 13 Feb 2016 12:32:05 -0800 Subject: [PATCH] initial test support for document enumeration check to see that empty directories are handled correctly check to see that single, multiple files in one dir are handled check to see that single files in multiple directories are handled --- tests/test_sources.py | 76 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/tests/test_sources.py b/tests/test_sources.py index e1c45c8..63c1d23 100644 --- a/tests/test_sources.py +++ b/tests/test_sources.py @@ -6,13 +6,27 @@ 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 -from examples import * +import examples # -- SUT from tldp.sources import Sources, SourceDocument +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 TestSources(unittest.TestCase): @@ -22,6 +36,64 @@ class TestSources(unittest.TestCase): def tearDown(self): shutil.rmtree(self.tempdir) + def mkdir_components(self, components): + dirname = self.tempdir + assert len(components) >= 1 + while components: + dirname = os.path.join(dirname, components.pop(0)) + if not os.path.isdir(dirname): + os.mkdir(dirname) + self.assertTrue(os.path.isdir(dirname)) + relpath = os.path.relpath(dirname, self.tempdir) + return relpath, dirname + + def addfile(self, dirname, exfile, stem=None, ext=None): + if stem is None: + stem, _ = stem_and_ext(exfile.filename) + if ext is None: + _, ext = stem_and_ext(exfile.filename) + newname = os.path.join(dirname, stem + ext) + shutil.copy(exfile.filename, newname) + relname = os.path.relpath(newname, self.tempdir) + return relname, newname + + +class TestFileSourcesMultiDir(TestSources): + + def test_multidir_finding_singlefiles(self): + ex = random.choice(examples.examples) + doc0 = SimpleNamespace(stem="A-Unique-Stem", components=['LDP', 'howto']) + doc1 = SimpleNamespace(stem="A-Different-Stem", components=['LDP', 'guide']) + documents = (doc0, doc1) + for d in documents: + d.reldir, d.absdir = self.mkdir_components(d.components) + d.relname, d.absname = self.addfile(d.absdir, ex, stem=d.stem) + s = Sources([x.absdir for x in documents]) + self.assertEquals(2, len(s.docs)) + sought = set([x.stem for x in documents]) + found = set([x.stem for x in s.docs]) + self.assertEquals(sought, found) + + +class TestFileSourcesOneDir(TestSources): + + def test_finding_singlefile(self): + ex = random.choice(examples.examples) + maindir = ['LDP', 'LDP', 'howto'] + reldir, absdir = self.mkdir_components(maindir) + _, _ = self.addfile(absdir, ex) + s = Sources([absdir]) + self.assertEquals(1, len(s.docs)) + + def test_skipping_misnamed_singlefile(self): + ex = random.choice(examples.examples) + maindir = ['LDP', 'LDP', 'howto'] + reldir, absdir = self.mkdir_components(maindir) + self.addfile(absdir, ex, ext=".mis") + s = Sources([absdir]) + self.assertEquals(1, len(s.docs)) + + class TestInvalidSources(TestSources): def test_validateDirs_onebad(self): @@ -42,7 +114,7 @@ class TestInvalidSources(TestSources): def testEmptyDir(self): s = Sources([self.tempdir]) self.assertEquals(0, len(s.docs)) - + class TestMissingSourceDocuments(TestSources):