From e87e2ceb059d0d555714eceba69f4dfd221bd960 Mon Sep 17 00:00:00 2001 From: Dave Schaefer Date: Wed, 4 Oct 2023 21:35:16 -0600 Subject: [PATCH] Sort packages alphabetically inside each category When installing any package, sort all package names alphabetically inside the category, for easier reading. Unsure if this is the best place or way to implement. Small prototype to add to discussion in https://github.com/pypa/pipenv/issues/5964 Tests: before patch: ``` AssertionError: assert ['atomicwrite...ama', 'build'] == ['atomicwrite...', 'colorama'] At index 1 diff: 'colorama' != 'build' Full diff: - ['atomicwrites', 'build', 'colorama'] ? --------- + ['atomicwrites', 'colorama', 'build'] ? +++++++++ ``` after patch: pass. --- pipenv/project.py | 1 + tests/integration/test_install_basic.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pipenv/project.py b/pipenv/project.py index b96487c1..cb34f689 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -1226,6 +1226,7 @@ class Project: newly_added = True p[category][normalized_name] = entry + p[category] = dict(sorted(p[category].items())) # Write Pipfile. self.write_toml(p) diff --git a/tests/integration/test_install_basic.py b/tests/integration/test_install_basic.py index 02d5b855..bf4d2f13 100644 --- a/tests/integration/test_install_basic.py +++ b/tests/integration/test_install_basic.py @@ -556,3 +556,20 @@ dataclasses-json = {file = "https://files.pythonhosted.org/packages/85/94/1b3021 assert c.returncode == 0 c = p.pipenv("""run python -c "from dataclasses_json import dataclass_json" """) assert c.returncode == 0 + +@pytest.mark.basic +@pytest.mark.install +def test_packages_sorted_alphabetically_in_category(pipenv_instance_private_pypi): + with pipenv_instance_private_pypi() as p: + with open(p.pipfile_path, "w") as f: + contents = """ +[packages] +atomicwrites = "*" +colorama = "*" + """.strip() + f.write(contents) + c = p.pipenv("install build") + assert c.returncode == 0 + assert "build" in p.pipfile["packages"] + assert list(p.pipfile["packages"].keys()) == ["atomicwrites", "build", "colorama"] +