-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathjunit.rs
78 lines (71 loc) · 2.15 KB
/
junit.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::{fs, io::Read as _};
use cucumber::{given, then, when, writer, World as _};
use futures::FutureExt as _;
use regex::RegexBuilder;
use tempfile::NamedTempFile;
use tracing_subscriber::{
filter::LevelFilter,
fmt::{
self,
format::{DefaultFields, Format},
},
layer::SubscriberExt as _,
Layer as _,
};
#[given(regex = r"(\d+) secs?")]
#[when(regex = r"(\d+) secs?")]
#[then(regex = r"(\d+) secs?")]
fn step(world: &mut World) {
world.0 += 1;
assert!(world.0 < 4, "Too much!");
tracing::info!("step");
}
#[tokio::test]
async fn output() {
let mut file = NamedTempFile::new().unwrap();
drop(
World::cucumber()
.before(|_, _, _, _| {
async { tracing::info!("before") }.boxed_local()
})
.after(|_, _, _, _, _| {
async { tracing::info!("after") }.boxed_local()
})
.with_writer(writer::JUnit::new(file.reopen().unwrap(), 1))
.fail_on_skipped()
.with_default_cli()
.configure_and_init_tracing(
fmt::layer().fmt_fields(DefaultFields::new()).event_format(
Format::default().with_ansi(false).without_time(),
),
|layer| {
tracing_subscriber::registry()
.with(LevelFilter::INFO.and_then(layer))
},
)
.run("tests/features/wait")
.await,
);
let mut buffer = String::new();
file.read_to_string(&mut buffer).unwrap();
// Required to strip out non-deterministic parts of output, so we could
// compare them well.
let non_deterministic = RegexBuilder::new(
"time(stamp)?=\"[^\"]+\"\
|: [^\\.\\s]*\\.(feature|rs)(:\\d+:\\d+)?\
|^\\s+\
|\\s?\\n",
)
.multi_line(true)
.build()
.unwrap();
assert_eq!(
non_deterministic.replace_all(&buffer, ""),
non_deterministic.replace_all(
&fs::read_to_string("tests/junit/correct.xml").unwrap(),
"",
),
);
}
#[derive(Clone, Copy, cucumber::World, Debug, Default)]
struct World(usize);