[test] Don't parse expected/actuals for very long messages

The default threshold is 10_000 characters but can be changed by using setting the `idea.expected.message.length.threshold` property key. #IDEA-347460 Fixed

GitOrigin-RevId: 2c5ab995aab90eed2be001a2962bb8a42e984a7a
This commit is contained in:
Bart van Helvert
2024-02-29 10:55:31 +01:00
committed by intellij-monorepo-bot
parent 3d708aa8d7
commit 8b3d6afb4a
4 changed files with 78 additions and 69 deletions

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.rt.execution.testFrameworks;
import com.intellij.rt.execution.junit.ComparisonFailureData;
@@ -26,6 +12,14 @@ public class AbstractExpectedPatterns {
private static final Pattern ASSERT_EQUALS_PATTERN = Pattern.compile("expected:<(.*)> but was:<(.*)>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
private static final Pattern ASSERT_EQUALS_CHAINED_PATTERN = Pattern.compile("but was:<(.*)>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
/**
* System property to specify the maximum threshold for expected patterns.
* When the message length exceeds this threshold, we won't parse the message because running regex over such messages will be very slow.
*/
public static final String MESSAGE_LENGTH_THRESHOLD_PROPERTY = "idea.expected.message.length.threshold";
public static final int DEFAULT_MESSAGE_LENGTH_THRESHOLD = 10_000;
protected static void registerPatterns(String[] patternStrings, List<Pattern> patterns) {
for (String string : patternStrings) {
patterns.add(Pattern.compile(string, Pattern.DOTALL | Pattern.CASE_INSENSITIVE));
@@ -33,6 +27,7 @@ public class AbstractExpectedPatterns {
}
protected static ComparisonFailureData createExceptionNotification(String message, List<Pattern> patterns) {
if (exceedsMessageThreshold(message)) return null;
ComparisonFailureData assertEqualsNotification = createExceptionNotification(message, ASSERT_EQUALS_PATTERN);
if (assertEqualsNotification != null) {
return ASSERT_EQUALS_CHAINED_PATTERN.matcher(assertEqualsNotification.getExpected()).find() ? null : assertEqualsNotification;
@@ -48,6 +43,7 @@ public class AbstractExpectedPatterns {
}
protected static ComparisonFailureData createExceptionNotification(String message, Pattern pattern) {
if (exceedsMessageThreshold(message)) return null;
final Matcher matcher = pattern.matcher(message);
if (matcher.find() && matcher.end() == message.length()) {
return new ComparisonFailureData(matcher.group(1).replaceAll("\\\\n", "\n"),
@@ -55,4 +51,30 @@ public class AbstractExpectedPatterns {
}
return null;
}
/**
* @return whether the size of the message is too big to parse.
*/
protected static boolean exceedsMessageThreshold(String message) {
return message.length() > getMessageThreshold();
}
private static int getMessageThreshold() {
int threshold = DEFAULT_MESSAGE_LENGTH_THRESHOLD;
try {
String property = System.getProperty(MESSAGE_LENGTH_THRESHOLD_PROPERTY);
if (property == null) property = System.getProperty("idea.junit.message.length.threshold"); // legacy property that was used for JUnit
if (property != null) {
try {
threshold = Integer.parseInt(property);
}
catch (NumberFormatException ignore) {
}
}
return threshold;
}
catch (SecurityException ignore) {
}
return threshold;
}
}