From 714c30e429dab8083b9e5317209e44619d875d22 Mon Sep 17 00:00:00 2001
From: Elena ``of Valhalla'' Grandi <valhalla@trueelena.org>
Date: Mon, 7 Dec 2020 13:49:26 +0100
Subject: Test compatibility with python < 3.8

---
 tests/test_collection.py  | 17 +++++++++--------
 tests/test_derivatives.py |  3 ++-
 tests/utils.py            | 18 ++++++++++++++++++
 3 files changed, 29 insertions(+), 9 deletions(-)
 create mode 100644 tests/utils.py

(limited to 'tests')

diff --git a/tests/test_collection.py b/tests/test_collection.py
index 6a65ff3..4aa0dfe 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -8,12 +8,13 @@ import git
 import ruamel.yaml
 
 import lesana
+from . import utils
 
 
 class testEntries(unittest.TestCase):
     def setUp(self):
         self.tmpdir = tempfile.mkdtemp()
-        shutil.copytree('tests/data/simple', self.tmpdir, dirs_exist_ok=True)
+        utils.copytree('tests/data/simple', self.tmpdir, dirs_exist_ok=True)
         self.collection = lesana.Collection(self.tmpdir)
         self.basepath = self.collection.itemdir
         self.filenames = []
@@ -120,7 +121,7 @@ class testEntries(unittest.TestCase):
 class testEmptyCollection(unittest.TestCase):
     def setUp(self):
         self.tmpdir = tempfile.mkdtemp()
-        shutil.copytree('tests/data/empty', self.tmpdir, dirs_exist_ok=True)
+        utils.copytree('tests/data/empty', self.tmpdir, dirs_exist_ok=True)
         self.collection = lesana.Collection(self.tmpdir)
 
     def tearDown(self):
@@ -136,7 +137,7 @@ class testEmptyCollection(unittest.TestCase):
 class testSimpleCollection(unittest.TestCase):
     def setUp(self):
         self.tmpdir = tempfile.mkdtemp()
-        shutil.copytree('tests/data/simple', self.tmpdir, dirs_exist_ok=True)
+        utils.copytree('tests/data/simple', self.tmpdir, dirs_exist_ok=True)
         self.collection = lesana.Collection(self.tmpdir)
 
     def tearDown(self):
@@ -261,7 +262,7 @@ class testSimpleCollection(unittest.TestCase):
 class testComplexCollection(unittest.TestCase):
     def setUp(self):
         self.tmpdir = tempfile.mkdtemp()
-        shutil.copytree('tests/data/complex', self.tmpdir, dirs_exist_ok=True)
+        utils.copytree('tests/data/complex', self.tmpdir, dirs_exist_ok=True)
         self.collection = lesana.Collection(self.tmpdir)
 
     def tearDown(self):
@@ -360,7 +361,7 @@ class testComplexCollection(unittest.TestCase):
 class testCollectionWithErrors(unittest.TestCase):
     def setUp(self):
         self.tmpdir = tempfile.mkdtemp()
-        shutil.copytree('tests/data/wrong', self.tmpdir, dirs_exist_ok=True)
+        utils.copytree('tests/data/wrong', self.tmpdir, dirs_exist_ok=True)
         self.collection = lesana.Collection(self.tmpdir)
 
     def tearDown(self):
@@ -480,7 +481,7 @@ class testCollectionCreation(unittest.TestCase):
 
     def test_deletion(self):
         shutil.copy('tests/data/simple/settings.yaml', self.tmpdir)
-        shutil.copytree(
+        utils.copytree(
             'tests/data/simple/items', os.path.join(self.tmpdir, 'items'),
         )
         collection = lesana.Collection.init(self.tmpdir)
@@ -506,7 +507,7 @@ class testCollectionCreation(unittest.TestCase):
 
     def test_partial_eid_deletion(self):
         shutil.copy('tests/data/simple/settings.yaml', self.tmpdir)
-        shutil.copytree(
+        utils.copytree(
             'tests/data/simple/items', os.path.join(self.tmpdir, 'items'),
         )
         collection = lesana.Collection.init(self.tmpdir)
@@ -540,7 +541,7 @@ class testCollectionCreation(unittest.TestCase):
 
     def test_git_adding(self):
         shutil.copy('tests/data/simple/settings.yaml', self.tmpdir)
-        shutil.copytree(
+        utils.copytree(
             'tests/data/simple/items', os.path.join(self.tmpdir, 'items'),
         )
         collection = lesana.Collection.init(self.tmpdir)
diff --git a/tests/test_derivatives.py b/tests/test_derivatives.py
index f79123c..0218f3d 100644
--- a/tests/test_derivatives.py
+++ b/tests/test_derivatives.py
@@ -4,6 +4,7 @@ import unittest
 
 import lesana
 from lesana import types
+from . import utils
 
 
 class DerivedType(types.LesanaString):
@@ -22,7 +23,7 @@ class Derivative(lesana.Collection):
 class testDerivatives(unittest.TestCase):
     def setUp(self):
         self.tmpdir = tempfile.mkdtemp()
-        shutil.copytree(
+        utils.copytree(
             'tests/data/derivative',
             self.tmpdir,
             dirs_exist_ok=True
diff --git a/tests/utils.py b/tests/utils.py
new file mode 100644
index 0000000..a56a120
--- /dev/null
+++ b/tests/utils.py
@@ -0,0 +1,18 @@
+import shutil
+import sys
+
+
+def copytree(src, dest, dirs_exist_ok=False):
+    """
+    Helper function to remove existing directories
+
+    Used in the tests for compatibility with python < 3.8
+    """
+    if sys.version_info >= (3, 8):
+        shutil.copytree(src, dest, dirs_exist_ok=dirs_exist_ok)
+    else:
+        if dirs_exist_ok:
+            if not dest.startswith('/tmp'):
+                raise ValueError("Refusing to delete a directory outside /tmp")
+            shutil.rmtree(dest)
+        shutil.copytree(src, dest)
-- 
cgit v1.2.3