mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-21 22:11:40 +07:00
[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:
committed by
intellij-monorepo-bot
parent
3d708aa8d7
commit
8b3d6afb4a
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,10 @@
|
||||
/*
|
||||
* 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.execution.junit;
|
||||
|
||||
import com.intellij.junit4.ExpectedPatterns;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.rt.execution.junit.ComparisonFailureData;
|
||||
import com.intellij.rt.execution.testFrameworks.AbstractExpectedPatterns;
|
||||
import org.junit.Assert;
|
||||
import org.junit.ComparisonFailure;
|
||||
import org.junit.Test;
|
||||
@@ -46,6 +34,38 @@ public class JUnitExpectedPatternsTest {
|
||||
but: was "bbb\\nbb\""""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageTooLongDefaultThreshold() {
|
||||
String longMessage = StringUtil.repeat("a", AbstractExpectedPatterns.DEFAULT_MESSAGE_LENGTH_THRESHOLD);
|
||||
Assert.assertNull(createNotification("""
|
||||
reason
|
||||
Expected: is "aaa\\naa"
|
||||
but: was \"""" + longMessage + "\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageTooLongOverrideThreshold() {
|
||||
try {
|
||||
System.setProperty(
|
||||
AbstractExpectedPatterns.MESSAGE_LENGTH_THRESHOLD_PROPERTY,
|
||||
Integer.toString(AbstractExpectedPatterns.DEFAULT_MESSAGE_LENGTH_THRESHOLD * 2)
|
||||
);
|
||||
String shortEnoughMessage = StringUtil.repeat("a", AbstractExpectedPatterns.DEFAULT_MESSAGE_LENGTH_THRESHOLD);
|
||||
Assert.assertNotNull(createNotification("""
|
||||
reason
|
||||
Expected: is "aaa\\naa"
|
||||
but: was \"""" + shortEnoughMessage + "\""));
|
||||
String longMessage = StringUtil.repeat("a", AbstractExpectedPatterns.DEFAULT_MESSAGE_LENGTH_THRESHOLD * 2);
|
||||
Assert.assertNull(createNotification("""
|
||||
reason
|
||||
Expected: is "aaa\\naa"
|
||||
but: was \"""" + longMessage + "\""));
|
||||
}
|
||||
finally {
|
||||
System.clearProperty(AbstractExpectedPatterns.MESSAGE_LENGTH_THRESHOLD_PROPERTY);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHamcrestAssertThatEqWithReason() {
|
||||
Assert.assertNotNull(createNotification("""
|
||||
|
||||
@@ -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.junit4;
|
||||
|
||||
import com.intellij.rt.execution.junit.ComparisonFailureData;
|
||||
@@ -36,8 +22,8 @@ public class ExpectedPatterns extends AbstractExpectedPatterns {
|
||||
"expecting:\\s+<(.*)> to be equal to:\\s+<(.*)>\\s+but was not"
|
||||
};
|
||||
|
||||
private static final String MESSAGE_LENGTH_FOR_PATTERN_MATCHING = "idea.junit.message.length.threshold";
|
||||
private static final String JUNIT_FRAMEWORK_COMPARISON_NAME = "junit.framework.ComparisonFailure";
|
||||
|
||||
private static final String ORG_JUNIT_COMPARISON_NAME = "org.junit.ComparisonFailure";
|
||||
|
||||
static {
|
||||
@@ -62,12 +48,8 @@ public class ExpectedPatterns extends AbstractExpectedPatterns {
|
||||
}
|
||||
|
||||
final String message = assertion.getMessage();
|
||||
if (message != null && acceptedByThreshold(message.length())) {
|
||||
try {
|
||||
|
||||
return createExceptionNotification(message);
|
||||
}
|
||||
catch (Throwable ignored) {}
|
||||
if (message != null) {
|
||||
return createExceptionNotification(message);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -87,20 +69,4 @@ public class ExpectedPatterns extends AbstractExpectedPatterns {
|
||||
}
|
||||
return isComparisonFailure(aClass.getSuperclass());
|
||||
}
|
||||
|
||||
|
||||
private static boolean acceptedByThreshold(int messageLength) {
|
||||
int threshold = 10000;
|
||||
try {
|
||||
final String property = System.getProperty(MESSAGE_LENGTH_FOR_PATTERN_MATCHING);
|
||||
if (property != null) {
|
||||
try {
|
||||
threshold = Integer.parseInt(property);
|
||||
}
|
||||
catch (NumberFormatException ignore) {}
|
||||
}
|
||||
}
|
||||
catch (SecurityException ignored) {}
|
||||
return messageLength < threshold;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// 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.testng;
|
||||
|
||||
import com.intellij.rt.execution.junit.ComparisonFailureData;
|
||||
@@ -29,6 +29,7 @@ class TestNGExpectedPatterns extends AbstractExpectedPatterns {
|
||||
}
|
||||
|
||||
public static ComparisonFailureData createExceptionNotification(String message) {
|
||||
if (exceedsMessageThreshold(message)) return null;
|
||||
ComparisonFailureData softAssertNotification = createExceptionNotification(message, SOFT_ASSERT_PATTERN);
|
||||
if (softAssertNotification != null) {
|
||||
return SOFT_ASSERT_CHAINED_PATTERN.matcher(softAssertNotification.getExpected()).find() ? null : softAssertNotification;
|
||||
|
||||
Reference in New Issue
Block a user