Skip to content

Commit 53d8c69

Browse files
authored
Merge pull request #751 from lann/tracing-testing
Use tracing TestWriter in tests
2 parents 8ef9d28 + 3f40946 commit 53d8c69

File tree

6 files changed

+17
-58
lines changed

6 files changed

+17
-58
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/http/src/lib.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ pub(crate) trait HttpExecutor: Clone + Send + Sync + 'static {
405405

406406
#[cfg(test)]
407407
mod tests {
408-
use std::{collections::BTreeMap, sync::Once};
408+
use std::collections::BTreeMap;
409409

410410
use anyhow::Result;
411411
use spin_manifest::{HttpConfig, HttpExecutor};
@@ -414,18 +414,6 @@ mod tests {
414414

415415
use super::*;
416416

417-
static LOGGER: Once = Once::new();
418-
419-
/// We can only initialize the tracing subscriber once per crate.
420-
pub(crate) fn init() {
421-
LOGGER.call_once(|| {
422-
tracing_subscriber::fmt()
423-
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
424-
.with_ansi(atty::is(atty::Stream::Stderr))
425-
.init();
426-
});
427-
}
428-
429417
#[test]
430418
fn test_default_headers_with_base_path() -> Result<()> {
431419
let scheme = "https";
@@ -525,8 +513,6 @@ mod tests {
525513
Ok(())
526514
}
527515

528-
// fn _search(key: &str, headers: &[(String, String)]) -> Option<String> {}
529-
530516
fn search<'a>(keys: &'a [&'a str], headers: &[(&[&str], String)]) -> Option<String> {
531517
let mut res: Option<String> = None;
532518
for (k, v) in headers {
@@ -540,8 +526,6 @@ mod tests {
540526

541527
#[tokio::test]
542528
async fn test_spin_http() -> Result<()> {
543-
init();
544-
545529
let mut cfg = spin_testing::TestConfig::default();
546530
cfg.test_program("rust-http-test.wasm")
547531
.http_trigger(HttpConfig {
@@ -571,8 +555,6 @@ mod tests {
571555

572556
#[tokio::test]
573557
async fn test_wagi_http() -> Result<()> {
574-
init();
575-
576558
let mut cfg = spin_testing::TestConfig::default();
577559
cfg.test_program("wagi-test.wasm").http_trigger(HttpConfig {
578560
route: "/test".to_string(),

crates/http/src/routes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,13 @@ impl fmt::Display for RoutePattern {
144144

145145
#[cfg(test)]
146146
mod route_tests {
147+
use spin_testing::init_tracing;
148+
147149
use super::*;
148-
use crate::tests::init;
149150

150151
#[test]
151152
fn test_exact_route() {
152-
init();
153+
init_tracing();
153154

154155
let rp = RoutePattern::from("/", "/foo/bar");
155156
assert!(rp.matches("/foo/bar"));

crates/redis/src/tests.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ use redis::{Msg, Value};
44
use spin_manifest::{RedisConfig, RedisExecutor};
55
use spin_testing::TestConfig;
66
use spin_trigger::TriggerExecutorBuilder;
7-
use std::sync::Once;
8-
9-
static LOGGER: Once = Once::new();
10-
11-
/// We can only initialize the tracing subscriber once per crate.
12-
pub(crate) fn init() {
13-
LOGGER.call_once(|| {
14-
tracing_subscriber::fmt()
15-
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
16-
.init();
17-
});
18-
}
197

208
fn create_trigger_event(channel: &str, payload: &str) -> redis::Msg {
219
Msg::from_value(&redis::Value::Bulk(vec![
@@ -29,8 +17,6 @@ fn create_trigger_event(channel: &str, payload: &str) -> redis::Msg {
2917
#[ignore]
3018
#[tokio::test]
3119
async fn test_pubsub() -> Result<()> {
32-
init();
33-
3420
let mut cfg = TestConfig::default();
3521
cfg.test_program("redis-rust.wasm")
3622
.redis_trigger(RedisConfig {

crates/testing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ spin-engine = { path = "../engine" }
1212
spin-manifest = { path = "../manifest" }
1313
spin-http-engine = { path = "../http" }
1414
spin-trigger = { path = "../trigger" }
15+
tracing-subscriber = "0.3"

crates/testing/src/lib.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44
use std::{
55
net::SocketAddr,
66
path::{Path, PathBuf},
7+
sync::Once,
78
};
89

9-
use http::{uri::Scheme, Request, Response};
10+
use http::Response;
1011
use hyper::Body;
11-
use spin_engine::{Builder, ExecutionContextConfiguration};
1212
use spin_http_engine::HttpTrigger;
1313
use spin_manifest::{
1414
Application, ApplicationInformation, ApplicationOrigin, ApplicationTrigger, CoreComponent,
1515
HttpConfig, ModuleSource, RedisConfig, RedisTriggerConfiguration, SpinVersion, TriggerConfig,
1616
};
1717
use spin_trigger::TriggerExecutorBuilder;
1818

19+
/// Initialize a test writer for `tracing`, making its output compatible with libtest
20+
pub fn init_tracing() {
21+
static ONCE: Once = Once::new();
22+
ONCE.call_once(|| {
23+
tracing_subscriber::fmt().with_test_writer().init();
24+
})
25+
}
26+
1927
#[derive(Default)]
2028
pub struct TestConfig {
2129
module_path: Option<PathBuf>,
@@ -25,6 +33,7 @@ pub struct TestConfig {
2533

2634
impl TestConfig {
2735
pub fn module_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
36+
init_tracing();
2837
self.module_path = Some(path.into());
2938
self
3039
}
@@ -99,33 +108,12 @@ impl TestConfig {
99108
}
100109
}
101110

102-
pub async fn prepare_builder<T: Default + Send + 'static>(
103-
&self,
104-
app: Application,
105-
) -> Builder<T> {
106-
let config = ExecutionContextConfiguration {
107-
components: app.components,
108-
label: app.info.name,
109-
..Default::default()
110-
};
111-
let mut builder = Builder::new(config).expect("Builder::new failed");
112-
builder.link_defaults().expect("link_defaults failed");
113-
builder
114-
}
115-
116111
pub async fn build_http_trigger(&self) -> HttpTrigger {
117112
TriggerExecutorBuilder::new(self.build_application())
118113
.build()
119114
.await
120115
.unwrap()
121116
}
122-
123-
pub async fn handle_http_request(&self, req: Request<Body>) -> anyhow::Result<Response<Body>> {
124-
self.build_http_trigger()
125-
.await
126-
.handle(req, Scheme::HTTP, test_socket_addr())
127-
.await
128-
}
129117
}
130118

131119
pub fn test_socket_addr() -> SocketAddr {

0 commit comments

Comments
 (0)