Skip to content

Commit

Permalink
Dependency manager: handle missing terminating newline
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Oct 31, 2023
1 parent d07d619 commit 1cd0ade
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/codemodder/dependency_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_dependency_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit 1cd0ade

Please sign in to comment.