Skip to content

Commit e7d5bc9

Browse files
monkutgaborschulzstorkwranglerCopilot
authored
🔥 🔧 Remove/depreciated pkg_resource usage and other depreciated usage (#1384)
* Add Support for Graviton 2 / ARM Architecture * Changed parameter in README to architecture * Removed redundant blank line * Bugfix + black compliance * Fix: architecture argument not used properly * Fixed black compliance * Fixed architecture label for wheels, added check for invalid architecture * Testing architecture * Added tests for ARM64 architecture * Only allow ARM64 arch if Python version >= 3.8 * Removed redundant references to architecture * Removed redundant references to architecture * Added test for default architecture setting * added .pre-commit hook for black-check and isort-check * Fixed test failure in Python 3.7 * 🔧 re-work regex for abi3 * 🔧 fix Zappa.create_lambda_function to pass a list to 'Architectures' in `create_function()` call. * 🔧 replace usage of os.listdir()/os.path.join() with pathlib.Path object usage. ✅ update * :whtie_check_mark: Update `test_copy_editable_packages` testcase for update to use Path objects * 🔥 remove unnecessary comments * 🔧 replace (depreciated) click.BaseCommand usage with click.Command * 🔥 remove bad json 'comma' * 🔧 fix typeerror 'list' object is not callable in `get_deps_list()` 🔧 replace datetime.utcnow() -> datetime.now(timezone.utc) * 🔧 fix bad reference in get_deps_list() * 🔧 update ci.yml to use pipenv in place of manual venv creation/prep in attempt to resolve `Run .venv/bin/coveralls --service=github` ImportError: cannot import name 'Reporter' from 'coverage.report' error. * 🔧 add install `pipenv` step to ci.yml * 🔧 refactor `test_zappa_init` and `cli.py` to allow cleaner mocking of ZappaCLI user inputs. * Update tests/tests.py add clear prefix for temporarydirectory Co-authored-by: Copilot <[email protected]> * 🔧 replace os.path related items with pathlib Path usage 🔧 change print() to LOG statement * 🔧 attempt to resolve coveralls error * 🔧 attempt to migrate to coveralls github action * 🔧 add carryforward * 🔧 explicitly set github-token for coverallapp/github-action * 🔧 turn on coverallsapp/github-action@v2 debug * 🔧 add --cov-report=xml in Makefile tests command 🔧 add `format: python` to ci.yml for coveralls * 🔧 change coveralls format from python -> cobertura * 🔧 add missing line-feed slash * 🔥 remove commented out "old coveralls support" --------- Co-authored-by: DS992 <> Co-authored-by: Gabor Schulz <[email protected]> Co-authored-by: shane <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent c2a8e20 commit e7d5bc9

File tree

8 files changed

+416
-347
lines changed

8 files changed

+416
-347
lines changed

.github/workflows/ci.yml

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,34 @@ jobs:
2626
restore-keys: ${{ runner.os }}-pip${{ matrix.python }}-
2727
- name: make sure we have version tags
2828
run: git fetch --unshallow --tags
29-
- name: Setup Virtualenv
30-
run: python -m venv .venv
31-
- name: Install
32-
run: source .venv/bin/activate && make requirements && python setup.py install
33-
- name: Lint
34-
run: source .venv/bin/activate && make flake black-check isort-check
29+
- name: Install pipenv
30+
run: python -m pip install --upgrade pip pipenv
31+
- name: Setup/install dependencies (pipenv)
32+
run: python -m pipenv install --dev
33+
- name: Lint (flake/black/isort)
34+
run: pipenv run make flake black-check isort-check
3535
- name: Test
36-
run: source .venv/bin/activate && make tests
37-
- name: Upload Coverage
38-
run: .venv/bin/coveralls --service=github
39-
env:
40-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41-
COVERALLS_FLAG_NAME: py${{ matrix.python }}
42-
COVERALLS_PARALLEL: true
43-
COVERALLS_SERVICE_JOB_ID: ${{ github.run_id }}
36+
run: pipenv run make tests
37+
- name: Coveralls Parallel
38+
uses: coverallsapp/github-action@v2
39+
with:
40+
github-token: ${{ secrets.GITHUB_TOKEN }}
41+
flag-name: py${{ matrix.python }}
42+
parallel: true
43+
format: cobertura
44+
debug: true
4445

4546
coverage:
4647
needs: test
4748
runs-on: ubuntu-latest
4849
steps:
4950
- name: Set up Python
5051
uses: actions/setup-python@v5
51-
- name: Finalise Coverage
52-
run: pip3 install --upgrade coveralls && coveralls --service=github --finish
53-
env:
54-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52+
- name: Coveralls Finished
53+
uses: coverallsapp/github-action@v2
54+
with:
55+
github-token: ${{ secrets.GITHUB_TOKEN }}
56+
parallel-finished: true
57+
format: cobertura
58+
carryforward: "run-3.9,run-3.10,run-3.11,run-3.12,run-3.13"
59+
debug: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,5 +262,6 @@ Temporary Items
262262
##########################
263263

264264
*.zip
265+
*-slim-handler-*.tar.gz
265266
README.test.md
266267
zappa_settings.json

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ tests:
9191
tests/tests_async.py \
9292
tests/tests.py \
9393
tests/tests_utilities.py \
94-
--cov=zappa
94+
--cov=zappa \
95+
--cov-report=xml \
9596
--durations=0
9697

tests/tests.py

Lines changed: 93 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf8 -*-
21
import base64
32
import collections
43
import hashlib
@@ -25,6 +24,7 @@
2524
import botocore.stub
2625
import flask
2726
import mock
27+
import pytest
2828
from click.exceptions import ClickException
2929
from click.globals import resolve_color_default
3030
from packaging import version
@@ -308,34 +308,35 @@ def test_get_exclude_glob__file_not_deleted(self):
308308

309309
def test_getting_installed_packages(self, *args):
310310
z = Zappa(runtime="python3.8")
311+
sample_package = mock.MagicMock()
312+
sample_package.name = "super_package"
313+
sample_package.version = "0.1"
314+
sample_package.locate_file = mock.MagicMock()
315+
sample_package.locate_file.return_value = "/venv/site-packages"
311316

312-
# mock pkg_resources call to be same as what our mocked site packages dir has
313-
mock_package = collections.namedtuple("mock_package", ["project_name", "version", "location"])
314-
mock_pip_installed_packages = [mock_package("super_package", "0.1", "/venv/site-packages")]
317+
site_packages = mock.MagicMock()
318+
site_packages.is_dir = mock.MagicMock(return_value=True)
319+
site_packages.glob = mock.MagicMock(return_value=[Path("/venv/site-packages/super_package")])
315320

316-
with mock.patch("os.path.isdir", return_value=True):
317-
with mock.patch("os.listdir", return_value=["super_package"]):
318-
import pkg_resources # this gets called in non-test Zappa mode
319-
320-
with mock.patch("pkg_resources.WorkingSet", return_value=mock_pip_installed_packages):
321-
self.assertDictEqual(z.get_installed_packages("", ""), {"super_package": "0.1"})
321+
mock_pip_installed_packages = [sample_package]
322+
with mock.patch("importlib.metadata.distributions", return_value=mock_pip_installed_packages):
323+
self.assertDictEqual(z.get_installed_packages(site_packages=site_packages), {"super_package": "0.1"})
322324

323325
def test_get_current_venv(self, *args):
324326
z = Zappa()
325327

326-
expected = "/expected/versions/path"
327-
328328
# VIRTUL_ENV test
329+
expected = "/expected/versions/path"
329330
os_env = {"VIRTUAL_ENV": expected}
330331
with mock.patch.dict("os.environ", os_env):
331332
current_venv = z.get_current_venv()
332-
self.assertEqual(current_venv, expected)
333+
self.assertEqual(str(current_venv), expected)
333334

334335
# pyenv test
335336
with mock.patch.dict("os.environ", {}, clear=True):
336337
with mock.patch("subprocess.check_output", side_effect=[None, b"/expected", b"path"]):
337338
current_venv = z.get_current_venv()
338-
self.assertEqual(current_venv, expected)
339+
self.assertEqual(str(current_venv), expected)
339340

340341
with mock.patch("subprocess.check_output", side_effect=OSError("No pyenv!")):
341342
current_venv = z.get_current_venv()
@@ -344,39 +345,52 @@ def test_get_current_venv(self, *args):
344345
def test_getting_installed_packages_mixed_case_location(self, *args):
345346
z = Zappa(runtime="python3.8")
346347

347-
# mock pip packages call to be same as what our mocked site packages dir has
348-
mock_package = collections.namedtuple("mock_package", ["project_name", "version", "location"])
349-
mock_pip_installed_packages = [
350-
mock_package("SuperPackage", "0.1", "/Venv/site-packages"),
351-
mock_package("SuperPackage64", "0.1", "/Venv/site-packages64"),
352-
]
353-
354-
with mock.patch("os.path.isdir", return_value=True):
355-
with mock.patch("os.listdir", return_value=[]):
356-
import pkg_resources # this gets called in non-test Zappa mode
357-
358-
with mock.patch("pkg_resources.WorkingSet", return_value=mock_pip_installed_packages):
359-
self.assertDictEqual(
360-
z.get_installed_packages("/venv/Site-packages", "/venv/site-packages64"),
361-
{
362-
"superpackage": "0.1",
363-
"superpackage64": "0.1",
364-
},
365-
)
348+
mock_pip_installed_packages = []
349+
for package_name, version, location in ("SuperPackage", "0.1", "/Venv/site-packages"), (
350+
"SuperPackage64",
351+
"0.1",
352+
"/Venv/site-packages64",
353+
):
354+
sample_package = mock.MagicMock()
355+
sample_package.name = package_name
356+
sample_package.version = version
357+
sample_package.locate_file = mock.MagicMock()
358+
sample_package.locate_file.return_value = location
359+
mock_pip_installed_packages.append(sample_package)
360+
361+
site_packages = mock.MagicMock()
362+
site_packages.is_dir = mock.MagicMock(return_value=True)
363+
site_packages.glob = mock.MagicMock(return_value=[Path("/venv/site-packages/SuperPackage")])
364+
365+
site_packages64 = mock.MagicMock()
366+
site_packages64.is_dir = mock.MagicMock(return_value=True)
367+
site_packages64.glob = mock.MagicMock(return_value=[Path("/venv/site-packages64/SuperPackage64")])
368+
369+
with mock.patch("importlib.metadata.distributions", return_value=mock_pip_installed_packages):
370+
self.assertDictEqual(
371+
z.get_installed_packages(site_packages, site_packages64),
372+
{
373+
"superpackage": "0.1",
374+
"superpackage64": "0.1",
375+
},
376+
)
366377

367378
def test_getting_installed_packages_mixed_case(self, *args):
368379
z = Zappa(runtime="python3.8")
369380

370-
# mock pkg_resources call to be same as what our mocked site packages dir has
371-
mock_package = collections.namedtuple("mock_package", ["project_name", "version", "location"])
372-
mock_pip_installed_packages = [mock_package("SuperPackage", "0.1", "/venv/site-packages")]
381+
sample_package = mock.MagicMock()
382+
sample_package.name = "SuperPackage"
383+
sample_package.version = "0.1"
384+
sample_package.locate_file = mock.MagicMock()
385+
sample_package.locate_file.return_value = "/venv/site-packages"
373386

374-
with mock.patch("os.path.isdir", return_value=True):
375-
with mock.patch("os.listdir", return_value=["superpackage"]):
376-
import pkg_resources # this gets called in non-test Zappa mode
387+
site_packages = mock.MagicMock()
388+
site_packages.is_dir = mock.MagicMock(return_value=True)
389+
site_packages.glob = mock.MagicMock(return_value=[Path("/venv/site-packages/superpackage")])
377390

378-
with mock.patch("pkg_resources.WorkingSet", return_value=mock_pip_installed_packages):
379-
self.assertDictEqual(z.get_installed_packages("", ""), {"superpackage": "0.1"})
391+
mock_pip_installed_packages = [sample_package]
392+
with mock.patch("importlib.metadata.distributions", return_value=mock_pip_installed_packages):
393+
self.assertDictEqual(z.get_installed_packages(site_packages=site_packages), {"superpackage": "0.1"})
380394

381395
def test_load_credentials(self):
382396
z = Zappa()
@@ -1353,26 +1367,49 @@ def test_wsgi_from_v2_event_with_iam_authorizer(self):
13531367
# CLI
13541368
##
13551369

1370+
# @pytest.fixture(autouse=True)
13561371
def test_zappa_init(self):
1372+
13571373
# delete if file exists
1358-
if os.path.exists("zappa_settings.json"):
1359-
os.remove("zappa_settings.json")
1374+
current_dir = os.getcwd()
1375+
with tempfile.TemporaryDirectory(prefix="zappa_test") as tempdir:
1376+
try:
1377+
os.chdir(tempdir)
1378+
tempdir = Path(tempdir)
13601379

1361-
process = subprocess.Popen(
1362-
["zappa", "init"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
1363-
)
1364-
process.communicate("dev\nmy-zappa-bucket\ntest_settings\ndefault\nn\ny\n")
1365-
self.assertTrue(os.path.exists("zappa_settings.json"))
1380+
settings_filepath = tempdir / "zappa_settings.json"
1381+
if settings_filepath.exists():
1382+
settings_filepath.unlink() # delete the file if it exists
1383+
assert settings_filepath.exists() is False
13661384

1367-
with open("zappa_settings.json", "r") as f:
1368-
zappa_settings = json.load(f)
1369-
self.assertEqual(zappa_settings["dev"]["s3_bucket"], "my-zappa-bucket")
1370-
self.assertEqual(zappa_settings["dev"]["django_settings"], "test_settings")
1371-
self.assertEqual(zappa_settings["dev"]["exclude"], ["boto3", "dateutil", "botocore", "s3transfer", "concurrent"])
1385+
zappa_cli = ZappaCLI()
1386+
with mock.patch("zappa.cli.ZappaCLI._get_init_env", return_value="dev"), mock.patch(
1387+
"zappa.cli.ZappaCLI._get_init_profile", return_value=("default", {"region": "us-west-2"})
1388+
), mock.patch("zappa.cli.ZappaCLI._get_init_bucket", return_value="my-zappa-bucket"), mock.patch(
1389+
"zappa.cli.ZappaCLI._get_init_django_settings", return_value="test_settings"
1390+
), mock.patch(
1391+
"zappa.cli.ZappaCLI._get_init_global_settings", return_value=["n", False]
1392+
), mock.patch(
1393+
"zappa.cli.ZappaCLI._get_init_confirm", return_value="y"
1394+
):
1395+
zappa_cli.init()
1396+
1397+
# make sure the expected `zappa_settings.json` file was created in the current directory
1398+
self.assertTrue(settings_filepath.exists())
1399+
1400+
with settings_filepath.open("r") as f:
1401+
zappa_settings = json.load(f)
1402+
self.assertEqual(zappa_settings["dev"]["s3_bucket"], "my-zappa-bucket")
1403+
self.assertEqual(zappa_settings["dev"]["django_settings"], "test_settings")
1404+
self.assertEqual(
1405+
zappa_settings["dev"]["exclude"], ["boto3", "dateutil", "botocore", "s3transfer", "concurrent"]
1406+
)
13721407

1373-
# delete the file
1374-
if os.path.exists("zappa_settings.json"):
1375-
os.remove("zappa_settings.json")
1408+
# delete the file
1409+
if os.path.exists("zappa_settings.json"):
1410+
os.remove("zappa_settings.json")
1411+
finally:
1412+
os.chdir(current_dir)
13761413

13771414
def test_cli_sanity(self):
13781415
zappa_cli = ZappaCLI()
@@ -2274,8 +2311,6 @@ def test_remote_env_package(self):
22742311
with zipfile.ZipFile(zappa_cli.zip_path, "r") as lambda_zip:
22752312
content = lambda_zip.read("zappa_settings.py")
22762313
zappa_cli.remove_local_zip()
2277-
# m = re.search("REMOTE_ENV='(.*)'", content)
2278-
# self.assertEqual(m.group(1), 's3://lmbda-env/dev/env.json')
22792314

22802315
zappa_cli = ZappaCLI()
22812316
zappa_cli.api_stage = "remote_env"
@@ -2285,8 +2320,6 @@ def test_remote_env_package(self):
22852320
with zipfile.ZipFile(zappa_cli.zip_path, "r") as lambda_zip:
22862321
content = lambda_zip.read("zappa_settings.py")
22872322
zappa_cli.remove_local_zip()
2288-
# m = re.search("REMOTE_ENV='(.*)'", content)
2289-
# self.assertEqual(m.group(1), 's3://lmbda-env/prod/env.json')
22902323

22912324
def test_package_only(self):
22922325
for delete_local_zip in [True, False]:

0 commit comments

Comments
 (0)