Skip to content

Commit 1c8b077

Browse files
committed
make chrono an optional dependency
1 parent b2fe203 commit 1c8b077

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ edition = "2021"
1212
rust-version = "1.70"
1313

1414
[features]
15-
default = ["uuids"]
15+
default = ["timestamps", "uuids"]
16+
timestamps = ["dep:chrono"]
1617
uuids = ["dep:uuid", "dep:newtype-uuid"]
1718

1819
[dependencies]
19-
chrono = { version = "0.4.41", default-features = false, features = ["std"] }
20+
chrono = { version = "0.4.41", default-features = false, features = ["std"], optional = true }
2021
indexmap = "2.7.1"
2122
quick-xml = "0.38.1"
2223
newtype-uuid = { version = "1.2.4", optional = true }

src/report.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: MIT OR Apache-2.0
33

44
use crate::{serialize::serialize_report, SerializeError};
5+
#[cfg(feature = "timestamps")]
56
use chrono::{DateTime, FixedOffset};
67
use indexmap::map::IndexMap;
78
#[cfg(feature = "uuids")]
@@ -41,6 +42,7 @@ pub struct Report {
4142
/// The time at which the first test in this report began execution.
4243
///
4344
/// This is not part of the JUnit spec, but may be useful for some tools.
45+
#[cfg(feature = "timestamps")]
4446
pub timestamp: Option<DateTime<FixedOffset>>,
4547

4648
/// The overall time taken by the test suite.
@@ -68,6 +70,7 @@ impl Report {
6870
name: name.into(),
6971
#[cfg(feature = "uuids")]
7072
uuid: None,
73+
#[cfg(feature = "timestamps")]
7174
timestamp: None,
7275
time: None,
7376
tests: 0,
@@ -96,6 +99,7 @@ impl Report {
9699
}
97100

98101
/// Sets the start timestamp for the report.
102+
#[cfg(feature = "timestamps")]
99103
pub fn set_timestamp(&mut self, timestamp: impl Into<DateTime<FixedOffset>>) -> &mut Self {
100104
self.timestamp = Some(timestamp.into());
101105
self
@@ -174,6 +178,7 @@ pub struct TestSuite {
174178
pub failures: usize,
175179

176180
/// The time at which the TestSuite began execution.
181+
#[cfg(feature = "timestamps")]
177182
pub timestamp: Option<DateTime<FixedOffset>>,
178183

179184
/// The overall time taken by the TestSuite.
@@ -201,6 +206,7 @@ impl TestSuite {
201206
Self {
202207
name: name.into(),
203208
time: None,
209+
#[cfg(feature = "timestamps")]
204210
timestamp: None,
205211
tests: 0,
206212
disabled: 0,
@@ -215,6 +221,7 @@ impl TestSuite {
215221
}
216222

217223
/// Sets the start timestamp for the TestSuite.
224+
#[cfg(feature = "timestamps")]
218225
pub fn set_timestamp(&mut self, timestamp: impl Into<DateTime<FixedOffset>>) -> &mut Self {
219226
self.timestamp = Some(timestamp.into());
220227
self
@@ -318,6 +325,7 @@ pub struct TestCase {
318325
/// The time at which this test case began execution.
319326
///
320327
/// This is not part of the JUnit spec, but may be useful for some tools.
328+
#[cfg(feature = "timestamps")]
321329
pub timestamp: Option<DateTime<FixedOffset>>,
322330

323331
/// The time it took to execute this test case.
@@ -346,6 +354,7 @@ impl TestCase {
346354
name: name.into(),
347355
classname: None,
348356
assertions: None,
357+
#[cfg(feature = "timestamps")]
349358
timestamp: None,
350359
time: None,
351360
status,
@@ -369,6 +378,7 @@ impl TestCase {
369378
}
370379

371380
/// Sets the start timestamp for the test case.
381+
#[cfg(feature = "timestamps")]
372382
pub fn set_timestamp(&mut self, timestamp: impl Into<DateTime<FixedOffset>>) -> &mut Self {
373383
self.timestamp = Some(timestamp.into());
374384
self
@@ -557,6 +567,7 @@ pub struct TestRerun {
557567
/// The time at which this rerun began execution.
558568
///
559569
/// This is not part of the JUnit spec, but may be useful for some tools.
570+
#[cfg(feature = "timestamps")]
560571
pub timestamp: Option<DateTime<FixedOffset>>,
561572

562573
/// The time it took to execute this rerun.
@@ -590,6 +601,7 @@ impl TestRerun {
590601
pub fn new(kind: NonSuccessKind) -> Self {
591602
TestRerun {
592603
kind,
604+
#[cfg(feature = "timestamps")]
593605
timestamp: None,
594606
time: None,
595607
message: None,
@@ -602,6 +614,7 @@ impl TestRerun {
602614
}
603615

604616
/// Sets the start timestamp for this rerun.
617+
#[cfg(feature = "timestamps")]
605618
pub fn set_timestamp(&mut self, timestamp: impl Into<DateTime<FixedOffset>>) -> &mut Self {
606619
self.timestamp = Some(timestamp.into());
607620
self

src/serialize.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
NonSuccessKind, Property, Report, SerializeError, TestCase, TestCaseStatus, TestRerun,
88
TestSuite, XmlString,
99
};
10+
#[cfg(feature = "timestamps")]
1011
use chrono::{DateTime, FixedOffset};
1112
use quick_xml::{
1213
events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event},
@@ -54,6 +55,7 @@ pub(crate) fn serialize_report_impl(
5455
name,
5556
#[cfg(feature = "uuids")]
5657
uuid,
58+
#[cfg(feature = "timestamps")]
5759
timestamp,
5860
time,
5961
tests,
@@ -73,6 +75,7 @@ pub(crate) fn serialize_report_impl(
7375
if let Some(uuid) = uuid {
7476
testsuites_tag.push_attribute(("uuid", uuid.to_string().as_str()));
7577
}
78+
#[cfg(feature = "timestamps")]
7679
if let Some(timestamp) = timestamp {
7780
serialize_timestamp(&mut testsuites_tag, timestamp);
7881
}
@@ -103,6 +106,7 @@ pub(crate) fn serialize_test_suite(
103106
errors,
104107
failures,
105108
time,
109+
#[cfg(feature = "timestamps")]
106110
timestamp,
107111
test_cases,
108112
properties,
@@ -120,6 +124,7 @@ pub(crate) fn serialize_test_suite(
120124
("failures", failures.to_string().as_str()),
121125
]);
122126

127+
#[cfg(feature = "timestamps")]
123128
if let Some(timestamp) = timestamp {
124129
serialize_timestamp(&mut test_suite_tag, timestamp);
125130
}
@@ -174,6 +179,7 @@ fn serialize_test_case(
174179
name,
175180
classname,
176181
assertions,
182+
#[cfg(feature = "timestamps")]
177183
timestamp,
178184
time,
179185
status,
@@ -192,6 +198,7 @@ fn serialize_test_case(
192198
testcase_tag.push_attribute(("assertions", format!("{assertions}").as_str()));
193199
}
194200

201+
#[cfg(feature = "timestamps")]
195202
if let Some(timestamp) = timestamp {
196203
serialize_timestamp(&mut testcase_tag, timestamp);
197204
}
@@ -308,6 +315,7 @@ fn serialize_rerun(
308315
writer: &mut Writer<impl io::Write>,
309316
) -> quick_xml::Result<()> {
310317
let TestRerun {
318+
#[cfg(feature = "timestamps")]
311319
timestamp,
312320
time,
313321
kind,
@@ -327,6 +335,7 @@ fn serialize_rerun(
327335
};
328336

329337
let mut tag = BytesStart::new(tag_name);
338+
#[cfg(feature = "timestamps")]
330339
if let Some(timestamp) = timestamp {
331340
serialize_timestamp(&mut tag, timestamp);
332341
}
@@ -411,6 +420,7 @@ fn serialize_end_tag(
411420
writer.write_event(Event::End(end_tag))
412421
}
413422

423+
#[cfg(feature = "timestamps")]
414424
fn serialize_timestamp(tag: &mut BytesStart<'_>, timestamp: &DateTime<FixedOffset>) {
415425
// The format string is obtained from https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html#fn8.
416426
// The only change is that this only prints timestamps up to 3 decimal places (to match times).

0 commit comments

Comments
 (0)