Skip to content

Commit 61d7c8a

Browse files
committed
Modify xml report in extension
1 parent 8639045 commit 61d7c8a

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

.circleci/collect_results.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ do
2727
echo -n " as $AGGREGATED_FILE_NAME"
2828
cp "$RESULT_XML_FILE" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
2929
# Extract source file from system-out and use as the file attribute for each test case
30-
path=$(grep -oP '(?<=path: ).*' "$RESULT_XML_FILE" | tail -n 1)
31-
testClassName=$(grep -oP '(?<=testClassName: ).*' "$RESULT_XML_FILE" | tail -n 1)
32-
escapedPath=$(echo "$path" | sed 's/[\/&]/\\&/g')
33-
escapedClassName=$(echo "$testClassName" | sed 's/[\/&]/\\&/g')
34-
sed -i "/<testcase/ s/\(classname=\"$escapedClassName\".*\)/\1 file=\"$escapedPath\"/" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
30+
# path=$(grep -oP '(?<=path: ).*' "$RESULT_XML_FILE" | tail -n 1)
31+
# testClassName=$(grep -oP '(?<=testClassName: ).*' "$RESULT_XML_FILE" | tail -n 1)
32+
# escapedPath=$(echo "$path" | sed 's/[\/&]/\\&/g')
33+
# escapedClassName=$(echo "$testClassName" | sed 's/[\/&]/\\&/g')
34+
# sed -i "/<testcase/ s/\(classname=\"$escapedClassName\".*\)/\1 file=\"$escapedPath\"/" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
3535
# sed -i '/<system-out>/,/<\/system-out>/d' "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
3636
# Replace Java Object hashCode by marker in testcase XML nodes to get stable test names
3737
sed -i '/<testcase/ s/@[0-9a-f]\{5,\}/@HASHCODE/g' "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"

components/context/src/main/resources/META-INF/services/org.junit.platform.launcher.listeners.TestExecutionListener renamed to components/context/src/main/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener

File renamed without changes.

components/context/src/test/java/datadog/context/SourceFileExtension.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package datadog.context;
22

3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
import java.util.Map;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
39
import org.junit.platform.engine.TestExecutionResult;
410
import org.junit.platform.launcher.TestExecutionListener;
511
import org.junit.platform.launcher.TestIdentifier;
@@ -16,8 +22,55 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
1622

1723
public void executionFinished(
1824
TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
19-
System.out.println("EXECUTIONFINISHED.");
20-
// get test source file info here
25+
// this should happen in testPlanExecutionFinished after all tests are run, but this method is
26+
// easier to test locally for now
27+
28+
Map<String, String> sourceFiles = TestSourceFileExtension.getSourceFiles();
29+
30+
// hard coded for now
31+
// get xml report file
32+
Path path =
33+
Paths.get(
34+
"/Users/sarah.chen/Source/github.com/DataDog/dd-trace-java/components/context/build/test-results/test/TEST-datadog.context.ContextKeyTest.xml");
35+
try {
36+
String content = new String(Files.readAllBytes(path));
37+
38+
// modify report with test source file info
39+
// use regex pattern to get class name
40+
Pattern pattern = Pattern.compile("<testcase(.*?)classname=\"(.*?)\"(.*?)>");
41+
Matcher matcher = pattern.matcher(content);
42+
StringBuffer result = new StringBuffer();
43+
while (matcher.find()) {
44+
String attributes = matcher.group(1);
45+
String className = matcher.group(2);
46+
String timeAttribute = matcher.group(3);
47+
48+
// add source file attribute
49+
String fileAttribute = "";
50+
if (sourceFiles.containsKey(className)) {
51+
fileAttribute = " file=\"" + sourceFiles.get(className) + "\"";
52+
}
53+
String newTestCase =
54+
"<testcase"
55+
+ attributes
56+
+ "classname=\""
57+
+ className
58+
+ "\""
59+
+ fileAttribute
60+
+ timeAttribute
61+
+ ">";
62+
matcher.appendReplacement(result, newTestCase);
63+
}
64+
// add the rest
65+
matcher.appendTail(result);
66+
67+
// set old path to new xml result
68+
// System.out.println("result: " + result); shows that result is correct, but haven't figured
69+
// out a way to overwrite the original path yet
70+
71+
} catch (Exception e) {
72+
System.out.println("didnt work.");
73+
}
2174
}
2275

2376
public void testPlanExecutionFinished(TestPlan testPlan) {
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
package datadog.context;
22

33
import java.net.URL;
4+
import java.util.HashMap;
5+
import java.util.Map;
46
import org.junit.jupiter.api.extension.ExtensionContext;
57
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
68

79
public class TestSourceFileExtension implements TestInstancePostProcessor {
10+
private static Map<String, String> sourceFiles = new HashMap<String, String>();
11+
812
@Override
913
public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
1014
getTestData(context);
1115
}
1216

13-
private static void getTestData(ExtensionContext context) {
14-
System.out.println("----------------------------------");
17+
public static Map<String, String> getSourceFiles() {
18+
return sourceFiles;
19+
}
1520

21+
private static void getTestData(ExtensionContext context) {
22+
// get test class and test class name
1623
Class<?> testClass = context.getTestClass().get();
1724
String testClassName = testClass.getName();
18-
System.out.println("testClassName: " + testClassName);
25+
// get URL of test class file
1926
URL resource = testClass.getResource(testClass.getSimpleName() + ".class");
2027
if (resource != null) {
28+
// get path after "dd-trace-java" and before the final "/"
2129
String absolutePath = resource.getPath();
2230
String subPath =
2331
absolutePath.substring(
2432
absolutePath.indexOf("dd-trace-java") + "dd-trace-java".length(),
2533
absolutePath.lastIndexOf("/"));
26-
System.out.println("path: " + subPath);
34+
// add the test class name and source file to sourceFiles
35+
sourceFiles.put(testClassName, subPath);
2736
} else {
28-
System.out.println("no path.");
37+
sourceFiles.put(testClassName, "");
2938
}
30-
31-
System.out.println("----------------------------------");
3239
}
3340
}

0 commit comments

Comments
 (0)