Skip to content

Commit

Permalink
Clean PR
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahchen6 committed Jan 10, 2025
1 parent de1d722 commit 114bead
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
7 changes: 0 additions & 7 deletions .circleci/collect_results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ do
AGGREGATED_FILE_NAME=$(echo "$RESULT_XML_FILE" | rev | cut -d "/" -f 1,2,5 | rev | tr "/" "_")
echo -n " as $AGGREGATED_FILE_NAME"
cp "$RESULT_XML_FILE" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
# Extract source file from system-out and use as the file attribute for each test case
# path=$(grep -oP '(?<=path: ).*' "$RESULT_XML_FILE" | tail -n 1)
# testClassName=$(grep -oP '(?<=testClassName: ).*' "$RESULT_XML_FILE" | tail -n 1)
# escapedPath=$(echo "$path" | sed 's/[\/&]/\\&/g')
# escapedClassName=$(echo "$testClassName" | sed 's/[\/&]/\\&/g')
# sed -i "/<testcase/ s/\(classname=\"$escapedClassName\".*\)/\1 file=\"$escapedPath\"/" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
# sed -i '/<system-out>/,/<\/system-out>/d' "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
# Replace Java Object hashCode by marker in testcase XML nodes to get stable test names
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -17,8 +18,8 @@ class ContextKeyTest {
@ValueSource(strings = {"key"})
void testConstructor(String name) {
ContextKey<String> key = ContextKey.named(name);
System.out.println("*test fails*");
assertNull(key, "created key should not be null");
assertNotNull(key, "created key should not be null");
// assertEquals fails. remove '+ "X"' to fix.
assertEquals(name, key.toString() + "X", name + " label should be supported");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static Map<String, String> getSourceFiles() {
}

private static void getTestData(ExtensionContext context) {
// get test class name and source file
// get test classname and source file
String testClassName = context.getTestClass().get().getName();
String testClassPath = testClassName.replace(".", "/") + ".java";
String absolutePath = Paths.get("").toAbsolutePath() + "/src/test/java/" + testClassPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.regex.Matcher;
Expand All @@ -14,32 +13,32 @@
import org.junit.platform.launcher.TestPlan;

public class InsertSourceFileExtension implements TestExecutionListener {
@Override
public void executionStarted(TestIdentifier testIdentifier) {
System.out.println("EXECUTIONSTARTED.");
System.out.println(testIdentifier.getDisplayName() + " test started.");
}

@Override
public void testPlanExecutionStarted(TestPlan testPlan) {
System.out.println("TESTPLANEXECUTIONSTARTED.");
// does not print when tested locally
System.out.println("---testPlanExecutionStarted---");
}

@Override
public void executionFinished(
TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
// should this happen in testPlanExecutionFinished after all tests are run?

// get mapping of test classname to source file
Map<String, String> sourceFiles = GatherSourceFileInfoExtension.getSourceFiles();

// for each test
// for each test...
for (String sourceFile : sourceFiles.keySet()) {
String pathString =
Paths.get("").toAbsolutePath() + "/build/test-results/test/TEST-" + sourceFile + ".xml";

// get xml report file
Path filePath = Paths.get(pathString);
String filePath =
Paths.get("").toAbsolutePath() + "/build/test-results/test/TEST-" + sourceFile + ".xml";
try {
String fileContent = new String(Files.readAllBytes(filePath));
String fileContent = new String(Files.readAllBytes(Paths.get(filePath)));

// modify report with test source file info
// use regex pattern to get class name
// add test source file info to report
Pattern pattern = Pattern.compile("<testcase(.*?)classname=\"(.*?)\"(.*?)>");
Matcher matcher = pattern.matcher(fileContent);
StringBuffer result = new StringBuffer();
Expand All @@ -48,11 +47,7 @@ public void executionFinished(
String className = matcher.group(2);
String endAttributes = matcher.group(3);

// add source file attribute
String fileAttribute = "";
if (sourceFiles.containsKey(className)) {
fileAttribute = " file=\"" + sourceFiles.get(className) + "\"";
}
String fileAttribute = " file=\"" + sourceFiles.getOrDefault(className, "") + "\"";
String newTestCase =
"<testcase"
+ begAttributes
Expand All @@ -64,24 +59,23 @@ public void executionFinished(
+ ">";
matcher.appendReplacement(result, newTestCase);
}
// add the rest
matcher.appendTail(result);

// set old filePath to new xml result
System.out.println("result: " + result.substring(0, 1000));

// i think this logic gets re-overwritten by xml reports
BufferedWriter writer = new BufferedWriter(new FileWriter(pathString));
// this logic must be wrong or go elsewhere bc its getting overwritten. `result` output
// seems correct.
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
writer.write(result.toString());
writer.close();

} catch (Exception e) {
System.out.println("Modifying XML files did not work.");
}
}
}

@Override
public void testPlanExecutionFinished(TestPlan testPlan) {
System.out.println("TESTPLANEXECUTIONFINISHED.");
// does not print when tested locally
System.out.println("---testPlanExecutionFinished---.");
}
}

0 comments on commit 114bead

Please sign in to comment.