Skip to content
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

test: avoid using unstable API from insta #2357

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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
67 changes: 19 additions & 48 deletions dc/s2n-quic-dc/src/event/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ pub mod metrics {
#[cfg(any(test, feature = "testing"))]
pub mod testing {
use super::*;
use crate::event::snapshot::Location;
use core::sync::atomic::{AtomicU32, Ordering};
use std::sync::Mutex;
#[derive(Clone, Debug)]
Expand All @@ -450,7 +451,7 @@ pub mod testing {
return;
}
if let Some(location) = self.location.as_ref() {
location.snapshot(&self.output.lock().unwrap());
location.snapshot_log(&self.output.lock().unwrap());
}
}
}
Expand All @@ -459,7 +460,14 @@ pub mod testing {
#[track_caller]
pub fn snapshot() -> Self {
let mut sub = Self::no_snapshot();
sub.location = Location::try_new();
sub.location = Location::from_thread_name();
sub
}
#[doc = r" Creates a subscriber with snapshot assertions enabled"]
#[track_caller]
pub fn named_snapshot<Name: core::fmt::Display>(name: Name) -> Self {
let mut sub = Self::no_snapshot();
sub.location = Some(Location::new(name));
sub
}
#[doc = r" Creates a subscriber with snapshot assertions disabled"]
Expand Down Expand Up @@ -505,7 +513,14 @@ pub mod testing {
#[track_caller]
pub fn snapshot() -> Self {
let mut sub = Self::no_snapshot();
sub.location = Location::try_new();
sub.location = Location::from_thread_name();
sub
}
#[doc = r" Creates a subscriber with snapshot assertions enabled"]
#[track_caller]
pub fn named_snapshot<Name: core::fmt::Display>(name: Name) -> Self {
let mut sub = Self::no_snapshot();
sub.location = Some(Location::new(name));
sub
}
#[doc = r" Creates a publisher with snapshot assertions disabled"]
Expand Down Expand Up @@ -543,52 +558,8 @@ pub mod testing {
return;
}
if let Some(location) = self.location.as_ref() {
location.snapshot(&self.output.lock().unwrap());
location.snapshot_log(&self.output.lock().unwrap());
}
}
}
#[derive(Clone, Debug)]
struct Location(&'static core::panic::Location<'static>);
impl Location {
#[track_caller]
fn try_new() -> Option<Self> {
let thread = std::thread::current();
if thread.name().map_or(false, |name| name != "main") {
Some(Self(core::panic::Location::caller()))
} else {
None
}
}
fn snapshot(&self, output: &[String]) {
if cfg!(miri) {
return;
}
use std::path::{Component, Path};
let value = output.join("\n");
let thread = std::thread::current();
let function_name = thread.name().unwrap();
let test_path = Path::new(self.0.file().trim_end_matches(".rs"));
let module_path = test_path
.components()
.filter_map(|comp| match comp {
Component::Normal(comp) => comp.to_str(),
_ => Some("_"),
})
.chain(Some("events"))
.collect::<Vec<_>>()
.join("::");
let current_dir = std::env::current_dir().unwrap();
insta::_macro_support::assert_snapshot(
insta::_macro_support::AutoName.into(),
&value,
current_dir.to_str().unwrap(),
function_name,
&module_path,
self.0.file(),
self.0.line(),
"",
)
.unwrap()
}
}
}
4 changes: 4 additions & 0 deletions quic/s2n-quic-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ mod generated;
pub mod metrics;
pub use generated::*;

#[cfg(any(test, feature = "testing"))]
#[doc(hidden)]
pub mod snapshot;

