Skip to content

Commit

Permalink
Python2 regression.
Browse files Browse the repository at this point in the history
  • Loading branch information
cBiscuitSurprise committed Dec 10, 2020
1 parent f5d1a2f commit 48ff701
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
14 changes: 10 additions & 4 deletions aws_lambda_builders/workflows/nodejs_npm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def extract_tarfile(self, tarfile_path, unpack_dir):
def file_exists(self, filename):
return os.path.isfile(filename)

def filename(self, filename):
return os.path.basename(filename)

def joinpath(self, *args):
return os.path.join(*args)

Expand Down Expand Up @@ -142,13 +145,15 @@ def package_local_dependency(
dependency_tarfile_path = DependencyUtils.package_local_dependency(
package_path, dep_path, dep_artifacts_dir, dep_scratch_dir, output_dir, osutils, subprocess_npm
)
dependency_tarfile_path = osutils.copy_file(dependency_tarfile_path, output_dir)

LOG.debug("NODEJS packed localized child dependency to %s", dependency_tarfile_path)
packaged_dependency_tarfile_path = osutils.joinpath(output_dir, osutils.filename(dependency_tarfile_path))
osutils.copy_file(dependency_tarfile_path, output_dir)

LOG.debug("NODEJS packed localized child dependency to %s", packaged_dependency_tarfile_path)

LOG.debug("NODEJS updating package.json %s", local_manifest_path)

DependencyUtils.update_manifest(local_manifest_path, dep_name, dependency_tarfile_path, osutils)
DependencyUtils.update_manifest(local_manifest_path, dep_name, packaged_dependency_tarfile_path, osutils)

if not top_level:
localized_package_dir = osutils.joinpath(artifacts_dir, "package")
Expand All @@ -167,7 +172,8 @@ def update_manifest(manifest_path, dep_name, dependency_tarfile_path, osutils):
Helper function to update dependency path to localized tar
"""

manifest_backup = osutils.copy_file(manifest_path, "{}.bak".format(manifest_path))
manifest_backup = "{}.bak".format(manifest_path)
osutils.copy_file(manifest_path, manifest_backup)

with osutils.open_file(manifest_backup, "r") as manifest_backup_file:
manifest = json.loads(manifest_backup_file.read())
Expand Down
47 changes: 28 additions & 19 deletions tests/functional/workflows/nodejs_npm/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,42 +127,51 @@ def test_popen_can_accept_cwd(self):
def test_dir_exists(self):
self.assertFalse(self.osutils.dir_exists("20201210_some_directory_that_should_not_exist"))

with tempfile.TemporaryDirectory() as temp_dir:
self.assertTrue(self.osutils.dir_exists(temp_dir))
temp_dir = tempfile.mkdtemp()

self.assertTrue(self.osutils.dir_exists(temp_dir))

shutil.rmtree(temp_dir)

def test_mkdir_makes_directory(self):
dir_to_create = os.path.join(
tempfile._get_default_tempdir(), next(tempfile._get_candidate_names())
) # pylint: disable=protected-access
dir_to_create = os.path.join(tempfile.gettempdir(), "20201210_some_directory_that_should_not_exist")
self.assertFalse(os.path.isdir(dir_to_create))

self.osutils.mkdir(dir_to_create)

self.assertTrue(os.path.isdir(dir_to_create))

shutil.rmtree(dir_to_create)

def test_open_file_opens_file_for_reading(self):
with tempfile.TemporaryDirectory() as temp_dir:
file_to_open = os.path.join(temp_dir, "test_open.txt")
temp_dir = tempfile.mkdtemp()

file_to_open = os.path.join(temp_dir, "test_open.txt")

with open(file_to_open, "w") as fid:
fid.write("this is text")
with open(file_to_open, "w") as fid:
fid.write("this is text")

with self.osutils.open_file(file_to_open) as fid:
content = fid.read()
with self.osutils.open_file(file_to_open) as fid:
content = fid.read()

self.assertEqual("this is text", content)
self.assertEqual("this is text", content)

shutil.rmtree(temp_dir)

def test_open_file_opens_file_for_writing(self):
with tempfile.TemporaryDirectory() as temp_dir:
file_to_open = os.path.join(temp_dir, "test_open.txt")
temp_dir = tempfile.mkdtemp()

file_to_open = os.path.join(temp_dir, "test_open.txt")

with self.osutils.open_file(file_to_open, "w") as fid:
fid.write("this is some other text")

with self.osutils.open_file(file_to_open, "w") as fid:
fid.write("this is some other text")
with self.osutils.open_file(file_to_open) as fid:
content = fid.read()

with self.osutils.open_file(file_to_open) as fid:
content = fid.read()
self.assertEqual("this is some other text", content)

self.assertEqual("this is some other text", content)
shutil.rmtree(temp_dir)


class TestDependencyUtils(TestCase):
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/workflows/nodejs_npm/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from json import dumps
from os.path import basename
from unittest import TestCase
from mock import patch

Expand Down Expand Up @@ -72,6 +73,7 @@ def test_tars_and_unpacks_npm_project(self, OSUtilMock, SubprocessNpmMock):
osutils.dirname.side_effect = lambda value: "/dir:{}".format(value)
osutils.abspath.side_effect = lambda value: "/abs:{}".format(value)
osutils.joinpath.side_effect = lambda *args: "/".join(args)
osutils.filename.side_effect = lambda path: basename(path)
osutils.open_file.side_effect = MockOpener().open

subprocess_npm.run.return_value = "package.tar"
Expand Down Expand Up @@ -118,6 +120,7 @@ def test_tars_and_unpacks_local_dependencies(self, OSUtilMock, SubprocessNpmMock
osutils.dirname.side_effect = lambda value: "/dir:{}".format(value)
osutils.abspath.side_effect = lambda value: "/abs:{}".format(value)
osutils.joinpath.side_effect = lambda *args: "/".join(args)
osutils.filename.side_effect = lambda path: basename(path)
osutils.open_file.side_effect = MockOpener(file_open_responses).open

subprocess_npm.run.return_value = "package.tar"
Expand Down

0 comments on commit 48ff701

Please sign in to comment.