Skip to content

Commit 192de7d

Browse files
authored
test: avoid using unstable API from insta (#2357)
1 parent ed9db08 commit 192de7d

File tree

285 files changed

+804
-818
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

285 files changed

+804
-818
lines changed

dc/s2n-quic-dc/src/event/generated.rs

+19-48
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ pub mod metrics {
436436
#[cfg(any(test, feature = "testing"))]
437437
pub mod testing {
438438
use super::*;
439+
use crate::event::snapshot::Location;
439440
use core::sync::atomic::{AtomicU32, Ordering};
440441
use std::sync::Mutex;
441442
#[derive(Clone, Debug)]
@@ -450,7 +451,7 @@ pub mod testing {
450451
return;
451452
}
452453
if let Some(location) = self.location.as_ref() {
453-
location.snapshot(&self.output.lock().unwrap());
454+
location.snapshot_log(&self.output.lock().unwrap());
454455
}
455456
}
456457
}
@@ -459,7 +460,14 @@ pub mod testing {
459460
#[track_caller]
460461
pub fn snapshot() -> Self {
461462
let mut sub = Self::no_snapshot();
462-
sub.location = Location::try_new();
463+
sub.location = Location::from_thread_name();
464+
sub
465+
}
466+
#[doc = r" Creates a subscriber with snapshot assertions enabled"]
467+
#[track_caller]
468+
pub fn named_snapshot<Name: core::fmt::Display>(name: Name) -> Self {
469+
let mut sub = Self::no_snapshot();
470+
sub.location = Some(Location::new(name));
463471
sub
464472
}
465473
#[doc = r" Creates a subscriber with snapshot assertions disabled"]
@@ -505,7 +513,14 @@ pub mod testing {
505513
#[track_caller]
506514
pub fn snapshot() -> Self {
507515
let mut sub = Self::no_snapshot();
508-
sub.location = Location::try_new();
516+
sub.location = Location::from_thread_name();
517+
sub
518+
}
519+
#[doc = r" Creates a subscriber with snapshot assertions enabled"]
520+
#[track_caller]
521+
pub fn named_snapshot<Name: core::fmt::Display>(name: Name) -> Self {
522+
let mut sub = Self::no_snapshot();
523+
sub.location = Some(Location::new(name));
509524
sub
510525
}
511526
#[doc = r" Creates a publisher with snapshot assertions disabled"]
@@ -543,52 +558,8 @@ pub mod testing {
543558
return;
544559
}
545560
if let Some(location) = self.location.as_ref() {
546-
location.snapshot(&self.output.lock().unwrap());
561+
location.snapshot_log(&self.output.lock().unwrap());
547562
}
548563
}
549564
}
550-
#[derive(Clone, Debug)]
551-
struct Location(&'static core::panic::Location<'static>);
552-
impl Location {
553-
#[track_caller]
554-
fn try_new() -> Option<Self> {
555-
let thread = std::thread::current();
556-
if thread.name().map_or(false, |name| name != "main") {
557-
Some(Self(core::panic::Location::caller()))
558-
} else {
559-
None
560-
}
561-
}
562-
fn snapshot(&self, output: &[String]) {
563-
if cfg!(miri) {
564-
return;
565-
}
566-
use std::path::{Component, Path};
567-
let value = output.join("\n");
568-
let thread = std::thread::current();
569-
let function_name = thread.name().unwrap();
570-
let test_path = Path::new(self.0.file().trim_end_matches(".rs"));
571-
let module_path = test_path
572-
.components()
573-
.filter_map(|comp| match comp {
574-
Component::Normal(comp) => comp.to_str(),
575-
_ => Some("_"),
576-
})
577-
.chain(Some("events"))
578-
.collect::<Vec<_>>()
579-
.join("::");
580-
let current_dir = std::env::current_dir().unwrap();
581-
insta::_macro_support::assert_snapshot(
582-
insta::_macro_support::AutoName.into(),
583-
&value,
584-
current_dir.to_str().unwrap(),
585-
function_name,
586-
&module_path,
587-
self.0.file(),
588-
self.0.line(),
589-
"",
590-
)
591-
.unwrap()
592-
}
593-
}
594565
}

