From 1981997ee25ef8bd3f2b3e79f6722ad5338413f1 Mon Sep 17 00:00:00 2001
From: Marek Roszko <mark.roszko@gmail.com>
Date: Sun, 21 Jan 2024 16:10:43 -0500
Subject: [PATCH] Dump the ERC report as part of the erc failure

---
 eeschema/erc_report.cpp                       | 24 ++++++++++---------
 eeschema/erc_report.h                         | 18 ++++++++++++++
 .../eeschema/erc/test_erc_global_labels.cpp   |  9 +++++--
 .../erc/test_erc_hierarchical_schematics.cpp  |  6 ++++-
 .../erc/test_erc_label_not_connected.cpp      |  8 +++++--
 qa/tests/eeschema/erc/test_erc_no_connect.cpp |  8 +++++--
 .../eeschema/erc/test_erc_stacking_pins.cpp   |  8 +++++--
 7 files changed, 61 insertions(+), 20 deletions(-)

diff --git a/eeschema/erc_report.cpp b/eeschema/erc_report.cpp
index 7c4736c45c..ce3d2b4947 100644
--- a/eeschema/erc_report.cpp
+++ b/eeschema/erc_report.cpp
@@ -41,13 +41,8 @@ ERC_REPORT::ERC_REPORT( SCHEMATIC* aSchematic, EDA_UNITS aReportUnits ) :
 }
 
 
-bool ERC_REPORT::WriteTextReport( const wxString& aFullFileName )
+wxString ERC_REPORT::GetTextReport()
 {
-    wxFFile file( aFullFileName, wxT( "wt" ) );
-
-    if( !file.IsOpened() )
-        return false;
-
     UNITS_PROVIDER unitsProvider( schIUScale, m_reportUnits );
 
     wxString msg = wxString::Format( _( "ERC report (%s, Encoding UTF8)\n" ),
@@ -93,13 +88,20 @@ bool ERC_REPORT::WriteTextReport( const wxString& aFullFileName )
     msg << wxString::Format( _( "\n ** ERC messages: %d  Errors %d  Warnings %d\n" ), total_count,
                              err_count, warn_count );
 
-    // Currently: write report using UTF8 (as usual in Kicad).
-    // TODO: see if we can use the current encoding page (mainly for Windows users),
-    // Or other format (HTML?)
-    file.Write( msg );
+    return msg;
+}
+
+
+bool ERC_REPORT::WriteTextReport( const wxString& aFullFileName )
+{
+    wxFFile file( aFullFileName, wxT( "wt" ) );
+
+    if( !file.IsOpened() )
+        return false;
+
+    file.Write( GetTextReport() );
 
     // wxFFile dtor will close the file.
-
     return true;
 }
 
diff --git a/eeschema/erc_report.h b/eeschema/erc_report.h
index d19cc50462..54878dc63d 100644
--- a/eeschema/erc_report.h
+++ b/eeschema/erc_report.h
@@ -32,7 +32,25 @@ class ERC_REPORT
 public:
     ERC_REPORT( SCHEMATIC* aSchematic, EDA_UNITS aReportUnits );
 
+    /**
+     * Returns the ERC report in "text" (human readable) format
+     *
+     * @return The complete report
+     */
+    wxString GetTextReport();
+
+    /**
+     * Writes the text report also available via GetTextReport directly to a given file path
+     *
+     * @return True if the file write completed successfully, false otherwise
+     */
     bool WriteTextReport( const wxString& aFullFileName );
+
+    /**
+     * Writes a JSON formatted ERC Report to the given file path
+     *
+     * @return True if the file write completed successfully, false otherwise
+     */
     bool WriteJsonReport( const wxString& aFullFileName );
 
 private:
diff --git a/qa/tests/eeschema/erc/test_erc_global_labels.cpp b/qa/tests/eeschema/erc/test_erc_global_labels.cpp
index 66f6843c45..1e5f1f7f63 100644
--- a/qa/tests/eeschema/erc/test_erc_global_labels.cpp
+++ b/qa/tests/eeschema/erc/test_erc_global_labels.cpp
@@ -25,6 +25,7 @@
 #include <schematic.h>
 #include <erc_settings.h>
 #include <erc.h>
+#include <erc_report.h>
 #include <settings/settings_manager.h>
 #include <locale_io.h>
 
@@ -72,8 +73,12 @@ BOOST_FIXTURE_TEST_CASE( ERCGlobalLabels, ERC_REGRESSION_TEST_FIXTURE )
 
         errors.SetSeverities( RPT_SEVERITY_ERROR | RPT_SEVERITY_WARNING );
 
-        BOOST_CHECK_MESSAGE( errors.GetCount() == test.second, "Expected " << test.second
-            << " errors in " << test.first.ToStdString() << " but got " << errors.GetCount() );
+        ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MILLIMETRES );
+
+        BOOST_CHECK_MESSAGE( errors.GetCount() == test.second,
+                             "Expected " << test.second << " errors in " << test.first.ToStdString()
+                                         << " but got " << errors.GetCount() << "\n"
+                                         << reportWriter.GetTextReport() );
 
     }
 }
