From c7df4bd0ba5e34018a447021d96a1a155374a4be Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Mon, 7 Aug 2017 01:27:19 +0300 Subject: [PATCH] tests: add test for utils.py Some sanity tests to make sure something like #132 doesn't happen anymore. Signed-off-by: Ruslan Kuprieiev --- tests/test_utils.py | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/test_utils.py diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 000000000..0ce5b9e09 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,60 @@ +import os +import shutil +import filecmp +from random import randint +from time import sleep + +from dvc import utils +from tests.basic_env import DirHierarchyEnvironment + +class TestExecutor(DirHierarchyEnvironment): + def setUp(self): + pass + + def tearDown(self): + pass + + def test_rmtree(self): + root = 'testdir' + + os.makedirs(root + '/subdir') + with open(root + '/file1', 'w+') as f: + f.write('file1contents') + with open(root + '/subdir/file2', 'w+') as f: + f.write('file2contents') + + utils.rmtree(root) + self.assertFalse(os.path.exists(root)) + + def test_copyfile(self): + src = 'file1' + dest = 'file2' + dest_dir = 'testdir' + + with open(src, 'w+') as f: + f.write('file1contents') + + os.mkdir(dest_dir) + + utils.copyfile(src, dest) + self.assertTrue(filecmp.cmp(src, dest)) + + utils.copyfile(src, dest_dir) + self.assertTrue(filecmp.cmp(src, '{}/{}'.format(dest_dir, src))) + + shutil.rmtree(dest_dir) + os.remove(src) + os.remove(dest) + + def test_map_progress(self): + def f(target): + sleep(randint(1,2)) + with open(target, 'w+') as o: + o.write(target) + sleep(randint(1,2)) + + targets = ['map{}'.format(i) for i in range(1, 10)] + n_threads = [1, 10, 20] + + for n in n_threads: + utils.map_progress(f, targets, n)