Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for actual failure or error nodes on testcase #286

Merged
merged 1 commit into from
Jul 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jenkins.tools.test.exception.ExecutedTestNamesSolverException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
Expand All @@ -24,33 +22,41 @@
public class ExecutedTestNamesSolver {

private static final String WARNING_MSG = "[WARNING] Unable to retrieve info from: %s";

private static final String TEST_PLACEHOLDER = "TEST-%s.xml";


/*
Element names for failure and error as declared at
https://maven.apache.org/surefire/maven-failsafe-plugin/xsd/failsafe-test-report-3.0.xsd and
https://gitbox.apache.org/repos/asf?p=maven-surefire.git;a=blob;f=maven-surefire-plugin/src/site/resources/xsd/surefire-test-report-3.0.xsd
*/
private static final String FAILURE_ELEMENT = "failure";
private static final String ERROR_ELEMENT = "error";

public ExecutedTestNamesDetails solve(Set<String> types, Set<String> executedTests, File baseDirectory) throws ExecutedTestNamesSolverException {

System.out.println("[INFO] -------------------------------------------------------");
System.out.println("[INFO] Solving test names");
System.out.println("[INFO] -------------------------------------------------------");

ExecutedTestNamesDetails testNames = new ExecutedTestNamesDetails();

List<String> reportsDirectoryPaths = getReportsDirectoryPaths(types, baseDirectory);
if(reportsDirectoryPaths.isEmpty()) {
if (reportsDirectoryPaths.isEmpty()) {
System.out.println("[WARNING] No test reports found!");
return testNames;
}
for (String reportsDirectoryPath: reportsDirectoryPaths) {

for (String reportsDirectoryPath : reportsDirectoryPaths) {
try {
File reportsDirectory = Paths.get(reportsDirectoryPath).toFile();
if (!reportsDirectory.exists()) {
System.out.println(String.format(WARNING_MSG, reportsDirectoryPath));
return testNames;
}

System.out.println(String.format("[INFO] Reading %s", reportsDirectoryPath));

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
for (String testName : executedTests) {
String reference = String.format(TEST_PLACEHOLDER, testName);
Expand All @@ -60,7 +66,7 @@ public ExecutedTestNamesDetails solve(Set<String> types, Set<String> executedTes
System.out.println(String.format(WARNING_MSG, testReportPath));
continue;
}

Document document = builder.parse(testReport);
Node testsuite = document.getChildNodes().item(0);
String nodeValue = testsuite.getAttributes().getNamedItem("tests").getNodeValue();
Expand All @@ -80,19 +86,19 @@ public ExecutedTestNamesDetails solve(Set<String> types, Set<String> executedTes
}
}
}

if (testCount.intValue() != found) {
System.out.println(String.format("[WARNING] Extracted: %s, Expected: %s from %s", found, testCount, testReportPath));
} else {
System.out.println(String.format("[INFO] Extracted %s testnames from %s", testCount, testReportPath));
}
}

} catch (ParserConfigurationException | SAXException | IOException e) {
throw new ExecutedTestNamesSolverException(e);
}
}

System.out.println("[INFO] ");
System.out.println("[INFO] Results:");
System.out.println("[INFO] ");
Expand All @@ -105,7 +111,7 @@ public ExecutedTestNamesDetails solve(Set<String> types, Set<String> executedTes
for (String testName : testNames.getFailed()) {
System.out.println(String.format("[INFO] - %s", testName));
}

return testNames;
}

Expand All @@ -114,7 +120,7 @@ private List<String> getReportsDirectoryPaths(Set<String> types, File baseDirect
if (types == null) {
return paths;
}
for(String type: types) {
for (String type : types) {
try (Stream<Path> walk = Files.walk(Paths.get(baseDirectory.getAbsolutePath()))) {
List<Path> result = walk.filter(Files::isDirectory)
.filter(file -> file.getFileName().toString().endsWith(String.format("%s-reports", type)))
Expand All @@ -124,23 +130,21 @@ private List<String> getReportsDirectoryPaths(Set<String> types, File baseDirect
}
} catch (IOException e) {
throw new ExecutedTestNamesSolverException(e);
}
}
}
return paths;
}

private boolean containsFailure(NodeList nodeList) {
if (nodeList.getLength() == 0) {
return false;
}

for (int j = 0; j < nodeList.getLength(); j++) {
if (nodeList.item(j).getNodeName().equals("skipped")) {
return false;
String elementName = nodeList.item(j).getNodeName();
if (elementName.equals(FAILURE_ELEMENT)
|| elementName.equals(ERROR_ELEMENT)) {
return true;
}
}
return true;

return false;
}

}