Skip to content

Commit 7fe7045

Browse files
authored
fix: create deps folder before starting copy/move operation of dependencies (#329)
* fix: create deps folder before starting copy/move operation of dependencies * correct parent folder creation for both move and copy * add integration tests for changes * fix test cases * fix formatting
1 parent 5d0c5ee commit 7fe7045

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

aws_lambda_builders/actions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def execute(self):
136136
if os.path.isdir(dependencies_source):
137137
copytree(dependencies_source, new_destination)
138138
else:
139-
os.makedirs(os.path.dirname(dependencies_source), exist_ok=True)
139+
os.makedirs(os.path.dirname(new_destination), exist_ok=True)
140140
shutil.copy2(dependencies_source, new_destination)
141141

142142

@@ -162,6 +162,10 @@ def execute(self):
162162
dependencies_source = os.path.join(self.artifact_dir, name)
163163
new_destination = os.path.join(self.dest_dir, name)
164164

165+
# shutil.move can't create subfolders if this is the first file in that folder
166+
if os.path.isfile(dependencies_source):
167+
os.makedirs(os.path.dirname(new_destination), exist_ok=True)
168+
165169
shutil.move(dependencies_source, new_destination)
166170

167171

tests/integration/test_actions.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import os
2+
from pathlib import Path
3+
import tempfile
4+
from unittest import TestCase
5+
from parameterized import parameterized
6+
7+
8+
from aws_lambda_builders.actions import CopyDependenciesAction, MoveDependenciesAction
9+
from aws_lambda_builders.utils import copytree
10+
11+
12+
class TestCopyDependenciesAction(TestCase):
13+
@parameterized.expand(
14+
[
15+
("single_file",),
16+
("multiple_files",),
17+
("empty_subfolders",),
18+
]
19+
)
20+
def test_copy_dependencies_action(self, source_folder):
21+
curr_dir = Path(__file__).resolve().parent
22+
test_folder = os.path.join(curr_dir, "testdata", source_folder)
23+
with tempfile.TemporaryDirectory() as tmpdir:
24+
empty_source = os.path.join(tmpdir, "empty_source")
25+
target = os.path.join(tmpdir, "target")
26+
27+
os.mkdir(empty_source)
28+
29+
copy_dependencies_action = CopyDependenciesAction(empty_source, test_folder, target)
30+
copy_dependencies_action.execute()
31+
32+
self.assertEqual(os.listdir(test_folder), os.listdir(target))
33+
34+
35+
class TestMoveDependenciesAction(TestCase):
36+
@parameterized.expand(
37+
[
38+
("single_file",),
39+
("multiple_files",),
40+
("empty_subfolders",),
41+
]
42+
)
43+
def test_move_dependencies_action(self, source_folder):
44+
curr_dir = Path(__file__).resolve().parent
45+
test_folder = os.path.join(curr_dir, "testdata", source_folder)
46+
with tempfile.TemporaryDirectory() as tmpdir:
47+
test_source = os.path.join(tmpdir, "test_source")
48+
empty_source = os.path.join(tmpdir, "empty_source")
49+
target = os.path.join(tmpdir, "target")
50+
51+
os.mkdir(test_source)
52+
os.mkdir(empty_source)
53+
54+
copytree(test_folder, test_source)
55+
56+
move_dependencies_action = MoveDependenciesAction(empty_source, test_source, target)
57+
move_dependencies_action.execute()
58+
59+
self.assertEqual(os.listdir(test_folder), os.listdir(target))

tests/integration/testdata/empty_subfolders/sub_folder/sub_folder/test_file.txt

Whitespace-only changes.

tests/integration/testdata/multiple_files/sub_folder/test_file3.txt

Whitespace-only changes.

tests/integration/testdata/multiple_files/test_file.txt

Whitespace-only changes.

tests/integration/testdata/multiple_files/test_file2.txt

Whitespace-only changes.

tests/integration/testdata/single_file/test_file.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)