diff --git a/src/codemodder/dependency_manager.py b/src/codemodder/dependency_manager.py index af41ead2..6398bf8c 100644 --- a/src/codemodder/dependency_manager.py +++ b/src/codemodder/dependency_manager.py @@ -57,6 +57,9 @@ def write(self, dry_run: bool = False) -> Optional[ChangeSet]: if not dry_run: with open(self.dependency_file, "w", encoding="utf-8") as f: f.writelines(self._lines) + if not self._lines[-1].endswith("\n"): + f.write("\n") + f.writelines([f"{line}\n" for line in self.new_requirements]) self.dependency_file_changed = True diff --git a/tests/test_dependency_manager.py b/tests/test_dependency_manager.py index 62a2bd06..2700b584 100644 --- a/tests/test_dependency_manager.py +++ b/tests/test_dependency_manager.py @@ -89,3 +89,15 @@ def test_dont_add_existing_dependency(self, version, tmpdir): assert dependency_file.read_text(encoding="utf-8") == ( f"requests\nsecurity~={version}\n" ) + + def test_dependency_file_no_terminating_newline(self, tmpdir): + dependency_file = Path(tmpdir) / "requirements.txt" + dependency_file.write_text("requests\nwhatever", encoding="utf-8") + + dm = DependencyManager(Path(tmpdir)) + dm.add([Security]) + dm.write() + + assert dependency_file.read_text(encoding="utf-8") == ( + "requests\nwhatever\nsecurity~=1.2.0\n" + )