Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added newline inference when adding dependencies in setup.py #190

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions src/codemodder/dependency_management/setup_py_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,51 @@ def add_dependencies_to_arg(self, arg: cst.Arg) -> cst.Arg:
# dependency listed in install_requires
self.line_num_changed = self.lineno_for_node(arg.value.elements[-1]) - 1

# grab the penultimate comma value if it has more than one element
new_comma = cst.Comma(whitespace_after=cst.SimpleWhitespace(" "))
last_element = arg.value.elements[-1]
new_last_element = cst.Element(
value=cst.SimpleString(value=f'"{str(self.dependencies[-1])}"')
)
if len(arg.value.elements) > 1:
new_comma = arg.value.elements[-2].comma
# if it has a newline, add a comma to the last element
# this follows black's standard
match new_comma.whitespace_after:
case cst.ParenthesizedWhitespace(
first_line=cst.TrailingWhitespace(newline=cst.Newline())
):
new_last_element = new_last_element.with_changes(comma=cst.Comma())
else:
# infer the indentation from lbracket, if any
match lbracket_whitespace := arg.value.lbracket.whitespace_after:
case cst.ParenthesizedWhitespace():
new_comma = cst.Comma(
whitespace_after=cst.ParenthesizedWhitespace(
indent=True,
last_line=lbracket_whitespace.last_line,
)
)
# In a list with a single element, libcst will attribute the newline to the rbracket
match arg.value.rbracket.whitespace_before.first_line:
case cst.TrailingWhitespace(newline=cst.Newline()):
new_last_element = new_last_element.with_changes(
comma=cst.Comma()
)

new_dependencies = [
cst.Element(value=cst.SimpleString(value=f'"{str(dep)}"'))
for dep in self.dependencies
]
# TODO: detect if elements are separated by newline in source code.
cst.Element(value=cst.SimpleString(value=f'"{str(dep)}"'), comma=new_comma)
for dep in self.dependencies[:-1]
] + [new_last_element]

