diff --git a/pipenv/project.py b/pipenv/project.py index cb34f689..43b1bc12 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -1226,7 +1226,9 @@ class Project: newly_added = True p[category][normalized_name] = entry - p[category] = dict(sorted(p[category].items())) + + if self.settings.get("sort_alphabetical"): + 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 bf4d2f13..85680978 100644 --- a/tests/integration/test_install_basic.py +++ b/tests/integration/test_install_basic.py @@ -557,9 +557,30 @@ dataclasses-json = {file = "https://files.pythonhosted.org/packages/85/94/1b3021 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): +def test_category_sorted_alphabetically_with_directive(pipenv_instance_private_pypi): + with pipenv_instance_private_pypi() as p: + with open(p.pipfile_path, "w") as f: + contents = """ +[pipenv] +sort_alphabetical = true + +[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"] + + +@pytest.mark.basic +@pytest.mark.install +def test_category_not_sorted_without_directive(pipenv_instance_private_pypi): with pipenv_instance_private_pypi() as p: with open(p.pipfile_path, "w") as f: contents = """ @@ -571,5 +592,8 @@ colorama = "*" c = p.pipenv("install build") assert c.returncode == 0 assert "build" in p.pipfile["packages"] - assert list(p.pipfile["packages"].keys()) == ["atomicwrites", "build", "colorama"] - + assert list(p.pipfile["packages"].keys()) == [ + "atomicwrites", + "colorama", + "build", + ]