sound: add init_logger() helper for testing

The init_logger() function gets compiled in tests only, and serves to
initialize logging only once per test thread.

Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
This commit is contained in:
Manos Pitsidianakis 2023-12-13 13:13:24 +02:00 committed by Manos Pitsidianakis
parent 6d81397eed
commit c0c75d36ff
5 changed files with 26 additions and 0 deletions

View File

@ -34,6 +34,7 @@ mod tests {
#[test]
fn test_null_backend_write() {
crate::init_logger();
let streams = Arc::new(RwLock::new(vec![Stream::default()]));
let null_backend = NullBackend::new(streams.clone());
@ -45,6 +46,7 @@ mod tests {
#[test]
fn test_null_backend_read() {
crate::init_logger();
let streams = Arc::new(RwLock::new(vec![Stream::default()]));
let null_backend = NullBackend::new(streams.clone());

View File

@ -575,6 +575,7 @@ mod tests {
#[test]
fn test_pipewire_backend_success() {
crate::init_logger();
let streams = Arc::new(RwLock::new(vec![Stream::default()]));
let stream_params = streams.clone();
@ -603,6 +604,7 @@ mod tests {
#[test]
fn test_pipewire_backend_invalid_stream() {
crate::init_logger();
let stream_params = Arc::new(RwLock::new(vec![]));
let _test_harness = PipewireTestHarness::new();

View File

@ -827,6 +827,7 @@ mod tests {
#[test]
fn test_sound_thread_success() {
crate::init_logger();
let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null);
let chmaps = Arc::new(RwLock::new(vec![]));
@ -921,6 +922,7 @@ mod tests {
#[test]
fn test_sound_thread_failure() {
crate::init_logger();
let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null);
let chmaps = Arc::new(RwLock::new(vec![]));
@ -995,6 +997,7 @@ mod tests {
#[test]
fn test_sound_backend() {
crate::init_logger();
let test_dir = tempdir().expect("Could not create a temp test directory.");
let socket_path = test_dir.path().join(SOCKET_PATH).display().to_string();
let config = SoundConfig::new(socket_path, false, BackendType::Null);
@ -1069,6 +1072,7 @@ mod tests {
#[test]
fn test_sound_backend_failures() {
crate::init_logger();
let test_dir = tempdir().expect("Could not create a temp test directory.");
let socket_path = test_dir

View File

@ -30,6 +30,12 @@
clippy::significant_drop_tightening
)]
#[cfg(test)]
pub fn init_logger() {
std::env::set_var("RUST_LOG", "trace");
let _ = env_logger::builder().is_test(true).try_init();
}
pub mod audio_backends;
pub mod device;
pub mod stream;
@ -319,6 +325,7 @@ mod tests {
#[test]
fn test_sound_server() {
const SOCKET_PATH: &str = "vsound.socket";
crate::init_logger();
let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null);
@ -341,6 +348,7 @@ mod tests {
#[test]
fn test_control_message_kind_try_from() {
crate::init_logger();
assert_eq!(
ControlMessageKind::try_from(<u32 as Into<Le32>>::into(VIRTIO_SND_R_JACK_INFO)),
Ok(ControlMessageKind::JackInfo)
@ -361,6 +369,7 @@ mod tests {
#[test]
fn test_control_message_kind_try_from_invalid() {
crate::init_logger();
// Test an invalid value that should result in an InvalidControlMessage error
let invalid_value: u32 = 0x1101;
assert_eq!(
@ -371,6 +380,7 @@ mod tests {
#[test]
fn test_try_from_valid_output() {
crate::init_logger();
let val = virtio_sound::VIRTIO_SND_D_OUTPUT;
assert_eq!(Direction::try_from(val).unwrap(), Direction::Output);
@ -383,6 +393,7 @@ mod tests {
#[test]
fn test_display() {
crate::init_logger();
let error = InvalidControlMessage(42);
let formatted_error = format!("{}", error);
assert_eq!(formatted_error, "Invalid control message code 42");
@ -390,6 +401,7 @@ mod tests {
#[test]
fn test_into_error() {
crate::init_logger();
let error = InvalidControlMessage(42);
let _error: Error = error.into();

View File

@ -53,8 +53,14 @@ mod tests {
}
}
fn init_logger() {
std::env::set_var("RUST_LOG", "trace");
let _ = env_logger::builder().is_test(true).try_init();
}
#[test]
fn test_sound_config_setup() {
init_logger();
let args = SoundArgs::from_args("/tmp/vhost-sound.socket");
let config = SoundConfig::try_from(args);