return cst.Arg(
keyword=arg.keyword,
value=arg.value.with_changes(
elements=arg.value.elements + tuple(new_dependencies)
elements=[
*arg.value.elements[:-1],
last_element.with_changes(comma=new_comma),
*new_dependencies,
],
),
equal=arg.equal,
comma=arg.comma,
Expand Down
126 changes: 116 additions & 10 deletions tests/dependency_management/test_setup_py_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,106 @@
TEST_DEPENDENCIES = [Requirement("defusedxml==0.7.1"), Requirement("security~=1.2.0")]


def test_update_setuppy_comma_single_element_newline(tmpdir):
original = """
from setuptools import setup
setup(
name="test pkg",
description="testing",
long_description="...",
author="Pixee",
packages=find_packages("src"),
package_dir={"": "src"},
python_requires=">3.6",
install_requires=[
"protobuf>=3.12,<3.18; python_version < '3'",
],
entry_points={},
)
"""

dependency_file = tmpdir.join("setup.py")
dependency_file.write(dedent(original))

store = PackageStore(
type=FileType.SETUP_PY,
file=str(dependency_file),
dependencies=set(),
py_versions=[">=3.6"],
)

writer = SetupPyWriter(store, tmpdir)
dependencies = [DefusedXML, Security]
writer.write(dependencies, dry_run=False)

after = """
from setuptools import setup
setup(
name="test pkg",
description="testing",
long_description="...",
author="Pixee",
packages=find_packages("src"),
package_dir={"": "src"},
python_requires=">3.6",
install_requires=[
"protobuf>=3.12,<3.18; python_version < '3'",
"defusedxml~=0.7.1",
"security~=1.2.0",
],
entry_points={},
)
"""
assert dependency_file.read() == dedent(after)


def test_update_setuppy_comma_single_element_inline(tmpdir):
original = """
from setuptools import setup
setup(
name="test pkg",
description="testing",
long_description="...",
author="Pixee",
packages=find_packages("src"),
package_dir={"": "src"},
python_requires=">3.6",
install_requires=["protobuf>=3.12,<3.18; python_version < '3'"],
entry_points={},
)
"""

dependency_file = tmpdir.join("setup.py")
dependency_file.write(dedent(original))

store = PackageStore(
type=FileType.SETUP_PY,
file=str(dependency_file),
dependencies=set(),
py_versions=[">=3.6"],
)

writer = SetupPyWriter(store, tmpdir)
dependencies = [DefusedXML, Security]
writer.write(dependencies, dry_run=False)

after = """
from setuptools import setup
setup(
name="test pkg",
description="testing",
long_description="...",
author="Pixee",
packages=find_packages("src"),
package_dir={"": "src"},
python_requires=">3.6",
install_requires=["protobuf>=3.12,<3.18; python_version < '3'", "defusedxml~=0.7.1", "security~=1.2.0"],
entry_points={},
)
"""
assert dependency_file.read() == dedent(after)


@pytest.mark.parametrize("dry_run", [True, False])
def test_update_setuppy_dependencies(tmpdir, dry_run):
original = """
Expand All @@ -27,7 +127,7 @@ def test_update_setuppy_dependencies(tmpdir, dry_run):
"protobuf>=3.12,<3.18; python_version < '3'",
"protobuf>=3.12,<4; python_version >= '3'",
"psutil>=5.7,<6",
"requests>=2.4.2,<3"
"requests>=2.4.2,<3",
],
entry_points={},
)
Expand Down Expand Up @@ -61,7 +161,9 @@ def test_update_setuppy_dependencies(tmpdir, dry_run):
"protobuf>=3.12,<3.18; python_version < '3'",
"protobuf>=3.12,<4; python_version >= '3'",
"psutil>=5.7,<6",
"requests>=2.4.2,<3", "defusedxml~=0.7.1", "security~=1.2.0"
"requests>=2.4.2,<3",
"defusedxml~=0.7.1",
"security~=1.2.0",
],
entry_points={},
)
Expand All @@ -73,12 +175,12 @@ def test_update_setuppy_dependencies(tmpdir, dry_run):
res = (
"--- \n"
"+++ \n"
"@@ -12,7 +12,7 @@\n"
""" "protobuf>=3.12,<3.18; python_version < '3'",\n"""
"@@ -13,6 +13,8 @@\n"
""" "protobuf>=3.12,<4; python_version >= '3'",\n"""
""" "psutil>=5.7,<6",\n"""
"""- "requests>=2.4.2,<3"\n"""
"""+ "requests>=2.4.2,<3", "defusedxml~=0.7.1", "security~=1.2.0"\n"""
""" "requests>=2.4.2,<3",\n"""
"""+ "defusedxml~=0.7.1",\n"""
"""+ "security~=1.2.0",\n"""
" ],\n "
" entry_points={},\n"
" )\n"
Expand Down Expand Up @@ -111,7 +213,7 @@ def test_other_setup_func(tmpdir):
"protobuf>=3.12,<3.18; python_version < '3'",
"protobuf>=3.12,<4; python_version >= '3'",
"psutil>=5.7,<6",
"requests>=2.4.2,<3"
"requests>=2.4.2,<3",
],
entry_points={},
)
Expand Down Expand Up @@ -149,7 +251,7 @@ def test_not_setup_file(tmpdir):
"protobuf>=3.12,<3.18; python_version < '3'",
"protobuf>=3.12,<4; python_version >= '3'",
"psutil>=5.7,<6",
"requests>=2.4.2,<3"
"requests>=2.4.2,<3",
],
entry_points={},
)
Expand Down Expand Up @@ -310,7 +412,9 @@ def test_setup_call_requirements_separate(tmpdir):
"protobuf>=3.12,<3.18; python_version < '3'",
"protobuf>=3.12,<4; python_version >= '3'",
"psutil>=5.7,<6",
"requests>=2.4.2,<3", "defusedxml==0.7.1", "security~=1.2.0"
"requests>=2.4.2,<3",
"defusedxml==0.7.1",
"security~=1.2.0",
]
setup(
name="test pkg",
Expand All @@ -336,7 +440,9 @@ def test_setup_call_requirements_separate(tmpdir):
""" "protobuf>=3.12,<4; python_version >= '3'",\n"""
""" "psutil>=5.7,<6",\n"""
"""- "requests>=2.4.2,<3"\n"""
"""+ "requests>=2.4.2,<3", "defusedxml~=0.7.1", "security~=1.2.0"\n"""
"""+ "requests>=2.4.2,<3",\n"""
"""+ "defusedxml~=0.7.1",\n"""
"""+ "security~=1.2.0",\n"""
" ],\n "
" entry_points={},\n"
" )\n"
Expand Down
Loading