Commit 4f1813d5 authored by Eyrak Paen-Rochlitz's avatar Eyrak Paen-Rochlitz
Browse files

[etunit.converter] Add option to include log file output of test cases

parent ec7b2714
......@@ -18,6 +18,7 @@ package org.eclipse.etrice.etunit.converter;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
......@@ -56,10 +57,12 @@ public class EtUnitReportConverter {
private boolean combinedResults = false;
private boolean replaceSuiteName = false;
private boolean prefixSuiteName = false;
private boolean hasTestcaseLogPath = false;
private boolean onlyCombinedResults = false;
private String combinedFile = null;
private String suiteName = null;
private String suiteNamePrefix = null;
private String testcaseLogPath = null;
/** check that etUnit header line is present */
private boolean checkEtUnitHeader = true;
......@@ -95,6 +98,14 @@ public class EtUnitReportConverter {
public void setOnlyCombinedResults(boolean onlyCombinedResults) {
this.onlyCombinedResults = onlyCombinedResults;
}
public boolean hasTestcaseLogPath() {
return this.hasTestcaseLogPath;
}
public void setHasTestcaseLogPath(boolean hasTestcaseLogPath) {
this.hasTestcaseLogPath = hasTestcaseLogPath;
}
public String getCombinedFile() {
return combinedFile;
......@@ -131,6 +142,14 @@ public class EtUnitReportConverter {
public void setCheckEtUnitHeader(boolean checkEtUnitHeader) {
this.checkEtUnitHeader = checkEtUnitHeader;
}
public String getTestcaseLogPath() {
return this.testcaseLogPath;
}
public void setTestcaseLogPath(String testcaseLogPath) {
this.testcaseLogPath = testcaseLogPath;
}
}
public static class Options extends BaseOptions {
......@@ -187,6 +206,16 @@ public class EtUnitReportConverter {
return false;
}
}
else if(args[i].equals(OPTION_TESTCASE_LOG_PATH)) {
setHasTestcaseLogPath(true);
if(++i < args.length) {
setTestcaseLogPath(args[i]);
}
else {
System.err.println("Error: " + OPTION_TESTCASE_LOG_PATH + "must be followed by a file path pattern");
return false;
}
}
else if (args[i].startsWith("-")) {
int nextOption = parseOption(args, i);
if (nextOption<0) {
......@@ -232,6 +261,7 @@ public class EtUnitReportConverter {
public static final String OPTION_ONLY_COMBINED = "-only_combined";
public static final String OPTION_SUITE_NAME = "-suite";
public static final String OPTION_SUITE_NAME_PREFIX = "-presuite";
public static final String OPTION_TESTCASE_LOG_PATH = "-testcase_log_path";
protected void printUsage() {
System.err.println("usage: EtUnitReportConverter [("+OPTION_COMBINED+"|"+OPTION_ONLY_COMBINED+") <combined file>] ["+OPTION_SUITE_NAME+" <name>] <*"+ETU_EXTENSION+" files>\n"
......@@ -239,6 +269,7 @@ public class EtUnitReportConverter {
+" "+OPTION_ONLY_COMBINED+" <combined file>: don't create reports for every single test, only combined one to the specified file\n"
+" "+OPTION_SUITE_NAME+" <name>: replace the suite name in the result\n"
+" "+OPTION_SUITE_NAME_PREFIX+" <prefix>: prefix the prefix to the suitename\n"
+" "+OPTION_TESTCASE_LOG_PATH+" <path>: use the tags #TESTSUITE# and #TESTCASE# to substitute the test suite name and test case name, respectively. To escape either tag, prepend with an additional #.\n"
);
}
......@@ -372,6 +403,19 @@ public class EtUnitReportConverter {
protected DocumentRoot applyOptions(BaseOptions options, DocumentRoot root) {
if (root != null && root.getTestsuites() != null) {
List<TestsuiteType> suites = root.getTestsuites().getTestsuite();
if (options.hasTestcaseLogPath()) {
for (TestsuiteType suite : suites) {
for (TestcaseType tc : suite.getTestcase()) {
String path = createLogPath(suite.getName(), tc.getName(), options.getTestcaseLogPath());
String content = getLogProvider().getLog(path);
if (content != null && !content.isEmpty()) {
// Relies on escaping behavior of EMF XMI serialization, meaning that if control
// chars are present a RuntimeException will be thrown!
tc.getSystemOut().add(getLogProvider().getLog(path));
}
}
}
}
if(options.isReplaceSuiteName()) {
if (suites.size()==1) {
suites.get(0).setName(options.getSuiteName());
......@@ -577,4 +621,48 @@ public class EtUnitReportConverter {
EPackage.Registry.INSTANCE.put("platform:/resource/org.eclipse.etrice.etunit.converter/model/etunit.xsd", EtunitPackage.eINSTANCE);
}
}
private String createLogPath(String suiteName, String testcaseName, String testcaseLogPath) {
String result = testcaseLogPath;
// Perform tag substitution
result = result.replaceAll("(?<!#)#testcase#", testcaseName);
result = result.replaceAll("(?<!#)#testsuite#", suiteName);
// Handle escaped tags
result = result.replaceAll("##testcase#", "#testcase#");
result = result.replaceAll("##testsuite#", "#testsuite#");
return result;
}
public interface ILogProvider {
/**
* Retrieves the log content corresponding to a given path.
*
* @param path The log path
* @return The contents of the log, empty string if there is no log, or null if
* an error occurred when retrieving.
*/
public String getLog(String path);
}
/**
* Default file-based implementation of log provider.
*/
private class DefaultFileLogProvider implements ILogProvider {
public String getLog(String path) {
try (FileReader log = new FileReader(path); BufferedReader bufLog = new BufferedReader(log)) {
StringBuilder builder = new StringBuilder();
// NOTE: LF character is used when appending the line since the target is XML content
bufLog.lines().forEach(line -> builder.append(line + '\n'));
return builder.toString();
} catch (FileNotFoundException e) {
return "";
} catch (IOException e) {
return null;
}
}
}
protected ILogProvider getLogProvider() {
return new DefaultFileLogProvider();
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment