Skip to content

Commit 71dc867

Browse files
committed
Add support for sanitizers, matchers
Resolves Azure#1877. This also makes it possible to compile `azure_core_test` for the wasm32 architecture but will not currently work for wasm32. I opened Azure#2009 to track.
1 parent 07f93ac commit 71dc867

File tree

8 files changed

+513
-455
lines changed

8 files changed

+513
-455
lines changed

Diff for: sdk/core/azure_core_test/src/lib.rs

+19-58
Original file line numberDiff line numberDiff line change
@@ -3,95 +3,60 @@
33

44
#![doc = include_str!("../README.md")]
55

6-
#[cfg(not(target_arch = "wasm32"))]
76
pub mod proxy;
8-
#[cfg(not(target_arch = "wasm32"))]
97
pub mod recorded;
10-
#[cfg(not(target_arch = "wasm32"))]
118
mod recording;
12-
mod transport;
139

1410
pub use azure_core::test::TestMode;
15-
use azure_core::{ClientOptions, TransportOptions};
16-
#[cfg(not(target_arch = "wasm32"))]
11+
pub use proxy::{matchers::*, sanitizers::*};
1712
pub use recording::*;
18-
use std::{
19-
path::{Path, PathBuf},
20-
sync::Arc,
21-
};
13+
use std::path::{Path, PathBuf};
2214

2315
const SPAN_TARGET: &str = "test-proxy";
2416

2517
/// Context information required by recorded client library tests.
2618
///
2719
/// This context is required for any recorded tests not attributed as `#[recorded::test(live)]`
2820
/// to setup up the HTTP client to record or play back session records.
29-
#[derive(Clone, Debug)]
21+
#[derive(Debug)]
3022
pub struct TestContext {
31-
test_mode: TestMode,
3223
crate_dir: &'static Path,
3324
test_name: &'static str,
25+
recording: Option<Recording>,
3426
}
3527

3628
impl TestContext {
3729
/// Not intended for use outside the `azure_core` crates.
3830
#[doc(hidden)]
39-
pub fn new(test_mode: TestMode, crate_dir: &'static str, test_name: &'static str) -> Self {
31+
pub fn new(crate_dir: &'static str, test_name: &'static str) -> Self {
4032
Self {
41-
test_mode,
4233
crate_dir: Path::new(crate_dir),
4334
test_name,
35+
recording: None,
4436
}
4537
}
4638

47-
/// Instruments the [`ClientOptions`] to support recording and playing back of session records.
48-
///
49-
/// # Examples
50-
///
51-
/// ```no_run
52-
/// use azure_core_test::{recorded, TestContext};
53-
///
54-
/// # struct MyClient;
55-
/// # #[derive(Default)]
56-
/// # struct MyClientOptions { client_options: azure_core::ClientOptions };
57-
/// # impl MyClient {
58-
/// # fn new(endpoint: impl AsRef<str>, options: Option<MyClientOptions>) -> Self { todo!() }
59-
/// # async fn invoke(&self) -> azure_core::Result<()> { todo!() }
60-
/// # }
61-
/// #[recorded::test]
62-
/// async fn test_invoke(ctx: TestContext) -> azure_core::Result<()> {
63-
/// let mut options = MyClientOptions::default();
64-
/// ctx.instrument(&mut options.client_options);
65-
///
66-
/// let client = MyClient::new("https://azure.net", Some(options));
67-
/// client.invoke().await
68-
/// }
69-
/// ```
70-
pub fn instrument(&self, options: &mut ClientOptions) {
71-
let transport = options.transport.clone().unwrap_or_default();
72-
options.transport = Some(TransportOptions::new_custom_policy(Arc::new(
73-
transport::ProxyTransportPolicy {
74-
inner: transport,
75-
mode: self.test_mode,
76-
},
77-
)));
78-
}
79-
8039
/// Gets the root directory of the crate under test.
8140
pub fn crate_dir(&self) -> &'static Path {
8241
self.crate_dir
8342
}
8443

44+
/// Gets the [`Recording`].
45+
///
46+
/// # Panics
47+
///
48+
/// Panics if a recording or playback has not been started.
49+
pub fn recording(&self) -> &Recording {
50+
self.recording
51+
.as_ref()
52+
.expect("not recording or playback started")
53+
}
54+
8555
/// Gets the test data directory under [`Self::crate_dir`].
8656
pub fn test_data_dir(&self) -> PathBuf {
8757
self.crate_dir.join("tests/data")
8858
}
8959

90-
/// Gets the current [`TestMode`].
91-
pub fn test_mode(&self) -> TestMode {
92-
self.test_mode
93-
}
94-
9560
/// Gets the current test function name.
9661
pub fn test_name(&self) -> &'static str {
9762
self.test_name
@@ -118,12 +83,8 @@ mod tests {
11883

11984
#[test]
12085
fn test_content_new() {
121-
let ctx = TestContext::new(
122-
TestMode::default(),
123-
env!("CARGO_MANIFEST_DIR"),
124-
"test_content_new",
125-
);
126-
assert_eq!(ctx.test_mode(), TestMode::Playback);
86+
let ctx = TestContext::new(env!("CARGO_MANIFEST_DIR"), "test_content_new");
87+
assert!(ctx.recording.is_none());
12788
assert!(ctx
12889
.crate_dir()
12990
.to_str()

0 commit comments

Comments
 (0)