Skip to content

Commit

Permalink
Fixed tests concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed May 28, 2019
1 parent 1811a7a commit 32cc628
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ where
{
let d = base_data_dir();
if !d.is_dir() {
std::fs::create_dir(d).or_else(|e| {
std::fs::create_dir(&d).or_else(|e| {
arg_error!(
"data-dir",
"Audioserve data directory {:?} cannot be created due to error {}",
Expand Down Expand Up @@ -456,7 +456,7 @@ where
#[cfg(test)]
mod test {
use super::*;

use crate::config::init::init_default_config;
#[test]
fn test_basic_args() {
init_default_config();
Expand Down
39 changes: 26 additions & 13 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,26 +413,39 @@ pub fn init_config() -> Result<()> {
Ok(())
}

//this default config is used only for testing
#[allow(dead_code)]
pub fn init_default_config() {
unsafe {
if CONFIG.is_some() {
return;
}

BASE_DATA_DIR = Some(dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")));
#[cfg(test)]
pub mod init {
/// Static config initialization for tests
/// as tests are run concurrently it requires also some synchronication
use super::{BASE_DATA_DIR, CONFIG, Config};
use std::path::PathBuf;
use std::sync::Mutex;
lazy_static!{
static ref GUARD:Mutex<bool> = Mutex::new(false);
}
let config = Config::default();
unsafe {
CONFIG = Some(config);
/// this default config is used only for testing
pub fn init_default_config() {
let mut l = GUARD.lock().unwrap();
unsafe {
if BASE_DATA_DIR.is_some() { return }
let base_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
BASE_DATA_DIR = Some(base_dir);
}

let config = Config::default();
unsafe {
CONFIG = Some(config);
}
*l = true
}

}


#[cfg(test)]
mod tests {
use super::*;

use crate::config::init::init_default_config;
#[test]
fn test_default_serialize() {
init_default_config();
Expand Down
4 changes: 2 additions & 2 deletions src/services/audio_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ pub fn get_real_file_type<P: AsRef<Path>>(
#[cfg(test)]
mod tests {
use super::*;
use crate::config::init_default_config;
use crate::config::init::init_default_config;
use serde_json;

#[test]
Expand Down Expand Up @@ -518,7 +518,7 @@ mod tests {

#[test]
fn test_chapters_file() {
pretty_env_logger::init();
//pretty_env_logger::init();
let path = Path::new("./test_data/01-file.mp3");
let chapters = chapters_from_csv(path).unwrap().unwrap();
assert_eq!(3, chapters.len());
Expand Down
2 changes: 1 addition & 1 deletion src/services/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ mod cache {
#[cfg(test)]
mod tests {
use super::*;
use crate::config::init_default_config;
use crate::config::init::init_default_config;

const TEST_DATA_DIR: &str = "./test_data";

Expand Down

0 comments on commit 32cc628

Please sign in to comment.