Skip to content

Commit

Permalink
JENKINS-11413: Add maximum size limit to ${FAILED_TESTS}
Browse files Browse the repository at this point in the history
Add a maxTests argument to the ${FAILED_TESTS} token.

The setting is undefined by default for backwards compatibility with existing configurations.
  • Loading branch information
jukka committed Nov 23, 2011
1 parent dc7025b commit 5806079
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Expand Up @@ -21,16 +21,23 @@ public class FailedTestsContent implements EmailContent {

private static final String TOKEN = "FAILED_TESTS";

public static final String MAX_TESTS_ARG_NAME = "maxTests";

public String getToken() {
return TOKEN;
}

public List<String> getArguments() {
return Collections.emptyList();
return Collections.singletonList(MAX_TESTS_ARG_NAME);
}

public String getHelpText() {
return "Displays failing unit test information, if any tests have failed.";
return "Displays failing unit test information, if any tests have failed.\n"
+ "<ul>\n"
+ "<li><i>" + MAX_TESTS_ARG_NAME + "</i> - display at most this many failing tests.<br>\n"
+ "No limit is set by default.\n"
+ "</ul>\n";

}

public <P extends AbstractProject<P, B>, B extends AbstractBuild<P, B>> String getContent(AbstractBuild<P, B> build, ExtendedEmailPublisher publisher,
Expand All @@ -52,9 +59,20 @@ public <P extends AbstractProject<P, B>, B extends AbstractBuild<P, B>> String g
buffer.append(" tests failed.");
buffer.append('\n');

List<CaseResult> failedTests = testResult.getFailedTests();
for (CaseResult failedTest : failedTests) {
outputTest(buffer, failedTest);
int maxTests = Args.get(args, MAX_TESTS_ARG_NAME, Integer.MAX_VALUE);
if (maxTests > 0) {
int printedTests = 0;
for (CaseResult failedTest : testResult.getFailedTests()) {
if (printedTests < maxTests) {
outputTest(buffer, failedTest);
printedTests++;
}
}
if (failCount > printedTests) {
buffer.append("... and ");
buffer.append(failCount - printedTests);
buffer.append(" other failed tests.\n\n");
}
}
}

Expand Down
@@ -1,5 +1,8 @@
package hudson.plugins.emailext.plugins.content;

import java.util.Collections;
import java.util.Map;

import hudson.model.AbstractBuild;
import hudson.tasks.test.AbstractTestResultAction;
import org.junit.Before;
Expand Down Expand Up @@ -43,4 +46,20 @@ public void testGetContent_whenAllTestsPassedShouldGiveMeaningfulMessage()

assertEquals( "All tests passed", content );
}

@Test
public void testGetContent_whenSomeTestsFailedShouldGiveMeaningfulMessage()
{
AbstractTestResultAction<?> testResults = mock( AbstractTestResultAction.class );
when( testResults.getFailCount() ).thenReturn( 123 );

when( build.getTestResultAction() ).thenReturn( testResults );

Map<String, Integer> args = Collections.singletonMap(
FailedTestsContent.MAX_TESTS_ARG_NAME, 0 );
String content = failedTestContent.getContent( build, null, null, args );

assertEquals( "123 tests failed.\n", content );
}

}

0 comments on commit 5806079

Please sign in to comment.