From 5cbb096af8ee3cb62738ef88807209ee4f196d9a Mon Sep 17 00:00:00 2001 From: per1234 Date: Fri, 31 Mar 2023 03:28:49 -0700 Subject: [PATCH] Prevent unintended rendering of report content MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The report is made in the form of GitHub PR comment. These comments are rendered according to the "GitHub Flavored Markdown" markup language. In addition to the intentional markup used to format the report, the uncontrolled strings of the report content might contain incidental markup. Previously, this would cause the report to be rendered incorrectly. For example, the FQBN `arduino:avr:mega:cpu=atmega2560` contains the markup for the megaphone emoji, so was rendered incorrectly: > arduino:avr📣cpu=atmega2560 The solution is to wrap the uncontrolled strings in the Markdown "code span" markup. Since the entire CSV format report is already wrapped in the equivalent "code fences" markup, the introduction of this markup into that part of the report is unnecessary. --- reportsizedeltas/reportsizedeltas.py | 21 +++++++--- .../tests/test_reportsizedeltas.py | 38 +++++++++---------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/reportsizedeltas/reportsizedeltas.py b/reportsizedeltas/reportsizedeltas.py index a30126d..ca409da 100644 --- a/reportsizedeltas/reportsizedeltas.py +++ b/reportsizedeltas/reportsizedeltas.py @@ -380,7 +380,7 @@ def add_summary_report_row(self, report_data, fqbn_data): row_number = len(report_data) # Add a row to the report row = ["" for _ in range(len(report_data[0]))] - row[0] = fqbn_data[self.ReportKeys.board] + row[0] = "`{board_name}`".format(board_name=fqbn_data[self.ReportKeys.board]) report_data.append(row) # Populate the row with data @@ -443,7 +443,7 @@ def add_detailed_report_row(self, report_data, fqbn_data): row_number = len(report_data) # Add a row to the report row = ["" for _ in range(len(report_data[0]))] - row[0] = fqbn_data[self.ReportKeys.board] + row[0] = "`{board_name}`".format(board_name=fqbn_data[self.ReportKeys.board]) report_data.append(row) # Populate the row with data @@ -453,8 +453,10 @@ def add_detailed_report_row(self, report_data, fqbn_data): column_number = get_report_column_number( report=report_data, column_heading=( - sketch[self.ReportKeys.name] + "
" - + size_data[self.ReportKeys.name] + "`{sketch_name}`
{size_name}".format( + sketch_name=sketch[self.ReportKeys.name], + size_name=size_data[self.ReportKeys.name] + ) ) ) @@ -776,7 +778,16 @@ def generate_csv_table(row_list): csv_string = io.StringIO() csv_writer = csv.writer(csv_string, lineterminator="\n") for row in row_list: - csv_writer.writerow(row) + cleaned_row = [] + for cell in row: + cleaned_cell = cell + if isinstance(cleaned_cell, str): + # The "code span" markup is not needed in the CSV report. + cleaned_cell = cleaned_cell.replace("`", "") + + cleaned_row.append(cleaned_cell) + + csv_writer.writerow(cleaned_row) return csv_string.getvalue() diff --git a/reportsizedeltas/tests/test_reportsizedeltas.py b/reportsizedeltas/tests/test_reportsizedeltas.py index a1bc55c..d9c8cbf 100644 --- a/reportsizedeltas/tests/test_reportsizedeltas.py +++ b/reportsizedeltas/tests/test_reportsizedeltas.py @@ -853,7 +853,7 @@ def test_get_sketches_reports(sketches_reports_path, expected_sketches_reports): [ ["Board", "flash", "%", "RAM for global variables", "%"], [ - "arduino:avr:uno", + "`arduino:avr:uno`", ":green_heart: -994 - -994", "-3.08 - -3.08", ":green_heart: -175 - -175", @@ -865,7 +865,7 @@ def test_get_sketches_reports(sketches_reports_path, expected_sketches_reports): [ ["Board", "flash", "%", "RAM for global variables", "%"], [ - "arduino:avr:uno", + "`arduino:avr:uno`", ":green_heart: -994 - -994", "-3.08 - -3.08", ":green_heart: -175 - -175", @@ -888,13 +888,13 @@ def test_get_sketches_reports(sketches_reports_path, expected_sketches_reports): [ ["Board", "flash", "%", "RAM for global variables", "%"], [ - "arduino:avr:uno", + "`arduino:avr:uno`", ":green_heart: -994 - -994", "-3.08 - -3.08", ":green_heart: -175 - -175", "-8.54 - -8.54" ], - ["arduino:mbed_portenta:envie_m7", "N/A", "N/A", "N/A", "N/A"] + ["`arduino:mbed_portenta:envie_m7`", "N/A", "N/A", "N/A", "N/A"] ] ) ] @@ -955,14 +955,14 @@ def test_add_summary_report_row(report_data, fqbn_data, expected_report_data): ] }, [ - ["Board", "examples/Foo
flash", "%", "examples/Foo
RAM for global variables", "%"], - ["arduino:avr:leonardo", -12, -0.05, 0, -0.0] + ["Board", "`examples/Foo`
flash", "%", "`examples/Foo`
RAM for global variables", "%"], + ["`arduino:avr:leonardo`", -12, -0.05, 0, -0.0] ] ), ( [ - ["Board", "examples/Foo
flash", "%", "examples/Foo
RAM for global variables", "%"], - ["arduino:avr:leonardo", -12, -0.05, 0, -0.0] + ["Board", "`examples/Foo`
flash", "%", "`examples/Foo`
RAM for global variables", "%"], + ["`arduino:avr:leonardo`", -12, -0.05, 0, -0.0] ], { report_keys.board: "arduino:mbed_portenta:envie_m7", @@ -992,9 +992,9 @@ def test_add_summary_report_row(report_data, fqbn_data, expected_report_data): ] }, [ - ["Board", "examples/Foo
flash", "%", "examples/Foo
RAM for global variables", "%"], - ["arduino:avr:leonardo", -12, -0.05, 0, -0.0], - ["arduino:mbed_portenta:envie_m7", "N/A", "N/A", "N/A", "N/A"] + ["Board", "`examples/Foo`
flash", "%", "`examples/Foo`
RAM for global variables", "%"], + ["`arduino:avr:leonardo`", -12, -0.05, 0, -0.0], + ["`arduino:mbed_portenta:envie_m7`", "N/A", "N/A", "N/A", "N/A"] ] ) ] @@ -1012,17 +1012,17 @@ def test_generate_report(): "**Memory usage change @ d8fd302**\n\n" "Board|flash|%|RAM for global variables|%\n" "-|-|-|-|-\n" - "arduino:avr:leonardo|:green_heart: -12 - -12|-0.05 - -0.05|0 - 0|0.0 - 0.0\n" - "arduino:avr:uno|:green_heart: -994 - -994|-3.08 - -3.08|:green_heart: -175 - -175|-8.54 - -8.54\n" - "arduino:mbed_portenta:envie_m7|N/A|N/A|N/A|N/A\n\n" + "`arduino:avr:leonardo`|:green_heart: -12 - -12|-0.05 - -0.05|0 - 0|0.0 - 0.0\n" + "`arduino:avr:uno`|:green_heart: -994 - -994|-3.08 - -3.08|:green_heart: -175 - -175|-8.54 - -8.54\n" + "`arduino:mbed_portenta:envie_m7`|N/A|N/A|N/A|N/A\n\n" "
\n" "Click for full report table\n\n" - "Board|examples/Bar
flash|%|examples/Bar
RAM for global variables|%|examples/Foo
flash|%|examples/Foo" - "
RAM for global variables|%\n" + "Board|`examples/Bar`
flash|%|`examples/Bar`
RAM for global variables|%|`examples/Foo`
flash|%|" + "`examples/Foo`
RAM for global variables|%\n" "-|-|-|-|-|-|-|-|-\n" - "arduino:avr:leonardo|N/A|N/A|N/A|N/A|-12|-0.05|0|0.0\n" - "arduino:avr:uno|N/A|N/A|N/A|N/A|-994|-3.08|-175|-8.54\n" - "arduino:mbed_portenta:envie_m7|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A\n\n" + "`arduino:avr:leonardo`|N/A|N/A|N/A|N/A|-12|-0.05|0|0.0\n" + "`arduino:avr:uno`|N/A|N/A|N/A|N/A|-994|-3.08|-175|-8.54\n" + "`arduino:mbed_portenta:envie_m7`|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A\n\n" "
\n\n" "
\n" "Click for full report CSV\n\n"