-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(tests): Node build in source test #6132
Merged
lucashuy
merged 10 commits into
aws:feat/build-in-source
from
lucashuy:node_build_in_source_test
Oct 31, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1c1e748
Added nodejs tests
lucashuy 11eacdf
make format
lucashuy e707b58
Enable retries
lucashuy 2cfd02f
Merge branch 'feat/build-in-source' of github.com:aws/aws-sam-cli int…
lucashuy 0ef8a74
Run build test inside of a scratch directory
lucashuy 5f45031
make format
lucashuy 1b8a570
Updated Makefile test to properly use scratch directory and moved sou…
lucashuy edd438d
Changed copytree call to in house version
lucashuy 0a33294
Removed redundant parent method calling
lucashuy 6b5915e
make format
lucashuy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
import pytest | ||
import logging | ||
from unittest import skip | ||
from parameterized import parameterized | ||
from samcli.lib.utils import osutils | ||
|
||
from tests.integration.buildcmd.build_integ_base import BuildIntegProvidedBase, BuildIntegEsbuildBase | ||
from tests.integration.buildcmd.build_integ_base import ( | ||
BuildIntegNodeBase, | ||
BuildIntegProvidedBase, | ||
BuildIntegEsbuildBase, | ||
) | ||
from tests.testing_utils import run_command | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
@@ -14,18 +19,18 @@ class TestBuildCommand_BuildInSource_Makefile(BuildIntegProvidedBase): | |
template = "template.yaml" | ||
is_nested_parent = False | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.code_uri = "provided_create_new_file" | ||
cls.code_uri_path = os.path.join(cls.test_data_path, cls.code_uri) | ||
cls.file_created_from_make_command = "file-created-from-make-command.txt" | ||
def setUp(self): | ||
super().setUp() | ||
|
||
def tearDown(self): | ||
super().tearDown() | ||
new_file_in_codeuri_path = os.path.join(self.code_uri_path, self.file_created_from_make_command) | ||
if os.path.isfile(new_file_in_codeuri_path): | ||
os.remove(new_file_in_codeuri_path) | ||
self.code_uri = "provided_create_new_file" | ||
test_data_code_uri = Path(self.test_data_path, self.code_uri) | ||
self.file_created_from_make_command = "file-created-from-make-command.txt" | ||
|
||
scratch_code_uri_path = Path(self.working_dir, self.code_uri) | ||
self.code_uri_path = str(scratch_code_uri_path) | ||
|
||
# copy source code into temporary directory and update code uri to that scratch dir | ||
osutils.copytree(test_data_code_uri, scratch_code_uri_path) | ||
|
||
@parameterized.expand( | ||
[ | ||
|
@@ -40,7 +45,7 @@ def test_builds_successfully_with_makefile(self, build_in_source, new_file_shoul | |
runtime="provided.al2", | ||
use_container=False, | ||
manifest=None, | ||
code_uri=self.code_uri, | ||
code_uri=self.code_uri_path, | ||
build_in_source=build_in_source, | ||
) | ||
|
||
|
@@ -55,13 +60,9 @@ class TestBuildCommand_BuildInSource_Esbuild(BuildIntegEsbuildBase): | |
|
||
def setUp(self): | ||
super().setUp() | ||
self.source_directories = [] | ||
|
||
def tearDown(self): | ||
super().tearDown() | ||
# clean up dependencies installed in source directories | ||
for source in self.source_directories: | ||
shutil.rmtree(os.path.join(source, "node_modules"), ignore_errors=True) | ||
source_files_path = Path(self.test_data_path, "Esbuild") | ||
osutils.copytree(source_files_path, self.working_dir) | ||
|
||
@parameterized.expand( | ||
[ | ||
|
@@ -72,12 +73,11 @@ def tearDown(self): | |
) | ||
@pytest.mark.flaky(reruns=3) | ||
def test_builds_successfully_without_local_dependencies(self, build_in_source, dependencies_expected_in_source): | ||
codeuri = os.path.join(self.test_data_path, "Esbuild", "Node") | ||
self.source_directories = [codeuri] | ||
codeuri = os.path.join(self.working_dir, "Node") | ||
|
||
self._test_with_default_package_json( | ||
build_in_source=build_in_source, | ||
runtime="nodejs16.x", | ||
runtime="nodejs18.x", | ||
code_uri=codeuri, | ||
handler="main.lambdaHandler", | ||
architecture="x86_64", | ||
|
@@ -90,9 +90,8 @@ def test_builds_successfully_without_local_dependencies(self, build_in_source, d | |
|
||
@pytest.mark.flaky(reruns=3) | ||
def test_builds_successfully_with_local_dependency(self): | ||
codeuri = os.path.join(self.test_data_path, "Esbuild", "NodeWithLocalDependency") | ||
self.source_directories = [codeuri] | ||
runtime = "nodejs16.x" | ||
codeuri = os.path.join(self.working_dir, "NodeWithLocalDependency") | ||
runtime = "nodejs18.x" | ||
architecture = "x86_64" | ||
|
||
self._test_with_default_package_json( | ||
|
@@ -107,3 +106,57 @@ def test_builds_successfully_with_local_dependency(self): | |
|
||
# check whether dependencies were installed in source dir | ||
self.assertEqual(os.path.isdir(os.path.join(codeuri, "node_modules")), True) | ||
|
||
|
||
class TestBuildCommand_BuildInSource_Nodejs(BuildIntegNodeBase): | ||
is_nested_parent = False | ||
template = "template.yaml" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
|
||
osutils.copytree(Path(self.test_data_path, "Esbuild"), self.working_dir) | ||
|
||
def tearDown(self): | ||
super().tearDown() | ||
Comment on lines
+120
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, no need to override a method if we are just going to call the parent impl. |
||
|
||
def validate_node_modules(self, is_build_in_source_behaviour: bool): | ||
# validate if node modules exist in the built artifact dir | ||
built_node_modules = Path(self.default_build_dir, "Function", "node_modules") | ||
self.assertEqual(built_node_modules.is_dir(), True, "node_modules not found in artifact dir") | ||
self.assertEqual(built_node_modules.is_symlink(), is_build_in_source_behaviour) | ||
|
||
# validate that node modules are suppose to exist in the source dir | ||
source_node_modules = Path(self.codeuri_path, "node_modules") | ||
self.assertEqual(source_node_modules.is_dir(), is_build_in_source_behaviour) | ||
|
||
@parameterized.expand( | ||
[ | ||
(True, True), # build in source | ||
(False, False), # don't build in source | ||
(None, False), # use default for workflow (don't build in source) | ||
] | ||
) | ||
@pytest.mark.flaky(reruns=3) | ||
def test_builds_successfully_without_local_dependencies(self, build_in_source, expected_built_in_source): | ||
self.codeuri_path = Path(self.working_dir, "Node") | ||
|
||
overrides = self.get_override( | ||
runtime="nodejs18.x", code_uri=self.codeuri_path, architecture="x86_64", handler="main.lambdaHandler" | ||
) | ||
command_list = self.get_command_list(build_in_source=build_in_source, parameter_overrides=overrides, debug=True) | ||
|
||
run_command(command_list, self.working_dir) | ||
self.validate_node_modules(expected_built_in_source) | ||
|
||
@pytest.mark.flaky(reruns=3) | ||
def test_builds_successfully_with_local_dependency(self): | ||
self.codeuri_path = Path(self.working_dir, "NodeWithLocalDependency") | ||
|
||
overrides = self.get_override( | ||
runtime="nodejs18.x", code_uri=self.codeuri_path, architecture="x86_64", handler="main.lambdaHandler" | ||
) | ||
command_list = self.get_command_list(build_in_source=True, parameter_overrides=overrides) | ||
|
||
run_command(command_list, self.working_dir) | ||
self.validate_node_modules(True) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Aren't we copying test files into scratch dir for all build tests? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The base class (
BuildIntegBase
) doesn't appear to be copying any test files, it looks to just create the scratch directories.