From 20f0f79760d1da0728beb4df483c38585f5c61fe Mon Sep 17 00:00:00 2001 From: Joe Marshall Date: Fri, 13 Oct 2023 12:26:24 +0100 Subject: [PATCH] review updates --- pyodide_lock/utils.py | 11 +++++++---- tests/test_wheel.py | 29 ++++++++++++++++------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/pyodide_lock/utils.py b/pyodide_lock/utils.py index ba03e2c..fbfeb8c 100644 --- a/pyodide_lock/utils.py +++ b/pyodide_lock/utils.py @@ -156,8 +156,9 @@ def add_wheels_to_spec( base_path: Path | None = None, base_url: str = "", ignore_missing_dependencies: bool = False, -) -> None: - """Add a list of wheel files to this pyodide-lock.json +) -> PyodideLockSpec: + """Add a list of wheel files to this pyodide-lock.json and return a + new PyodideLockSpec Parameters: wheel_files : list[Path] @@ -177,8 +178,9 @@ def add_wheels_to_spec( not 100% reliable, because it ignores any extras and does not do any sub-dependency or version resolution. """ + new_spec = lock_spec.copy(deep=True) if not wheel_files: - return + return new_spec wheel_files = [f.resolve() for f in wheel_files] if base_path is None: base_path = wheel_files[0].parent @@ -193,7 +195,8 @@ def add_wheels_to_spec( _fix_new_package_deps(lock_spec, new_packages, ignore_missing_dependencies) _set_package_paths(new_packages, base_path, base_url) - lock_spec.packages |= new_packages + new_spec.packages |= new_packages + return new_spec def _fix_new_package_deps( diff --git a/tests/test_wheel.py b/tests/test_wheel.py index adc718b..265da85 100644 --- a/tests/test_wheel.py +++ b/tests/test_wheel.py @@ -19,13 +19,14 @@ def test_add_one(test_wheel_list, example_lock_spec): - add_wheels_to_spec(example_lock_spec, test_wheel_list[0:1]) - # py_one only should get added - assert example_lock_spec.packages["py-one"].imports == ["one"] + new_lock_spec = add_wheels_to_spec(example_lock_spec, test_wheel_list[0:1]) + # py_one only should get added to the new spec + assert new_lock_spec.packages["py-one"].imports == ["one"] + assert "py-one" not in example_lock_spec.packages def test_add_simple_deps(test_wheel_list, example_lock_spec): - add_wheels_to_spec(example_lock_spec, test_wheel_list[0:3]) + example_lock_spec = add_wheels_to_spec(example_lock_spec, test_wheel_list[0:3]) # py_one, needs_one and needs_one_opt should get added assert "py-one" in example_lock_spec.packages assert "needs-one" in example_lock_spec.packages @@ -37,7 +38,7 @@ def test_add_simple_deps(test_wheel_list, example_lock_spec): def test_add_deps_with_extras(test_wheel_list, example_lock_spec): - add_wheels_to_spec(example_lock_spec, test_wheel_list[0:4]) + example_lock_spec = add_wheels_to_spec(example_lock_spec, test_wheel_list[0:4]) # py_one, needs_one, needs_one_opt and test_extra_dependencies should get added # because of the extra dependency in test_extra_dependencies, # needs_one_opt should now depend on one @@ -48,11 +49,11 @@ def test_add_deps_with_extras(test_wheel_list, example_lock_spec): def test_missing_dep(test_wheel_list, example_lock_spec): # this has a package with a missing dependency so should fail with pytest.raises(RuntimeError): - add_wheels_to_spec(example_lock_spec, test_wheel_list[0:5]) + example_lock_spec = add_wheels_to_spec(example_lock_spec, test_wheel_list[0:5]) def test_url_rewriting(test_wheel_list, example_lock_spec): - add_wheels_to_spec( + example_lock_spec = add_wheels_to_spec( example_lock_spec, test_wheel_list[0:3], base_url="http://www.nowhere.org/" ) # py_one, needs_one and needs_one_opt should get added @@ -67,7 +68,7 @@ def test_url_rewriting(test_wheel_list, example_lock_spec): def test_base_relative_path(test_wheel_list, example_lock_spec): # this should make all the file names relative to the # parent path of the wheels (which is "dist") - add_wheels_to_spec( + example_lock_spec = add_wheels_to_spec( example_lock_spec, test_wheel_list[0:3], base_url="http://www.nowhere.org/", @@ -84,14 +85,14 @@ def test_base_relative_path(test_wheel_list, example_lock_spec): # all requirements markers should not be needed, so dependencies should be empty def test_markers_not_needed(test_wheel_list, example_lock_spec): - add_wheels_to_spec(example_lock_spec, test_wheel_list[5:6]) + example_lock_spec = add_wheels_to_spec(example_lock_spec, test_wheel_list[5:6]) assert example_lock_spec.packages["markers-not-needed-test"].depends == [] # all requirements markers should be needed, # so returned dependencies should be the same length as marker_examples_needed def test_markers_needed(test_wheel_list, example_lock_spec, marker_examples_needed): - add_wheels_to_spec( + example_lock_spec = add_wheels_to_spec( example_lock_spec, test_wheel_list[6:7], ignore_missing_dependencies=True ) assert len(example_lock_spec.packages["markers-needed-test"].depends) == len( @@ -102,7 +103,9 @@ def test_markers_needed(test_wheel_list, example_lock_spec, marker_examples_need @pytest.mark.skipif(WHEEL is None, reason="wheel test requires a built wheel") def test_self_wheel(example_lock_spec): assert WHEEL is not None - add_wheels_to_spec(example_lock_spec, [WHEEL], ignore_missing_dependencies=True) + example_lock_spec = add_wheels_to_spec( + example_lock_spec, [WHEEL], ignore_missing_dependencies=True + ) expected = PackageSpec( name="pyodide-lock", @@ -126,7 +129,7 @@ def test_not_wheel(tmp_path, example_lock_spec): whlzip.writestr("README.md", data="Not a wheel") with pytest.raises(RuntimeError, match="metadata"): - add_wheels_to_spec(example_lock_spec, [wheel]) + example_lock_spec = add_wheels_to_spec(example_lock_spec, [wheel]) @pytest.mark.parametrize( @@ -141,7 +144,7 @@ def test_bad_names(tmp_path, bad_name, example_lock_spec): with zipfile.ZipFile(wheel, "w") as whlzip: whlzip.writestr("README.md", data="Not a wheel") with pytest.raises(RuntimeError, match="Wheel filename"): - add_wheels_to_spec(example_lock_spec, [wheel]) + example_lock_spec = add_wheels_to_spec(example_lock_spec, [wheel]) def test_wheel_compatibility_checking(example_lock_spec):