forked from aws/aws-lambda-builders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nodejs_npm.py
273 lines (212 loc) · 10 KB
/
test_nodejs_npm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import logging
import os
import shutil
import tempfile
from unittest import TestCase
import mock
from parameterized import parameterized
from aws_lambda_builders.builder import LambdaBuilder
from aws_lambda_builders.exceptions import WorkflowFailedError
logger = logging.getLogger("aws_lambda_builders.workflows.nodejs_npm.workflow")
class TestNodejsNpmWorkflow(TestCase):
"""
Verifies that `nodejs_npm` workflow works by building a Lambda using NPM
"""
TEST_DATA_FOLDER = os.path.join(os.path.dirname(__file__), "testdata")
def setUp(self):
self.artifacts_dir = tempfile.mkdtemp()
self.scratch_dir = tempfile.mkdtemp()
self.dependencies_dir = tempfile.mkdtemp()
self.no_deps = os.path.join(self.TEST_DATA_FOLDER, "no-deps")
self.builder = LambdaBuilder(language="nodejs", dependency_manager="npm", application_framework=None)
self.runtime = "nodejs12.x"
def tearDown(self):
shutil.rmtree(self.artifacts_dir)
shutil.rmtree(self.scratch_dir)
shutil.rmtree(self.dependencies_dir)
def test_builds_project_without_dependencies(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "no-deps")
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
)
expected_files = {"package.json", "included.js"}
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files, output_files)
def test_builds_project_without_manifest(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "no-manifest")
with mock.patch.object(logger, "warning") as mock_warning:
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
)
expected_files = {"app.js"}
output_files = set(os.listdir(self.artifacts_dir))
mock_warning.assert_called_once_with("package.json file not found. Continuing the build without dependencies.")
self.assertEqual(expected_files, output_files)
def test_builds_project_and_excludes_hidden_aws_sam(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "excluded-files")
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
)
expected_files = {"package.json", "included.js"}
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files, output_files)
def test_builds_project_with_remote_dependencies(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "npm-deps")
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
)
expected_files = {"package.json", "included.js", "node_modules"}
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files, output_files)
expected_modules = {"minimal-request-promise"}
output_modules = set(os.listdir(os.path.join(self.artifacts_dir, "node_modules")))
self.assertEqual(expected_modules, output_modules)
def test_builds_project_with_npmrc(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "npmrc")
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
)
expected_files = {"package.json", "included.js", "node_modules"}
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files, output_files)
expected_modules = {"fake-http-request"}
output_modules = set(os.listdir(os.path.join(self.artifacts_dir, "node_modules")))
self.assertEqual(expected_modules, output_modules)
@parameterized.expand(["package-lock", "shrinkwrap", "package-lock-and-shrinkwrap"])
def test_builds_project_with_lockfile(self, dir_name):
expected_files_common = {"package.json", "included.js", "node_modules"}
expected_files_by_dir_name = {
"package-lock": {"package-lock.json"},
"shrinkwrap": {"npm-shrinkwrap.json"},
"package-lock-and-shrinkwrap": {"package-lock.json", "npm-shrinkwrap.json"},
}
source_dir = os.path.join(self.TEST_DATA_FOLDER, dir_name)
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
)
expected_files = expected_files_common.union(expected_files_by_dir_name[dir_name])
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files, output_files)
def test_fails_if_npm_cannot_resolve_dependencies(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "broken-deps")
with self.assertRaises(WorkflowFailedError) as ctx:
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
)
self.assertIn("No matching version found for minimal-request-promise@0.0.0-NON_EXISTENT", str(ctx.exception))
def test_fails_if_package_json_is_broken(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "broken-package")
with self.assertRaises(WorkflowFailedError) as ctx:
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
)
self.assertIn("NodejsNpmBuilder:ParseManifest", str(ctx.exception))
def test_builds_project_with_remote_dependencies_without_download_dependencies_with_dependencies_dir(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "npm-deps")
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
dependencies_dir=self.dependencies_dir,
download_dependencies=False,
)
expected_files = {"package.json", "included.js"}
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files, output_files)
def test_builds_project_with_remote_dependencies_with_download_dependencies_and_dependencies_dir(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "npm-deps")
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
dependencies_dir=self.dependencies_dir,
download_dependencies=True,
)
expected_files = {"package.json", "included.js", "node_modules"}
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files, output_files)
expected_modules = {"minimal-request-promise"}
output_modules = set(os.listdir(os.path.join(self.artifacts_dir, "node_modules")))
self.assertEqual(expected_modules, output_modules)
expected_modules = {"minimal-request-promise"}
output_modules = set(os.listdir(os.path.join(self.dependencies_dir, "node_modules")))
self.assertEqual(expected_modules, output_modules)
expected_dependencies_files = {"node_modules"}
output_dependencies_files = set(os.listdir(os.path.join(self.dependencies_dir)))
self.assertNotIn(expected_dependencies_files, output_dependencies_files)
def test_builds_project_with_remote_dependencies_without_download_dependencies_without_dependencies_dir(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "npm-deps")
with mock.patch.object(logger, "info") as mock_info:
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
dependencies_dir=None,
download_dependencies=False,
)
expected_files = {"package.json", "included.js"}
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files, output_files)
mock_info.assert_called_with(
"download_dependencies is False and dependencies_dir is None. Copying the source files into the "
"artifacts directory. "
)
def test_builds_project_without_combine_dependencies(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "npm-deps")
self.builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "package.json"),
runtime=self.runtime,
dependencies_dir=self.dependencies_dir,
download_dependencies=True,
combine_dependencies=False,
)
expected_files = {"package.json", "included.js"}
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files, output_files)
expected_modules = "minimal-request-promise"
output_modules = set(os.listdir(os.path.join(self.dependencies_dir, "node_modules")))
self.assertIn(expected_modules, output_modules)
expected_dependencies_files = {"node_modules"}
output_dependencies_files = set(os.listdir(os.path.join(self.dependencies_dir)))
self.assertNotIn(expected_dependencies_files, output_dependencies_files)