From 15c8baadf5899745ef0aed993af00ee8cbe000ef Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Mon, 10 Apr 2023 20:01:23 +0200 Subject: [PATCH] Place beets behind Arc Mutex in tests --- src/database/json.rs | 4 ++-- src/library/beets.rs | 4 ++-- tests/database/json.rs | 13 ++++++------ tests/library/beets.rs | 46 +++++++++++++++++++++++++----------------- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/database/json.rs b/src/database/json.rs index a940d9f..03e1830 100644 --- a/src/database/json.rs +++ b/src/database/json.rs @@ -19,11 +19,11 @@ pub trait DatabaseJsonBackend { /// A JSON file database. pub struct DatabaseJson { - backend: Box, + backend: Box, } impl DatabaseJson { - pub fn new(backend: Box) -> Self { + pub fn new(backend: Box) -> Self { DatabaseJson { backend } } } diff --git a/src/library/beets.rs b/src/library/beets.rs index 98d9a5d..f5b1531 100644 --- a/src/library/beets.rs +++ b/src/library/beets.rs @@ -91,7 +91,7 @@ pub trait BeetsExecutor { /// Struct for interacting with the music library via beets. pub struct Beets { - executor: Box, + executor: Box, } trait LibraryPrivate { @@ -106,7 +106,7 @@ trait LibraryPrivate { } impl Beets { - pub fn new(executor: Box) -> Beets { + pub fn new(executor: Box) -> Beets { Beets { executor } } } diff --git a/tests/database/json.rs b/tests/database/json.rs index 8b22aef..d747841 100644 --- a/tests/database/json.rs +++ b/tests/database/json.rs @@ -1,4 +1,4 @@ -use std::fs; +use std::{fs, path::PathBuf}; use musichoard::{ database::{ @@ -7,10 +7,14 @@ use musichoard::{ }, Artist, }; +use once_cell::sync::Lazy; use tempfile::NamedTempFile; use crate::COLLECTION; +static DATABASE_TEST_FILE: Lazy = + Lazy::new(|| fs::canonicalize("./tests/files/database/database.json").unwrap()); + #[test] fn write() { let file = NamedTempFile::new().unwrap(); @@ -21,8 +25,7 @@ fn write() { let write_data = COLLECTION.to_owned(); database.write(&write_data).unwrap(); - let expected_path = fs::canonicalize("./tests/files/database/database.json").unwrap(); - let expected = fs::read_to_string(expected_path).unwrap(); + let expected = fs::read_to_string(&*DATABASE_TEST_FILE).unwrap(); let actual = fs::read_to_string(file.path()).unwrap(); assert_eq!(actual, expected); @@ -30,9 +33,7 @@ fn write() { #[test] fn read() { - let file_path = fs::canonicalize("./tests/files/database/database.json").unwrap(); - - let backend = DatabaseJsonFile::new(&file_path); + let backend = DatabaseJsonFile::new(&*DATABASE_TEST_FILE); let database = DatabaseJson::new(Box::new(backend)); let mut read_data: Vec = vec![]; diff --git a/tests/library/beets.rs b/tests/library/beets.rs index f176fe7..be06b02 100644 --- a/tests/library/beets.rs +++ b/tests/library/beets.rs @@ -1,4 +1,9 @@ -use std::fs; +use std::{ + fs, + sync::{Arc, Mutex}, +}; + +use once_cell::sync::Lazy; use musichoard::{ library::{ @@ -10,11 +15,22 @@ use musichoard::{ use crate::COLLECTION; +static BEETS_EMPTY_CONFIG: Lazy>> = + Lazy::new(|| Arc::new(Mutex::new(Beets::new(Box::new(SystemExecutor::default()))))); + +static BEETS_TEST_CONFIG: Lazy>> = Lazy::new(|| { + Arc::new(Mutex::new(Beets::new(Box::new( + SystemExecutor::default().config(Some( + &fs::canonicalize("./tests/files/library/config.yml").unwrap(), + )), + )))) +}); + #[test] fn test_no_config_list() { - let executor = SystemExecutor::default(); + let beets_arc = BEETS_EMPTY_CONFIG.clone(); + let beets = &mut beets_arc.lock().unwrap(); - let mut beets = Beets::new(Box::new(executor)); let output = beets.list(&Query::default()).unwrap(); let expected: Vec = vec![]; @@ -23,11 +39,9 @@ fn test_no_config_list() { #[test] fn test_full_list() { - let executor = SystemExecutor::default().config(Some( - &fs::canonicalize("./tests/files/library/config.yml").unwrap(), - )); + let beets_arc = BEETS_TEST_CONFIG.clone(); + let beets = &mut beets_arc.lock().unwrap(); - let mut beets = Beets::new(Box::new(executor)); let output = beets.list(&Query::default()).unwrap(); let expected: Vec = COLLECTION.to_owned(); @@ -36,11 +50,9 @@ fn test_full_list() { #[test] fn test_album_artist_query() { - let executor = SystemExecutor::default().config(Some( - &fs::canonicalize("./tests/files/library/config.yml").unwrap(), - )); + let beets_arc = BEETS_TEST_CONFIG.clone(); + let beets = &mut beets_arc.lock().unwrap(); - let mut beets = Beets::new(Box::new(executor)); let output = beets .list(&Query::default().album_artist(QueryOption::Include(String::from("Аркона")))) .unwrap(); @@ -51,11 +63,9 @@ fn test_album_artist_query() { #[test] fn test_album_title_query() { - let executor = SystemExecutor::default().config(Some( - &fs::canonicalize("./tests/files/library/config.yml").unwrap(), - )); + let beets_arc = BEETS_TEST_CONFIG.clone(); + let beets = &mut beets_arc.lock().unwrap(); - let mut beets = Beets::new(Box::new(executor)); let output = beets .list(&Query::default().album_title(QueryOption::Include(String::from("Slovo")))) .unwrap(); @@ -66,11 +76,9 @@ fn test_album_title_query() { #[test] fn test_exclude_query() { - let executor = SystemExecutor::default().config(Some( - &fs::canonicalize("./tests/files/library/config.yml").unwrap(), - )); + let beets_arc = BEETS_TEST_CONFIG.clone(); + let beets = &mut beets_arc.lock().unwrap(); - let mut beets = Beets::new(Box::new(executor)); let output = beets .list(&Query::default().album_artist(QueryOption::Exclude(String::from("Аркона")))) .unwrap();