diff options
-rw-r--r-- | tests/test_collection.py | 181 | ||||
-rw-r--r-- | tests/test_commands.py | 4 | ||||
-rw-r--r-- | tests/test_derivatives.py | 9 |
3 files changed, 117 insertions, 77 deletions
diff --git a/tests/test_collection.py b/tests/test_collection.py index 51b7864..4f9cd75 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -16,14 +16,18 @@ from . import utils class testEntries(unittest.TestCase): def setUp(self): - self.tmpdir = tempfile.mkdtemp() - utils.copytree('tests/data/simple', self.tmpdir, dirs_exist_ok=True) - self.collection = lesana.Collection(self.tmpdir) + self.tmpdir = tempfile.TemporaryDirectory() + utils.copytree( + 'tests/data/simple', + self.tmpdir.name, + dirs_exist_ok=True + ) + self.collection = lesana.Collection(self.tmpdir.name) self.basepath = self.collection.itemdir self.filenames = [] def tearDown(self): - shutil.rmtree(self.tmpdir) + self.tmpdir.cleanup() def test_simple(self): fname = '085682ed-6792-499d-a3ab-9aebd683c011.yaml' @@ -135,12 +139,16 @@ class testEntries(unittest.TestCase): class testEmptyCollection(unittest.TestCase): def setUp(self): - self.tmpdir = tempfile.mkdtemp() - utils.copytree('tests/data/empty', self.tmpdir, dirs_exist_ok=True) - self.collection = lesana.Collection(self.tmpdir) + self.tmpdir = tempfile.TemporaryDirectory() + utils.copytree( + 'tests/data/empty', + self.tmpdir.name, + dirs_exist_ok=True + ) + self.collection = lesana.Collection(self.tmpdir.name) def tearDown(self): - shutil.rmtree(self.tmpdir) + self.tmpdir.cleanup() def test_loaded(self): self.assertEqual(self.collection.settings, {}) @@ -152,12 +160,16 @@ class testEmptyCollection(unittest.TestCase): class testSimpleCollection(unittest.TestCase): def setUp(self): - self.tmpdir = tempfile.mkdtemp() - utils.copytree('tests/data/simple', self.tmpdir, dirs_exist_ok=True) - self.collection = lesana.Collection(self.tmpdir) + self.tmpdir = tempfile.TemporaryDirectory() + utils.copytree( + 'tests/data/simple', + self.tmpdir.name, + dirs_exist_ok=True + ) + self.collection = lesana.Collection(self.tmpdir.name) def tearDown(self): - shutil.rmtree(self.tmpdir) + self.tmpdir.cleanup() def test_loaded(self): self.assertIsNotNone(self.collection.settings) @@ -301,7 +313,7 @@ class testSimpleCollection(unittest.TestCase): # TODO: make finding the templates less prone to breaking and # then remove the cwd change from here old_cwd = os.getcwd() - os.chdir(self.tmpdir) + os.chdir(self.tmpdir.name) data = { "name": "This is a name", } @@ -317,7 +329,7 @@ class testSimpleCollection(unittest.TestCase): # TODO: make finding the templates less prone to breaking and # then remove the cwd change from here old_cwd = os.getcwd() - os.chdir(self.tmpdir) + os.chdir(self.tmpdir.name) data = { "name": "This is a name", } @@ -341,7 +353,7 @@ class testSimpleCollection(unittest.TestCase): # TODO: make finding the templates less prone to breaking and # then remove the cwd change from here old_cwd = os.getcwd() - os.chdir(self.tmpdir) + os.chdir(self.tmpdir.name) data = { "name": "This is a name", } @@ -356,7 +368,7 @@ class testSimpleCollection(unittest.TestCase): # TODO: make finding the templates less prone to breaking and # then remove the cwd change from here old_cwd = os.getcwd() - os.chdir(self.tmpdir) + os.chdir(self.tmpdir.name) data = { "name": "This is a name", } @@ -370,12 +382,16 @@ class testSimpleCollection(unittest.TestCase): class testComplexCollection(unittest.TestCase): def setUp(self): - self.tmpdir = tempfile.mkdtemp() - utils.copytree('tests/data/complex', self.tmpdir, dirs_exist_ok=True) - self.collection = lesana.Collection(self.tmpdir) + self.tmpdir = tempfile.TemporaryDirectory() + utils.copytree( + 'tests/data/complex', + self.tmpdir.name, + dirs_exist_ok=True + ) + self.collection = lesana.Collection(self.tmpdir.name) def tearDown(self): - shutil.rmtree(self.tmpdir) + self.tmpdir.cleanup() def test_init(self): self.assertIsNotNone(self.collection.settings) @@ -567,18 +583,22 @@ class testComplexCollection(unittest.TestCase): class testCollectionWithErrors(unittest.TestCase): def setUp(self): - self.tmpdir = tempfile.mkdtemp() - utils.copytree('tests/data/wrong', self.tmpdir, dirs_exist_ok=True) - self.collection = lesana.Collection(self.tmpdir) + self.tmpdir = tempfile.TemporaryDirectory() + utils.copytree( + 'tests/data/wrong', + self.tmpdir.name, + dirs_exist_ok=True + ) + self.collection = lesana.Collection(self.tmpdir.name) def tearDown(self): - shutil.rmtree(self.tmpdir) + self.tmpdir.cleanup() def test_load_wrong_language(self): # We reload this collection, with an invalid value in lang, to # check that the log contains a warning. with self.assertLogs(level=logging.WARNING) as cm: - self.collection = lesana.Collection(self.tmpdir) + self.collection = lesana.Collection(self.tmpdir.name) self.assertEqual(len(cm.output), 2) self.assertIn("Invalid language", cm.output[1]) # The collection will default to english, but should still work. @@ -613,30 +633,33 @@ class testCollectionWithErrors(unittest.TestCase): class testCollectionCreation(unittest.TestCase): def setUp(self): - self.tmpdir = tempfile.mkdtemp() + self.tmpdir = tempfile.TemporaryDirectory() def tearDown(self): - shutil.rmtree(self.tmpdir) + self.tmpdir.cleanup() def test_init(self): - collection = lesana.Collection.init(self.tmpdir) + collection = lesana.Collection.init(self.tmpdir.name) self.assertIsInstance(collection, lesana.Collection) - self.assertTrue(os.path.isdir(os.path.join(self.tmpdir, '.git'))) - self.assertTrue(os.path.isdir(os.path.join(self.tmpdir, '.lesana'))) + self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, '.git'))) + self.assertTrue(os.path.isdir(os.path.join( + self.tmpdir.name, + '.lesana' + ))) self.assertTrue( - os.path.isfile(os.path.join(self.tmpdir, 'settings.yaml')) + os.path.isfile(os.path.join(self.tmpdir.name, 'settings.yaml')) ) self.assertTrue( - os.path.isfile(os.path.join(self.tmpdir, '.gitignore')) + os.path.isfile(os.path.join(self.tmpdir.name, '.gitignore')) ) checkout_hook = os.path.join( - self.tmpdir, + self.tmpdir.name, '.git', 'hooks', 'post-checkout', ) merge_hook = os.path.join( - self.tmpdir, + self.tmpdir.name, '.git', 'hooks', 'post-merge', @@ -648,17 +671,20 @@ class testCollectionCreation(unittest.TestCase): os.path.abspath(os.readlink(merge_hook)) ) # and then run it twice on the same directory, nothing should break - collection = lesana.Collection.init(self.tmpdir) + collection = lesana.Collection.init(self.tmpdir.name) self.assertIsInstance(collection, lesana.Collection) - self.assertTrue(os.path.isdir(os.path.join(self.tmpdir, '.git'))) - self.assertTrue(os.path.isdir(os.path.join(self.tmpdir, '.lesana'))) + self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, '.git'))) + self.assertTrue(os.path.isdir(os.path.join( + self.tmpdir.name, + '.lesana' + ))) self.assertTrue( - os.path.isfile(os.path.join(self.tmpdir, 'settings.yaml')) + os.path.isfile(os.path.join(self.tmpdir.name, 'settings.yaml')) ) self.assertTrue( - os.path.isfile(os.path.join(self.tmpdir, '.gitignore')) + os.path.isfile(os.path.join(self.tmpdir.name, '.gitignore')) ) - created = lesana.Collection(self.tmpdir) + created = lesana.Collection(self.tmpdir.name) self.assertTrue(created.settings['git']) def _do_nothing(*args, **kwargs): @@ -667,49 +693,64 @@ class testCollectionCreation(unittest.TestCase): def test_init_edit_file(self): collection = lesana.Collection.init( - self.tmpdir, edit_file=self._do_nothing + self.tmpdir.name, edit_file=self._do_nothing ) self.assertIsInstance(collection, lesana.Collection) - self.assertTrue(os.path.isdir(os.path.join(self.tmpdir, '.git'))) - self.assertTrue(os.path.isdir(os.path.join(self.tmpdir, '.lesana'))) + self.assertTrue(os.path.isdir(os.path.join(self.tmpdir.name, '.git'))) + self.assertTrue(os.path.isdir(os.path.join( + self.tmpdir.name, + '.lesana' + ))) self.assertTrue( - os.path.isfile(os.path.join(self.tmpdir, 'settings.yaml')) + os.path.isfile(os.path.join(self.tmpdir.name, 'settings.yaml')) ) self.assertTrue( - os.path.isfile(os.path.join(self.tmpdir, '.gitignore')) + os.path.isfile(os.path.join(self.tmpdir.name, '.gitignore')) ) def test_init_no_git(self): - collection = lesana.Collection.init(self.tmpdir, git_enabled=False) + collection = lesana.Collection.init( + self.tmpdir.name, + git_enabled=False + ) self.assertIsInstance(collection, lesana.Collection) - self.assertFalse(os.path.isdir(os.path.join(self.tmpdir, '.git'))) - self.assertTrue(os.path.isdir(os.path.join(self.tmpdir, '.lesana'))) + self.assertFalse(os.path.isdir(os.path.join(self.tmpdir.name, '.git'))) + self.assertTrue(os.path.isdir(os.path.join( + self.tmpdir.name, + '.lesana' + ))) self.assertTrue( - os.path.isfile(os.path.join(self.tmpdir, 'settings.yaml')) + os.path.isfile(os.path.join(self.tmpdir.name, 'settings.yaml')) ) self.assertFalse( - os.path.isfile(os.path.join(self.tmpdir, '.gitignore')) + os.path.isfile(os.path.join(self.tmpdir.name, '.gitignore')) ) # and then run it twice on the same directory, nothing should break - collection = lesana.Collection.init(self.tmpdir, git_enabled=False) + collection = lesana.Collection.init( + self.tmpdir.name, + git_enabled=False + ) self.assertIsInstance(collection, lesana.Collection) - self.assertFalse(os.path.isdir(os.path.join(self.tmpdir, '.git'))) - self.assertTrue(os.path.isdir(os.path.join(self.tmpdir, '.lesana'))) + self.assertFalse(os.path.isdir(os.path.join(self.tmpdir.name, '.git'))) + self.assertTrue(os.path.isdir(os.path.join( + self.tmpdir.name, + '.lesana' + ))) self.assertTrue( - os.path.isfile(os.path.join(self.tmpdir, 'settings.yaml')) + os.path.isfile(os.path.join(self.tmpdir.name, 'settings.yaml')) ) self.assertFalse( - os.path.isfile(os.path.join(self.tmpdir, '.gitignore')) + os.path.isfile(os.path.join(self.tmpdir.name, '.gitignore')) ) - created = lesana.Collection(self.tmpdir) + created = lesana.Collection(self.tmpdir.name) self.assertFalse(created.settings['git']) def test_deletion(self): - shutil.copy('tests/data/simple/settings.yaml', self.tmpdir) + shutil.copy('tests/data/simple/settings.yaml', self.tmpdir.name) utils.copytree( - 'tests/data/simple/items', os.path.join(self.tmpdir, 'items'), + 'tests/data/simple/items', os.path.join(self.tmpdir.name, 'items'), ) - collection = lesana.Collection.init(self.tmpdir) + collection = lesana.Collection.init(self.tmpdir.name) # We start with one item indexed with the term "another" collection.start_search('another') mset = collection._enquire.get_mset(0, 10) @@ -720,7 +761,7 @@ class testCollectionCreation(unittest.TestCase): self.assertFalse( os.path.exists( os.path.join( - self.tmpdir, + self.tmpdir.name, 'items', '11189ee47ddf4796b718a483b379f976.yaml', ) @@ -731,11 +772,11 @@ class testCollectionCreation(unittest.TestCase): self.assertEqual(mset.get_matches_estimated(), 0) def test_partial_eid_deletion(self): - shutil.copy('tests/data/simple/settings.yaml', self.tmpdir) + shutil.copy('tests/data/simple/settings.yaml', self.tmpdir.name) utils.copytree( - 'tests/data/simple/items', os.path.join(self.tmpdir, 'items'), + 'tests/data/simple/items', os.path.join(self.tmpdir.name, 'items'), ) - collection = lesana.Collection.init(self.tmpdir) + collection = lesana.Collection.init(self.tmpdir.name) # We start with one item indexed with the term "another" collection.start_search('another') mset = collection._enquire.get_mset(0, 10) @@ -746,7 +787,7 @@ class testCollectionCreation(unittest.TestCase): self.assertFalse( os.path.exists( os.path.join( - self.tmpdir, + self.tmpdir.name, 'items', '11189ee47ddf4796b718a483b379f976.yaml', ) @@ -765,13 +806,13 @@ class testCollectionCreation(unittest.TestCase): return found def test_git_adding(self): - shutil.copy('tests/data/simple/settings.yaml', self.tmpdir) + shutil.copy('tests/data/simple/settings.yaml', self.tmpdir.name) utils.copytree( - 'tests/data/simple/items', os.path.join(self.tmpdir, 'items'), + 'tests/data/simple/items', os.path.join(self.tmpdir.name, 'items'), ) - collection = lesana.Collection.init(self.tmpdir) + collection = lesana.Collection.init(self.tmpdir.name) fname = '11189ee47ddf4796b718a483b379f976.yaml' - repo = git.Repo(self.tmpdir) + repo = git.Repo(self.tmpdir.name) # By default, this collection doesn't have any git entry in the # settings (but there is a repo) collection.git_add_files([os.path.join(collection.itemdir, fname)]) @@ -788,7 +829,7 @@ class testCollectionCreation(unittest.TestCase): def test_init_custom_settings(self): collection = lesana.Collection.init( - self.tmpdir, + self.tmpdir.name, edit_file=self._do_nothing, settings={ 'name': 'A different name', @@ -800,7 +841,7 @@ class testCollectionCreation(unittest.TestCase): ) self.assertIsInstance(collection, lesana.Collection) self.assertTrue( - os.path.isfile(os.path.join(self.tmpdir, 'settings.yaml')) + os.path.isfile(os.path.join(self.tmpdir.name, 'settings.yaml')) ) self.assertEqual(collection.settings['name'], 'A different name') self.assertEqual(len(collection.settings['fields']), 2) diff --git a/tests/test_commands.py b/tests/test_commands.py index 83f5c5e..9cd5c88 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -57,7 +57,7 @@ class testCommandsSimple(unittest.TestCase, CommandsMixin): self._run_command(command.Index(), args) def tearDown(self): - pass + self.tmpdir.cleanup() def test_init(self): args = { @@ -216,7 +216,7 @@ class testCommandsComplex(unittest.TestCase, CommandsMixin): self._run_command(command.Index(), args) def tearDown(self): - pass + self.tmpdir.cleanup() def test_get_values_from_list(self): args = { diff --git a/tests/test_derivatives.py b/tests/test_derivatives.py index ec3ae65..ba9d79c 100644 --- a/tests/test_derivatives.py +++ b/tests/test_derivatives.py @@ -1,4 +1,3 @@ -import shutil import tempfile import unittest @@ -23,16 +22,16 @@ class Derivative(lesana.Collection): class testDerivatives(unittest.TestCase): def setUp(self): - self.tmpdir = tempfile.mkdtemp() + self.tmpdir = tempfile.TemporaryDirectory() utils.copytree( 'tests/data/derivative', - self.tmpdir, + self.tmpdir.name, dirs_exist_ok=True ) - self.collection = Derivative(self.tmpdir) + self.collection = Derivative(self.tmpdir.name) def tearDown(self): - shutil.rmtree(self.tmpdir) + self.tmpdir.cleanup() def test_load_subclasses(self): self.assertIsInstance(self.collection.fields['unknown'], DerivedType) |