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

[WIP] Add file attribute to JUnit reports #8216

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions .circleci/collect_results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ if [[ ${#TEST_RESULT_DIRS[@]} -eq 0 ]]; then
exit 0
fi

# Read sourceFile.xml into map
declare -A SOURCE_FILE_MAP
SOURCE_FILE_XML="test-results/sourceFiles.xml"
while IFS= read -r line
do
KEY=$(echo "$line" | cut -d ":" -f 1)
VALUE=$(echo "$line" | cut -d ":" -f 2)
SOURCE_FILE_MAP["$KEY"]="$VALUE"
done < "$SOURCE_FILE_XML"

echo "Saving test results:"
while IFS= read -r -d '' RESULT_XML_FILE
do
Expand All @@ -30,6 +40,13 @@ do
sed -i '/<testcase/ s/@[0-9a-f]\{5,\}/@HASHCODE/g' "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
# Replace random port numbers by marker in testcase XML nodes to get stable test names
sed -i '/<testcase/ s/localhost:[0-9]\{2,5\}/localhost:PORT/g' "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"

# Insert file attribute to testcases
for CLASSNAME in "${!SOURCE_FILE_MAP[@]}"; do
SOURCE_FILE="${SOURCE_FILE_MAP[CLASSNAME]}"
sed -i "/<testcase.*classname=$CLASSNAME/ s/\(time=\"[^\"]*\"\)/file=$SOURCE_FILE \1/g" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
done

if cmp -s "$RESULT_XML_FILE" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"; then
echo ""
else
Expand Down
4 changes: 4 additions & 0 deletions components/context/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ jmh {
val excludedClassesInstructionCoverage by extra {
listOf("datadog.context.ContextProviders") // covered by forked test
}

tasks.test {
jvmArgs("-Djunit.jupiter.extensions.autodetection.enabled=true")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
datadog.context.GetSourceFileInfoExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package datadog.context;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extension should probably live at ./utils/test-utils.


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;

public class GetSourceFileInfoExtension implements TestInstancePostProcessor {
private static Map<String, String> sourceFiles = new HashMap<String, String>();

@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context)
throws IOException {
getTestData(context);
}

public static Map<String, String> getSourceFiles() {
return sourceFiles;
}

private static void getTestData(ExtensionContext context) throws IOException {
// get test classname and source file
String testClassName = context.getTestClass().get().getName();
String testClassPath = testClassName.replace(".", "/") + ".java";
String root = Paths.get("").toAbsolutePath().toString();
String absolutePath = root + "/src/test/java/" + testClassPath;
String subPath =
absolutePath.substring(absolutePath.indexOf("dd-trace-java") + "dd-trace-java".length());

// print to sourceFiles.xml only if source file has not already been added
if (!sourceFiles.containsKey(testClassName)) {
File sourceFile = new File(root + "/build/test-results/sourceFiles.xml");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we're not outputting here XML, but some custom format? We should use something like .dat or .txt here rather than .xml.

if (!sourceFile.exists()) {
sourceFile.createNewFile();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(sourceFile, true));
writer.write(testClassName + ":" + subPath);
writer.newLine();
writer.close();
}
sourceFiles.put(testClassName, subPath);
}
}
17 changes: 17 additions & 0 deletions gradle/java.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,20 @@ apply plugin: 'datadog.dependency-locking'

apply from: "$rootDir/gradle/java_deps.gradle"
apply from: "$rootDir/gradle/java_no_deps.gradle"

tasks.named("test") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's more test tasks than these two. You may use something like:

tasks.withType(Test).configureEach {
  finalizedBy("myCustomTask")
}

to make sure this runs after all of them.

finalizedBy("myCustomTask")
}

tasks.named("forkedTest") {
finalizedBy("myCustomTask")
}

tasks.register('myCustomTask') {
group = "Custom Tasks" // Optional, for better organization
description = "This is a custom task that prints a message"

doLast {
println("Running my custom task!")
}
}
1 change: 1 addition & 0 deletions gradle/java_no_deps.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ext.testcontainersLimit = gradle.sharedServices.registerIfAbsent("testcontainers
if (tasks.matching({it.name == 'forkedTest'}).empty) {
tasks.register('forkedTest', Test).configure {
useJUnitPlatform()
jvmArgs("-Djunit.jupiter.extensions.autodetection.enabled=true")
}
}

Expand Down
Loading