Skip to content

Commit 295d7cf

Browse files
committed
Fix retrieving source files logic
1 parent 9465218 commit 295d7cf

File tree

5 files changed

+40
-48
lines changed

5 files changed

+40
-48
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
datadog.context.TestSourceFileExtension
1+
datadog.context.GatherSourceFileInfoExtension
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
datadog.context.SourceFileExtension
1+
datadog.context.InsertSourceFileExtension
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package datadog.context;
2+
3+
import java.nio.file.Paths;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import org.junit.jupiter.api.extension.ExtensionContext;
7+
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
8+
9+
public class GatherSourceFileInfoExtension implements TestInstancePostProcessor {
10+
private static Map<String, String> sourceFiles = new HashMap<String, String>();
11+
12+
@Override
13+
public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
14+
getTestData(context);
15+
}
16+
17+
public static Map<String, String> getSourceFiles() {
18+
return sourceFiles;
19+
}
20+
21+
private static void getTestData(ExtensionContext context) {
22+
// get test class name and source file
23+
String testClassName = context.getTestClass().get().getName();
24+
String testClassPath = testClassName.replace(".", "/") + ".java";
25+
String absolutePath = Paths.get("").toAbsolutePath() + "/src/test/java/" + testClassPath;
26+
String subPath =
27+
absolutePath.substring(absolutePath.indexOf("dd-trace-java") + "dd-trace-java".length());
28+
// add info to sourceFiles map
29+
sourceFiles.put(testClassName, subPath);
30+
}
31+
}

components/context/src/test/java/datadog/context/SourceFileExtension.java renamed to components/context/src/test/java/datadog/context/InsertSourceFileExtension.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.junit.platform.launcher.TestIdentifier;
1414
import org.junit.platform.launcher.TestPlan;
1515

16-
public class SourceFileExtension implements TestExecutionListener {
16+
public class InsertSourceFileExtension implements TestExecutionListener {
1717
public void executionStarted(TestIdentifier testIdentifier) {
1818
System.out.println("EXECUTIONSTARTED.");
1919
}
@@ -24,10 +24,9 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
2424

2525
public void executionFinished(
2626
TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
27-
// this should happen in testPlanExecutionFinished after all tests are run, but this method is
28-
// easier to test locally for now
27+
// should this happen in testPlanExecutionFinished after all tests are run?
2928

30-
Map<String, String> sourceFiles = TestSourceFileExtension.getSourceFiles();
29+
Map<String, String> sourceFiles = GatherSourceFileInfoExtension.getSourceFiles();
3130

3231
// for each test
3332
for (String sourceFile : sourceFiles.keySet()) {
@@ -69,10 +68,13 @@ public void executionFinished(
6968
matcher.appendTail(result);
7069

7170
// set old filePath to new xml result
72-
// System.out.println("result: " + result);
71+
System.out.println("result: " + result.substring(0, 1000));
72+
73+
// i think this logic gets re-overwritten by xml reports
7374
BufferedWriter writer = new BufferedWriter(new FileWriter(pathString));
7475
writer.write(result.toString());
7576
writer.close();
77+
7678
} catch (Exception e) {
7779
System.out.println("Modifying XML files did not work.");
7880
}
@@ -81,8 +83,5 @@ public void executionFinished(
8183

8284
public void testPlanExecutionFinished(TestPlan testPlan) {
8385
System.out.println("TESTPLANEXECUTIONFINISHED.");
84-
// get xml report file
85-
// modify report with test source file info
86-
// save resulting file
8786
}
8887
}

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

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)