|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import shutil |
| 4 | + |
| 5 | +from unittest import TestCase |
| 6 | + |
| 7 | +from aws_lambda_builders.utils import copytree |
| 8 | + |
| 9 | +class TestCopyTree(TestCase): |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + self.source = tempfile.mkdtemp() |
| 13 | + self.dest = tempfile.mkdtemp() |
| 14 | + |
| 15 | + def tearDown(self): |
| 16 | + shutil.rmtree(self.source) |
| 17 | + shutil.rmtree(self.dest) |
| 18 | + |
| 19 | + def test_must_copy_files_recursively(self): |
| 20 | + file(self.source, "a", "file.txt") |
| 21 | + file(self.source, "a", "b", "file.txt") |
| 22 | + file(self.source, "a", "c", "file.txt") |
| 23 | + |
| 24 | + copytree(self.source, self.dest) |
| 25 | + self.assertTrue(os.path.exists(os.path.join(self.dest, "a", "file.txt"))) |
| 26 | + self.assertTrue(os.path.exists(os.path.join(self.dest, "a", "b", "file.txt"))) |
| 27 | + self.assertTrue(os.path.exists(os.path.join(self.dest, "a", "c", "file.txt"))) |
| 28 | + |
| 29 | + def test_must_respect_excludes_list(self): |
| 30 | + file(self.source, ".git", "file.txt") |
| 31 | + file(self.source, "nested", ".aws-sam", "file.txt") |
| 32 | + file(self.source, "main.pyc") |
| 33 | + file(self.source, "a", "c", "file.txt") |
| 34 | + |
| 35 | + excludes = [".git", ".aws-sam", "*.pyc"] |
| 36 | + |
| 37 | + copytree(self.source, self.dest, ignore=shutil.ignore_patterns(*excludes)) |
| 38 | + self.assertEquals(set(os.listdir(self.dest)), {"nested", "a"}) |
| 39 | + self.assertEquals(set(os.listdir(os.path.join(self.dest, "nested"))), set()) |
| 40 | + self.assertEquals(set(os.listdir(os.path.join(self.dest, "a"))), {"c"}) |
| 41 | + self.assertEquals(set(os.listdir(os.path.join(self.dest, "a"))), {"c"}) |
| 42 | + |
| 43 | +def file(*args): |
| 44 | + path = os.path.join(*args) |
| 45 | + basedir = os.path.dirname(path) |
| 46 | + if not os.path.exists(basedir): |
| 47 | + os.makedirs(basedir) |
| 48 | + |
| 49 | + # empty file |
| 50 | + open(path, 'a').close() |
0 commit comments