aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/exts/utils/snekbox/_eval.py23
-rw-r--r--tests/bot/exts/utils/snekbox/test_snekbox.py24
2 files changed, 30 insertions, 17 deletions
diff --git a/bot/exts/utils/snekbox/_eval.py b/bot/exts/utils/snekbox/_eval.py
index 95039f0bd..6bc7d7bb3 100644
--- a/bot/exts/utils/snekbox/_eval.py
+++ b/bot/exts/utils/snekbox/_eval.py
@@ -102,15 +102,30 @@ class EvalResult:
return msg
- def get_failed_files_str(self, char_max: int = 85, file_max: int = 5) -> str:
- """Return a string containing the names of failed files, truncated to lower of char_max and file_max."""
+ def get_failed_files_str(self, char_max: int = 85) -> str:
+ """
+ Return a string containing the names of failed files, truncated char_max.
+
+ Will truncate on whole file names if less than 3 characters remaining.
+ """
names = []
for file in self.failed_files:
- char_max -= len(file)
- if char_max < 0 or len(names) >= file_max:
+ # Only attempt to truncate name if more than 3 chars remaining
+ if char_max < 3:
names.append("...")
break
+
+ to_display = min(char_max, len(file))
+ name_short = file[:to_display]
+ # Add ellipsis if name was truncated
+ if to_display < len(file):
+ name_short += "..."
+ names.append(name_short)
+ break
+
+ char_max -= len(file)
names.append(file)
+
text = ", ".join(names)
# Since the file names are provided by user
text = escape_markdown(text)
diff --git a/tests/bot/exts/utils/snekbox/test_snekbox.py b/tests/bot/exts/utils/snekbox/test_snekbox.py
index b129bfcdb..faa849178 100644
--- a/tests/bot/exts/utils/snekbox/test_snekbox.py
+++ b/tests/bot/exts/utils/snekbox/test_snekbox.py
@@ -154,23 +154,21 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase):
@patch("bot.exts.utils.snekbox._eval.FILE_COUNT_LIMIT", 2)
def test_eval_result_files_error_str(self):
"""EvalResult.files_error_message, should return files error message."""
- max_file_name = "a" * 32
cases = [
+ # Normal
(["x.ini"], "x.ini"),
- (["dog.py", "cat.py"], "dog.py, cat.py"),
- # 3 files limit
- (["a", "b", "c"], "a, b, c"),
- (["a", "b", "c", "d"], "a, b, c, ..."),
- (["x", "y", "z"] + ["a"] * 100, "x, y, z, ..."),
- # 32 char limit
- ([max_file_name], max_file_name),
- ([max_file_name, "b"], f"{max_file_name}, ..."),
- ([max_file_name + "a"], "...")
+ (["123456", "879"], "123456, 879"),
+ # Break on whole name if less than 3 characters remaining
+ (["12345678", "9"], "12345678, ..."),
+ # Otherwise break on max chars
+ (["123", "345", "67890000"], "123, 345, 6789..."),
+ (["abcdefg1234567"], "abcdefg123..."),
]
for failed_files, expected in cases:
- result = EvalResult("", 0, [], failed_files)
- msg = result.get_failed_files_str(char_max=32, file_max=3)
- self.assertEqual(msg, expected)
+ with self.subTest(failed_files=failed_files, expected=expected):
+ result = EvalResult("", 0, [], failed_files)
+ msg = result.get_failed_files_str(char_max=10)
+ self.assertEqual(msg, expected)
@patch('bot.exts.utils.snekbox._eval.Signals', side_effect=ValueError)
def test_eval_result_message_invalid_signal(self, _mock_signals: Mock):