quic/s2n-quic-core/src/event.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ mod generated;
1010
pub mod metrics;
1111
pub use generated::*;
1212

13+
#[cfg(any(test, feature = "testing"))]
14+
#[doc(hidden)]
15+
pub mod snapshot;
16+
1317
/// All event types which can be emitted from this library.
1418
pub trait Event: core::fmt::Debug {
1519
const NAME: &'static str;

quic/s2n-quic-core/src/event/generated.rs

+19-48
Original file line numberDiff line numberDiff line change
@@ -7534,6 +7534,7 @@ pub mod metrics {
75347534
#[cfg(any(test, feature = "testing"))]
75357535
pub mod testing {
75367536
use super::*;
7537+
use crate::event::snapshot::Location;
75377538
#[derive(Clone, Debug)]
75387539
pub struct Subscriber {
75397540
location: Option<Location>,
@@ -7602,7 +7603,7 @@ pub mod testing {
76027603
return;
76037604
}
76047605
if let Some(location) = self.location.as_ref() {
7605-
location.snapshot(&self.output);
7606+
location.snapshot_log(&self.output);
76067607
}
76077608
}
76087609
}
@@ -7611,7 +7612,14 @@ pub mod testing {
76117612
#[track_caller]
76127613
pub fn snapshot() -> Self {
76137614
let mut sub = Self::no_snapshot();
7614-
sub.location = Location::try_new();
7615+
sub.location = Location::from_thread_name();
7616+
sub
7617+
}
7618+
#[doc = r" Creates a subscriber with snapshot assertions enabled"]
7619+
#[track_caller]
7620+
pub fn named_snapshot<Name: core::fmt::Display>(name: Name) -> Self {
7621+
let mut sub = Self::no_snapshot();
7622+
sub.location = Some(Location::new(name));
76157623
sub
76167624
}
76177625
#[doc = r" Creates a subscriber with snapshot assertions disabled"]
@@ -8325,7 +8333,14 @@ pub mod testing {
83258333
#[track_caller]
83268334
pub fn snapshot() -> Self {
83278335
let mut sub = Self::no_snapshot();
8328-
sub.location = Location::try_new();
8336+
sub.location = Location::from_thread_name();
8337+
sub
8338+
}
8339+
#[doc = r" Creates a subscriber with snapshot assertions enabled"]
8340+
#[track_caller]
8341+
pub fn named_snapshot<Name: core::fmt::Display>(name: Name) -> Self {
8342+
let mut sub = Self::no_snapshot();
8343+
sub.location = Some(Location::new(name));
83298344
sub
83308345
}
83318346
#[doc = r" Creates a publisher with snapshot assertions disabled"]
@@ -8793,52 +8808,8 @@ pub mod testing {
87938808
return;
87948809
}
87958810
if let Some(location) = self.location.as_ref() {
8796-
location.snapshot(&self.output);
8811+
location.snapshot_log(&self.output);
87978812
}
87988813
}
87998814
}
8800-
#[derive(Clone, Debug)]
8801-
struct Location(&'static core::panic::Location<'static>);
8802-
impl Location {
8803-
#[track_caller]
8804-
fn try_new() -> Option<Self> {
8805-
let thread = std::thread::current();
8806-
if thread.name().map_or(false, |name| name != "main") {
8807-
Some(Self(core::panic::Location::caller()))
8808-
} else {
8809-
None
8810-
}
8811-
}
8812-
fn snapshot(&self, output: &[String]) {
8813-
if cfg!(miri) {
8814-
return;
8815-
}
8816-
use std::path::{Component, Path};
8817-
let value = output.join("\n");
8818-
let thread = std::thread::current();
8819-
let function_name = thread.name().unwrap();
8820-
let test_path = Path::new(self.0.file().trim_end_matches(".rs"));
8821-
let module_path = test_path
8822-
.components()
8823-
.filter_map(|comp| match comp {
8824-
Component::Normal(comp) => comp.to_str(),
8825-
_ => Some("_"),
8826-
})
8827-
.chain(Some("events"))
8828-
.collect::<Vec<_>>()
8829-
.join("::");
8830-
let current_dir = std::env::current_dir().unwrap();
8831-
insta::_macro_support::assert_snapshot(
8832-
insta::_macro_support::AutoName.into(),
8833-
&value,
8834-
current_dir.to_str().unwrap(),
8835-
function_name,
8836-
&module_path,
8837-
self.0.file(),
8838-
self.0.line(),
8839-
"",
8840-
)
8841-
.unwrap()
8842-
}
8843-
}
88448815
}
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use core::panic;
5+
use std::path::{Path, PathBuf};
6+
7+
#[derive(Clone, Debug)]
8+
pub struct Location {
9+
location: &'static panic::Location<'static>,
10+
name: String,
11+
}
12+
13+
impl Location {
14+
#[track_caller]
15+
pub fn new<N: core::fmt::Display>(name: N) -> Self {
16+
let location = panic::Location::caller();
17+
let name = name.to_string();
18+
Self { location, name }
19+
}
20+
21+
#[track_caller]
22+
#[allow(clippy::manual_map)] // using `Option::map` messes up the track_caller
23+
pub fn from_thread_name() -> Option<Self> {
24+
let thread = std::thread::current();
25+
26+
// only create a location if insta can figure out the test name from the
27+
// thread
28+
if let Some(name) = thread.name().filter(|name| *name != "main") {
29+
let name = name
30+
.split("::")
31+
.chain(Some("events"))
32+
.collect::<Vec<_>>()
33+
.join("__");
34+
Some(Self::new(name))
35+
} else {
36+
None
37+
}
38+
}
39+
40+
pub fn snapshot_log(&self, output: &[String]) {
41+
// miri doesn't support the syscalls that insta uses
42+
if cfg!(miri) {
43+
return;
44+
}
45+
46+
let value = output.join("\n");
47+
48+
let name = self.name.as_str();
49+
50+
let mut settings = insta::Settings::clone_current();
51+
52+
// we want to use the actual caller's module
53+
settings.set_prepend_module_to_snapshot(false);
54+
settings.set_input_file(self.location.file());
55+
settings.set_snapshot_path(self.snapshot_path());
56+
settings.set_omit_expression(true);
57+
58+
settings.bind(|| {
59+
insta::assert_snapshot!(name, &value);
60+
});
61+
}
62+
63+
fn snapshot_path(&self) -> PathBuf {
64+
let ws = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."));
65+
let file = Path::new(self.location.file());
66+
67+
let file = if file.is_relative() {
68+
ws.join(file)
69+
} else {
70+
file.to_path_buf()
71+
};
72+
73+
file.canonicalize()
74+
.unwrap()
75+
.parent()
76+
.unwrap()
77+
.join("snapshots")
78+
}
79+
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Failed }
66
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Failed }
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Failed }
66
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Failed }
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Failed }
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Unknown }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Failed }
66
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Testing }
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Testing }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Failed }
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Capable }
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Capable }
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Capable }
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: quic/s2n-quic-core/src/path/ecn/tests.rs
3-
expression: ""
2+
source: quic/s2n-quic-core/src/event/snapshot.rs
3+
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
44
---
55
EcnStateChanged { path: Path { local_addr: 127.0.0.1:0, local_cid: 0x4c6f63616c4900000000000000004c6f63616c49, remote_addr: 127.0.0.1:0, remote_cid: 0x5065657249640000000000000000506565724964, id: 0, is_active: false }, state: Capable }

0 commit comments

Comments
 (0)