Skip to content

dump timezone and offset for junit reporter xml timestamp #2388

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.apiguardian.api.API.Status.STABLE;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -32,7 +33,7 @@
@API(status = STABLE, since = "1.0")
public final class ReportEntry {

private final LocalDateTime timestamp = LocalDateTime.now();
private final ZonedDateTime timestamp = ZonedDateTime.now();
private final Map<String, String> keyValuePairs = new LinkedHashMap<>();

/**
Expand Down Expand Up @@ -86,6 +87,17 @@ public final Map<String, String> getKeyValuePairs() {
* @return when this entry was created; never {@code null}
*/
public final LocalDateTime getTimestamp() {
return this.timestamp.toLocalDateTime();
}

/**
* Get the zoned timestamp for when and where this {@code ReportEntry} was created.
*
* <p>Can be used, for example, to order entries.
*
* @return when this entry was created; never {@code null}
*/
public final ZonedDateTime getZonedTimestamp() {
return this.timestamp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package org.junit.platform.reporting.legacy.xml;

import static java.text.MessageFormat.format;
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;
import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;
import static java.util.stream.Collectors.toList;
import static org.junit.platform.commons.util.ExceptionUtils.readStackTrace;
import static org.junit.platform.commons.util.StringUtils.isNotBlank;
Expand All @@ -23,7 +23,7 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.NumberFormat;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -116,7 +116,7 @@ private void writeSuiteAttributes(TestIdentifier testIdentifier, List<TestIdenti
writeTestCounts(tests, writer);
writeAttributeSafely(writer, "time", getTime(testIdentifier, numberFormat));
writeAttributeSafely(writer, "hostname", getHostname().orElse("<unknown host>"));
writeAttributeSafely(writer, "timestamp", ISO_LOCAL_DATE_TIME.format(getCurrentDateTime()));
writeAttributeSafely(writer, "timestamp", ISO_ZONED_DATE_TIME.format(getCurrentDateTime()));
}

private void writeTestCounts(List<TestIdentifier> tests, XMLStreamWriter writer) throws XMLStreamException {
Expand Down Expand Up @@ -236,7 +236,7 @@ private void collectReportEntries(TestIdentifier testIdentifier, List<String> sy
systemOutElementsForCapturedOutput);
removeIfPresentAndAddAsSeparateElement(keyValuePairs, STDERR_REPORT_ENTRY_KEY, systemErrElements);
if (!keyValuePairs.isEmpty()) {
buildReportEntryDescription(reportEntry.getTimestamp(), keyValuePairs, i + 1,
buildReportEntryDescription(reportEntry.getZonedTimestamp(), keyValuePairs, i + 1,
formattedReportEntries);
}
}
Expand All @@ -253,10 +253,10 @@ private void removeIfPresentAndAddAsSeparateElement(Map<String, String> keyValue
}
}

private void buildReportEntryDescription(LocalDateTime timestamp, Map<String, String> keyValuePairs,
private void buildReportEntryDescription(ZonedDateTime timestamp, Map<String, String> keyValuePairs,
int entryNumber, StringBuilder result) {
result.append(
format("Report Entry #{0} (timestamp: {1})\n", entryNumber, ISO_LOCAL_DATE_TIME.format(timestamp)));
format("Report Entry #{0} (timestamp: {1})\n", entryNumber, ISO_ZONED_DATE_TIME.format(timestamp)));
keyValuePairs.forEach((key, value) -> result.append(format("\t- {0}: {1}\n", key, value)));
}

Expand All @@ -273,8 +273,8 @@ private Optional<String> getHostname() {
}
}

private LocalDateTime getCurrentDateTime() {
return LocalDateTime.now(this.reportData.getClock()).withNano(0);
private ZonedDateTime getCurrentDateTime() {
return ZonedDateTime.now(this.reportData.getClock()).withNano(0);
}

private String formatNonStandardAttributesAsString(TestIdentifier testIdentifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.ZoneId;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -317,10 +316,11 @@ void writesHostNameAndTimestamp(@TempDir Path tempDirectory) throws Exception {
engine.addTest("test", () -> {
});

LocalDateTime now = LocalDateTime.parse("2016-01-28T14:02:59.123");
ZoneId zone = ZoneId.systemDefault();
String timestamp = "2016-01-28T14:02:59Z[Europe/London]";
Copy link
Member

Choose a reason for hiding this comment

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

❓ That's the format you want in the XML? Could you please double check that tools that read this XML report, e.g. Jenkins, don't choke on this?

Copy link
Author

Choose a reason for hiding this comment

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

Yep going to do the same for pytest too pytest-dev/pytest#7662 (comment)

ZonedDateTime now = ZonedDateTime.parse(timestamp);
ZoneId zone = now.getZone();

executeTests(engine, tempDirectory, Clock.fixed(ZonedDateTime.of(now, zone).toInstant(), zone));
executeTests(engine, tempDirectory, Clock.fixed(now.toInstant(), zone));

String content = readValidXmlFile(tempDirectory.resolve("TEST-dummy.xml"));

Expand All @@ -329,7 +329,7 @@ void writesHostNameAndTimestamp(@TempDir Path tempDirectory) throws Exception {
.containsSubsequence(
"<testsuite",
"hostname=\"" + InetAddress.getLocalHost().getHostName() + "\"",
"timestamp=\"2016-01-28T14:02:59\"",
"timestamp=\"" + timestamp + "\"",
"<testcase",
"</testsuite>");
// @formatter:on
Expand Down