diff --git a/qa/tests/eeschema/erc/test_erc_hierarchical_schematics.cpp b/qa/tests/eeschema/erc/test_erc_hierarchical_schematics.cpp
index 85366427c9..313f1ed79b 100644
--- a/qa/tests/eeschema/erc/test_erc_hierarchical_schematics.cpp
+++ b/qa/tests/eeschema/erc/test_erc_hierarchical_schematics.cpp
@@ -25,6 +25,7 @@
 #include <schematic.h>
 #include <erc_settings.h>
 #include <erc.h>
+#include <erc_report.h>
 #include <settings/settings_manager.h>
 #include <locale_io.h>
 
@@ -74,8 +75,11 @@ BOOST_FIXTURE_TEST_CASE( ERCHierarchicalSchematics, ERC_REGRESSION_TEST_FIXTURE
 
         errors.SetSeverities( RPT_SEVERITY_ERROR | RPT_SEVERITY_WARNING );
 
+        ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MILLIMETRES );
+
         BOOST_CHECK_MESSAGE( errors.GetCount() == test.second,
                              "Expected " << test.second << " errors in " << test.first.ToStdString()
-                                         << " but got " << errors.GetCount() );
+                                         << " but got " << errors.GetCount() << "\n"
+                                         << reportWriter.GetTextReport() );
     }
 }
diff --git a/qa/tests/eeschema/erc/test_erc_label_not_connected.cpp b/qa/tests/eeschema/erc/test_erc_label_not_connected.cpp
index 6c47adae6b..99ef7440cd 100644
--- a/qa/tests/eeschema/erc/test_erc_label_not_connected.cpp
+++ b/qa/tests/eeschema/erc/test_erc_label_not_connected.cpp
@@ -25,6 +25,7 @@
 #include <schematic.h>
 #include <erc_settings.h>
 #include <erc.h>
+#include <erc_report.h>
 #include <settings/settings_manager.h>
 #include <locale_io.h>
 
@@ -77,8 +78,11 @@ BOOST_FIXTURE_TEST_CASE( ERCLabelNotConnected, ERC_REGRESSION_TEST_FIXTURE )
 
         errors.SetSeverities( RPT_SEVERITY_ERROR | RPT_SEVERITY_WARNING );
 
-        BOOST_CHECK_MESSAGE( errors.GetCount() == test.second, "Expected " << test.second
-            << " errors in " << test.first.ToStdString() << " but got " << errors.GetCount() );
+        ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MILLIMETRES );
+
+        BOOST_CHECK_MESSAGE( errors.GetCount() == test.second, "Expected " << test.second << " errors in " << test.first.ToStdString()
+                                         << " but got " << errors.GetCount() << "\n"
+                                         << reportWriter.GetTextReport() );
 
     }
 }
diff --git a/qa/tests/eeschema/erc/test_erc_no_connect.cpp b/qa/tests/eeschema/erc/test_erc_no_connect.cpp
index 8866efb48a..7dfa18c801 100644
--- a/qa/tests/eeschema/erc/test_erc_no_connect.cpp
+++ b/qa/tests/eeschema/erc/test_erc_no_connect.cpp
@@ -25,6 +25,7 @@
 #include <schematic.h>
 #include <erc_settings.h>
 #include <erc.h>
+#include <erc_report.h>
 #include <settings/settings_manager.h>
 #include <locale_io.h>
 
@@ -79,8 +80,11 @@ BOOST_FIXTURE_TEST_CASE( ERCNoConnect, ERC_REGRESSION_TEST_FIXTURE )
 
         errors.SetSeverities( RPT_SEVERITY_ERROR | RPT_SEVERITY_WARNING );
 
-        BOOST_CHECK_MESSAGE( errors.GetCount() == test.second, "Expected " << test.second
-            << " errors in " << test.first.ToStdString() << " but got " << errors.GetCount() );
+        ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MILLIMETRES );
+
+        BOOST_CHECK_MESSAGE( errors.GetCount() == test.second, "Expected " << test.second << " errors in " << test.first.ToStdString()
+                                         << " but got " << errors.GetCount() << "\n"
+                                         << reportWriter.GetTextReport() );
 
     }
 }
diff --git a/qa/tests/eeschema/erc/test_erc_stacking_pins.cpp b/qa/tests/eeschema/erc/test_erc_stacking_pins.cpp
index c0305a83de..3357f7262a 100644
--- a/qa/tests/eeschema/erc/test_erc_stacking_pins.cpp
+++ b/qa/tests/eeschema/erc/test_erc_stacking_pins.cpp
@@ -25,6 +25,7 @@
 #include <schematic.h>
 #include <erc_settings.h>
 #include <erc.h>
+#include <erc_report.h>
 #include <settings/settings_manager.h>
 #include <locale_io.h>
 
@@ -73,8 +74,11 @@ BOOST_FIXTURE_TEST_CASE( ERCStackingPins, ERC_REGRESSION_TEST_FIXTURE )
 
         errors.SetSeverities( RPT_SEVERITY_ERROR | RPT_SEVERITY_WARNING );
 
-        BOOST_CHECK_MESSAGE( errors.GetCount() == test.second, "Expected " << test.second
-            << " errors in " << test.first.ToStdString() << " but got " << errors.GetCount() );
+        ERC_REPORT reportWriter( m_schematic.get(), EDA_UNITS::MILLIMETRES );
+
+        BOOST_CHECK_MESSAGE( errors.GetCount() == test.second, "Expected " << test.second << " errors in " << test.first.ToStdString()
+                                         << " but got " << errors.GetCount() << "\n"
+                                         << reportWriter.GetTextReport() );
 
     }
 }