mirror of https://github.com/tLDP/python-tldp
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
This commit is contained in:
parent
91708bdc3c
commit
a1d457a3ae
|
@ -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):
|
||||
|
||||
|
|
Loading…
Reference in New Issue