From e7cb30f9bccaf2f614edb99f1468aed50d01941f Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Sun, 17 Dec 2023 08:56:08 -0500
Subject: [PATCH] Add ability to regex away sections of output for debug print
 on test failure

---
 django_expanded_test_cases/constants.py             |  8 ++++++++
 django_expanded_test_cases/mixins/response_mixin.py | 12 ++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/django_expanded_test_cases/constants.py b/django_expanded_test_cases/constants.py
index f5664aa..c3b7952 100644
--- a/django_expanded_test_cases/constants.py
+++ b/django_expanded_test_cases/constants.py
@@ -180,6 +180,14 @@ ETC_DEBUG_PRINT = bool(getattr(
     'DJANGO_EXPANDED_TESTCASES_DEBUG_PRINT',
     True,
 ))
+# A set of regex-matching strings to skip displaying during debug output.
+# Useful such as when importing third-party libraries with front-end elements, if you don't expect to ever
+# need to test for said elements.
+ETC_DEBUG_PRINT__SKIP_DISPLAY = getattr(
+    settings,
+    'DJANGO_EXPANDED_TESTCASES_DEBUG_PRINT__SKIP_DISPLAY',
+    [],
+)
 # Indicates whether partial matches are allowed for site title assertions.
 # IE: Will pass as long as provided text appears as a subsection of the full site title.
 # Defaults to needing exact match.
diff --git a/django_expanded_test_cases/mixins/response_mixin.py b/django_expanded_test_cases/mixins/response_mixin.py
index 97377f2..7a41d12 100644
--- a/django_expanded_test_cases/mixins/response_mixin.py
+++ b/django_expanded_test_cases/mixins/response_mixin.py
@@ -28,6 +28,7 @@ from django_expanded_test_cases.constants import (
     ETC_RESPONSE_DEBUG_USER_INFO_COLOR,
     ETC_OUTPUT_EMPHASIS_COLOR,
     ETC_AUTO_GENERATE_USERS,
+    ETC_DEBUG_PRINT__SKIP_DISPLAY
 )
 
 
@@ -84,6 +85,17 @@ class ResponseTestCaseMixin(CoreTestCaseMixin):
 
         # Print out data, if present.
         if response_content:
+
+            # Attempt to remove all regex matches from content to display.
+            for match_attempt in ETC_DEBUG_PRINT__SKIP_DISPLAY:
+
+                # For each string, modify to match output formatting and convert to regex.
+                match_attempt = r'{0}'.format(self.get_minimized_response_content(match_attempt))
+
+                # Attempt to do content strip.
+                response_content = re.sub(match_attempt, '', response_content)
+
+            # Display content to console (only shows up on test error).
             self._debug_print(response_content)
             self._debug_print()
 
-- 
GitLab