diff --git a/integration_tests/test_dependency_manager.py b/integration_tests/test_dependency_manager.py index 7f1017c7..066677fe 100644 --- a/integration_tests/test_dependency_manager.py +++ b/integration_tests/test_dependency_manager.py @@ -3,6 +3,7 @@ import shutil import pytest from integration_tests.base_test import SAMPLES_DIR, CleanRepoMixin +from textwrap import dedent class TestDependencyManager(CleanRepoMixin): @@ -10,16 +11,69 @@ class TestDependencyManager(CleanRepoMixin): toml_file = "pyproject.toml" requirements_file = "requirements.txt" + setup_file = "setup.py" @pytest.fixture def tmp_repo(self, tmp_path): (tmp_path / self.toml_file).touch() (tmp_path / self.requirements_file).touch() + (tmp_path / self.setup_file).touch() with open(tmp_path / self.requirements_file, "w", encoding="utf-8") as req: req.write("requests") shutil.copy(SAMPLES_DIR + "/make_request.py", tmp_path) return tmp_path + def write_pyproject_toml(self, tmp_repo): + toml = dedent( + """\ + [project] + dependencies = [ + "requests", + ] + """ + ) + with open(tmp_repo / self.toml_file, "w", encoding="utf-8") as file: + file.write(toml) + + def write_setup_file(self, tmp_repo): + setup = dedent( + """\ + from setuptools import setup, find_packages + setup( + name='test', + version='3.0.0', + packages=find_packages(where="."), + install_requires=["requests"], + ) + """ + ) + with open(tmp_repo / self.setup_file, "w", encoding="utf-8") as file: + file.write(setup) + + def test_add_to_pyproject_toml(self, tmp_repo): + self.write_pyproject_toml(tmp_repo) + command = [ + "codemodder", + tmp_repo, + "--output", + self.output_path, + "--codemod-include=url-sandbox", + "--verbose", + ] + completed_process = subprocess.run( + command, + check=False, + shell=False, + capture_output=True, + encoding="utf-8", + ) + lines = completed_process.stdout.splitlines() + assert completed_process.returncode == 0 + assert ( + f"The following dependencies were added to '{tmp_repo / self.toml_file}': security" + in lines + ) + def test_add_to_requirements_txt(self, tmp_repo): command = [ "codemodder", @@ -43,6 +97,31 @@ def test_add_to_requirements_txt(self, tmp_repo): in lines ) + def test_add_to_setup(self, tmp_repo): + os.chmod(tmp_repo / self.requirements_file, 0o400) + self.write_setup_file(tmp_repo) + command = [ + "codemodder", + tmp_repo, + "--output", + self.output_path, + "--codemod-include=url-sandbox", + "--verbose", + ] + completed_process = subprocess.run( + command, + check=False, + shell=False, + capture_output=True, + encoding="utf-8", + ) + lines = completed_process.stdout.splitlines() + assert completed_process.returncode == 0 + assert ( + f"The following dependencies were added to '{tmp_repo / self.setup_file}': security" + in lines + ) + def test_fail_to_add(self, tmp_repo): os.chmod(tmp_repo / self.requirements_file, 0o400)