/// All event types which can be emitted from this library.
pub trait Event: core::fmt::Debug {
const NAME: &'static str;
Expand Down
67 changes: 19 additions & 48 deletions quic/s2n-quic-core/src/event/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7534,6 +7534,7 @@ pub mod metrics {
#[cfg(any(test, feature = "testing"))]
pub mod testing {
use super::*;
use crate::event::snapshot::Location;
#[derive(Clone, Debug)]
pub struct Subscriber {
location: Option<Location>,
Expand Down Expand Up @@ -7602,7 +7603,7 @@ pub mod testing {
return;
}
if let Some(location) = self.location.as_ref() {
location.snapshot(&self.output);
location.snapshot_log(&self.output);
}
}
}
Expand All @@ -7611,7 +7612,14 @@ pub mod testing {
#[track_caller]
pub fn snapshot() -> Self {
let mut sub = Self::no_snapshot();
sub.location = Location::try_new();
sub.location = Location::from_thread_name();
sub
}
#[doc = r" Creates a subscriber with snapshot assertions enabled"]
#[track_caller]
pub fn named_snapshot<Name: core::fmt::Display>(name: Name) -> Self {
let mut sub = Self::no_snapshot();
sub.location = Some(Location::new(name));
sub
}
#[doc = r" Creates a subscriber with snapshot assertions disabled"]
Expand Down Expand Up @@ -8325,7 +8333,14 @@ pub mod testing {
#[track_caller]
pub fn snapshot() -> Self {
let mut sub = Self::no_snapshot();
sub.location = Location::try_new();
sub.location = Location::from_thread_name();
sub
}
#[doc = r" Creates a subscriber with snapshot assertions enabled"]
#[track_caller]
pub fn named_snapshot<Name: core::fmt::Display>(name: Name) -> Self {
let mut sub = Self::no_snapshot();
sub.location = Some(Location::new(name));
sub
}
#[doc = r" Creates a publisher with snapshot assertions disabled"]
Expand Down Expand Up @@ -8793,52 +8808,8 @@ pub mod testing {
return;
}
if let Some(location) = self.location.as_ref() {
location.snapshot(&self.output);
location.snapshot_log(&self.output);
}
}
}
#[derive(Clone, Debug)]
struct Location(&'static core::panic::Location<'static>);
impl Location {
#[track_caller]
fn try_new() -> Option<Self> {
let thread = std::thread::current();
if thread.name().map_or(false, |name| name != "main") {
Some(Self(core::panic::Location::caller()))
} else {
None
}
}
fn snapshot(&self, output: &[String]) {
if cfg!(miri) {
return;
}
use std::path::{Component, Path};
let value = output.join("\n");
let thread = std::thread::current();
let function_name = thread.name().unwrap();
let test_path = Path::new(self.0.file().trim_end_matches(".rs"));
let module_path = test_path
.components()
.filter_map(|comp| match comp {
Component::Normal(comp) => comp.to_str(),
_ => Some("_"),
})
.chain(Some("events"))
.collect::<Vec<_>>()
.join("::");
let current_dir = std::env::current_dir().unwrap();
insta::_macro_support::assert_snapshot(
insta::_macro_support::AutoName.into(),
&value,
current_dir.to_str().unwrap(),
function_name,
&module_path,
self.0.file(),
self.0.line(),
"",
)
.unwrap()
}
}
}
79 changes: 79 additions & 0 deletions quic/s2n-quic-core/src/event/snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use core::panic;
use std::path::{Path, PathBuf};

#[derive(Clone, Debug)]
pub struct Location {
location: &'static panic::Location<'static>,
name: String,
}

impl Location {
#[track_caller]
pub fn new<N: core::fmt::Display>(name: N) -> Self {
let location = panic::Location::caller();
let name = name.to_string();
Self { location, name }
}

#[track_caller]
#[allow(clippy::manual_map)] // using `Option::map` messes up the track_caller
pub fn from_thread_name() -> Option<Self> {
let thread = std::thread::current();

// only create a location if insta can figure out the test name from the
// thread
if let Some(name) = thread.name().filter(|name| *name != "main") {
let name = name
.split("::")
.chain(Some("events"))
.collect::<Vec<_>>()
.join("__");
Some(Self::new(name))
} else {
None
}
}

pub fn snapshot_log(&self, output: &[String]) {
// miri doesn't support the syscalls that insta uses
if cfg!(miri) {
return;
}

let value = output.join("\n");

let name = self.name.as_str();

let mut settings = insta::Settings::clone_current();

// we want to use the actual caller's module
settings.set_prepend_module_to_snapshot(false);
settings.set_input_file(self.location.file());
settings.set_snapshot_path(self.snapshot_path());
settings.set_omit_expression(true);

settings.bind(|| {
insta::assert_snapshot!(name, &value);
});
}

fn snapshot_path(&self) -> PathBuf {
let ws = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."));
let file = Path::new(self.location.file());

let file = if file.is_relative() {
ws.join(file)
} else {
file.to_path_buf()
};

file.canonicalize()
.unwrap()
.parent()
.unwrap()
.join("snapshots")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 }
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 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 }
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 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 }
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 number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: quic/s2n-quic-core/src/path/ecn/tests.rs
expression: ""
source: quic/s2n-quic-core/src/event/snapshot.rs
input_file: quic/s2n-quic-core/src/path/ecn/tests.rs
---
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 }
Loading
Loading