2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
from tempfile import NamedTemporaryFile as ntf
|
|
|
|
from tempfile import mkdtemp, mkstemp
|
|
|
|
import shutil
|
2016-02-13 20:32:05 +00:00
|
|
|
import random
|
|
|
|
|
|
|
|
try:
|
|
|
|
from types import SimpleNamespace
|
|
|
|
except ImportError:
|
|
|
|
from utils import SimpleNamespace
|
2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
# -- Test Data
|
2016-02-13 20:32:05 +00:00
|
|
|
import examples
|
2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
# -- SUT
|
2016-02-16 04:58:30 +00:00
|
|
|
from tldp.sources import SourceDirs, SourceDocument
|
2016-02-13 07:48:48 +00:00
|
|
|
|
2016-02-13 20:32:05 +00:00
|
|
|
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
|
|
|
|
|
2016-02-13 07:48:48 +00:00
|
|
|
|
2016-02-16 04:58:30 +00:00
|
|
|
class TestSourceDirs(unittest.TestCase):
|
2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.tempdir = mkdtemp(prefix='tldp-sources-test-')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.tempdir)
|
|
|
|
|
2016-02-13 20:32:05 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2016-02-16 04:58:30 +00:00
|
|
|
class TestFileSourceDirsMultiDir(TestSourceDirs):
|
2016-02-13 20:32:05 +00:00
|
|
|
|
|
|
|
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)
|
2016-02-16 04:58:30 +00:00
|
|
|
s = SourceDirs([x.absdir for x in documents])
|
2016-02-13 20:32:05 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2016-02-16 04:58:30 +00:00
|
|
|
class TestFileSourceDirsOneDir(TestSourceDirs):
|
2016-02-13 20:32:05 +00:00
|
|
|
|
|
|
|
def test_finding_singlefile(self):
|
|
|
|
ex = random.choice(examples.examples)
|
|
|
|
maindir = ['LDP', 'LDP', 'howto']
|
|
|
|
reldir, absdir = self.mkdir_components(maindir)
|
|
|
|
_, _ = self.addfile(absdir, ex)
|
2016-02-16 04:58:30 +00:00
|
|
|
s = SourceDirs([absdir])
|
2016-02-13 20:32:05 +00:00
|
|
|
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")
|
2016-02-16 04:58:30 +00:00
|
|
|
s = SourceDirs([absdir])
|
2016-02-13 20:32:05 +00:00
|
|
|
self.assertEquals(1, len(s.docs))
|
|
|
|
|
|
|
|
|
2016-02-16 04:58:30 +00:00
|
|
|
class TestInvalidSourceDirs(TestSourceDirs):
|
2016-02-13 07:48:48 +00:00
|
|
|
|
|
|
|
def test_validateDirs_onebad(self):
|
|
|
|
invalid0 = os.path.join(self.tempdir, 'unique', 'rabbit')
|
|
|
|
with self.assertRaises(OSError) as ecm:
|
2016-02-16 04:58:30 +00:00
|
|
|
SourceDirs([invalid0])
|
2016-02-13 07:48:48 +00:00
|
|
|
e = ecm.exception
|
|
|
|
self.assertTrue('unique/rabbit' in e.message)
|
|
|
|
|
|
|
|
def test_validateDirs_multibad(self):
|
|
|
|
invalid0 = os.path.join(self.tempdir, 'unique', 'rabbit')
|
|
|
|
invalid1 = os.path.join(self.tempdir, 'affable', 'elephant')
|
|
|
|
with self.assertRaises(OSError) as ecm:
|
2016-02-16 04:58:30 +00:00
|
|
|
SourceDirs([invalid0, invalid1])
|
2016-02-13 07:48:48 +00:00
|
|
|
e = ecm.exception
|
|
|
|
self.assertTrue('affable/elephant' in e.message)
|
|
|
|
|
|
|
|
def testEmptyDir(self):
|
2016-02-16 04:58:30 +00:00
|
|
|
s = SourceDirs([self.tempdir])
|
2016-02-13 07:48:48 +00:00
|
|
|
self.assertEquals(0, len(s.docs))
|
2016-02-13 20:32:05 +00:00
|
|
|
|
2016-02-13 07:48:48 +00:00
|
|
|
|
2016-02-16 04:58:30 +00:00
|
|
|
class TestMissingSourceDocuments(TestSourceDirs):
|
2016-02-13 07:59:44 +00:00
|
|
|
|
|
|
|
def test_init_missing(self):
|
|
|
|
missing = os.path.join(self.tempdir, 'vanishing')
|
|
|
|
with self.assertRaises(OSError) as ecm:
|
|
|
|
SourceDocument(missing)
|
|
|
|
e = ecm.exception
|
|
|
|
self.assertTrue('Missing' in e.message)
|
|
|
|
|
|
|
|
with self.assertRaises(OSError) as ecm:
|
|
|
|
SourceDocument(self.tempdir)
|
|
|
|
e = ecm.exception
|
|
|
|
self.assertTrue('Wrong type' in e.message)
|
|
|
|
|
2016-02-13 07:48:48 +00:00
|
|
|
#
|
|
|
|
# -- end of file
|