Skip to content

Commit

Permalink
fix: csv output under Windows with correct newlines (#4557) (#4558)
Browse files Browse the repository at this point in the history
* fixes #4557
  • Loading branch information
weichslgartner authored Nov 12, 2024
1 parent 9712d5c commit dafb9da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cve_bin_tool/output_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,10 @@ def output_file(self, output_type="console"):
with open(self.filename, "wb") as f:
self.output_cves(f, output_type)
else:
with open(self.filename, "w", encoding="utf8") as f:
# if type is csv, file should be opened with newline=''
# see https://docs.python.org/3/library/csv.html#csv.writer
newline = "" if output_type == "csv" else None
with open(self.filename, mode="w", newline=newline, encoding="utf8") as f:
self.output_cves(f, output_type)

def check_file_path(self, filepath: str, output_type: str, prefix: str = "output"):
Expand Down
10 changes: 10 additions & 0 deletions test/test_output_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,16 @@ def test_output_file(self):
self.assertEqual(contains_filename, True)
self.assertEqual(contains_msg, True)

def test_csv_output_file(self):
self.output_engine.output_file(output_type="csv")
filename = Path(self.output_engine.filename)
n_cves = sum(len(c["cves"]) for c in self.MOCK_OUTPUT.values())
with filename.open(newline="", mode="r") as f:
n_lines = len(f.read().splitlines())
# cvs file should have one line per cve plus a header line
assert n_lines == n_cves + 1
filename.unlink()

def test_output_file_wrapper(self):
"""Test file generation logic in output_file_wrapper"""
logger = logging.getLogger()
Expand Down

0 comments on commit dafb9da

Please sign in to comment.