diff --git a/src/main.rs b/src/main.rs index 4cb7a9a0..09996a0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ mod input; mod macros; mod output; mod rpc; +#[cfg(test)] mod tests; mod utils; @@ -65,7 +66,7 @@ fn status_file(stat_file: &str, playout_stat: &PlayoutStatus) { fn main() { // Init the config, set process controller, create logging. - init_config(); + init_config(None); let config = GlobalConfig::global(); let play_control = PlayerControl::new(); let playout_stat = PlayoutStatus::new(); diff --git a/src/tests/mod.rs b/src/tests/mod.rs index fc59a11e..128d68ec 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,43 +1,78 @@ -#[cfg(test)] -use chrono::prelude::*; +use std::{ + process, + thread::{self, sleep}, + time::Duration, +}; +mod utils; + +#[cfg(test)] +use crate::output::player; #[cfg(test)] use crate::utils::*; - #[cfg(test)] -fn get_fake_date_time(date_time: &str) -> DateTime { - let date_obj = NaiveDateTime::parse_from_str(date_time, "%Y-%m-%dT%H:%M:%S"); +use simplelog::*; - Local.from_local_datetime(&date_obj.unwrap()).unwrap() +fn timed_kill(sec: u64, mut proc_ctl: ProcessControl) { + sleep(Duration::from_secs(sec)); + + proc_ctl.kill_all(); + + process::exit(0); } #[test] -fn mock_date_time() { - let fake_time = get_fake_date_time("2022-05-20T06:00:00"); - mock_time::set_mock_time(fake_time); +#[ignore] +fn playlist_change_at_midnight() { + let config = TestConfig { + mode: "playlist".into(), + start: "00:00:00".into(), + length: "24:00:00".into(), + log_to_file: false, + mail_recipient: "".into(), + }; - assert_eq!( - fake_time.format("%Y-%m-%dT%H:%M:%S.2f").to_string(), - time_now().format("%Y-%m-%dT%H:%M:%S.2f").to_string() - ); + init_config(Some(config)); + + let play_control = PlayerControl::new(); + let playout_stat = PlayoutStatus::new(); + let proc_control = ProcessControl::new(); + let proc_ctl = proc_control.clone(); + + let logging = init_logging(); + CombinedLogger::init(logging).unwrap(); + + mock_time::set_mock_time("2022-05-09T23:59:45"); + + thread::spawn(move || timed_kill(30, proc_ctl)); + + player(play_control, playout_stat, proc_control); } #[test] -fn get_date_yesterday() { - let fake_time = get_fake_date_time("2022-05-20T05:59:24"); - mock_time::set_mock_time(fake_time); +#[ignore] +fn playlist_change_at_six() { + let config = TestConfig { + mode: "playlist".into(), + start: "06:00:00".into(), + length: "24:00:00".into(), + log_to_file: false, + mail_recipient: "".into(), + }; - let date = get_date(true, 21600.0, 86400.0); + init_config(Some(config)); - assert_eq!("2022-05-19".to_string(), date); -} - -#[test] -fn get_date_tomorrow() { - let fake_time = get_fake_date_time("2022-05-20T23:59:30"); - mock_time::set_mock_time(fake_time); - - let date = get_date(false, 0.0, 86400.01); - - assert_eq!("2022-05-21".to_string(), date); + let play_control = PlayerControl::new(); + let playout_stat = PlayoutStatus::new(); + let proc_control = ProcessControl::new(); + let proc_ctl = proc_control.clone(); + + let logging = init_logging(); + CombinedLogger::init(logging).unwrap(); + + mock_time::set_mock_time("2022-05-09T05:59:45"); + + thread::spawn(move || timed_kill(30, proc_ctl)); + + player(play_control, playout_stat, proc_control); } diff --git a/src/tests/utils/mod.rs b/src/tests/utils/mod.rs new file mode 100644 index 00000000..0009f476 --- /dev/null +++ b/src/tests/utils/mod.rs @@ -0,0 +1,37 @@ +#[cfg(test)] +use chrono::prelude::*; + +#[cfg(test)] +use crate::utils::*; + +#[test] +fn mock_date_time() { + let time_str = "2022-05-20T06:00:00"; + let date_obj = NaiveDateTime::parse_from_str(time_str, "%Y-%m-%dT%H:%M:%S"); + let time = Local.from_local_datetime(&date_obj.unwrap()).unwrap(); + + mock_time::set_mock_time(time_str); + + assert_eq!( + time.format("%Y-%m-%dT%H:%M:%S.2f").to_string(), + time_now().format("%Y-%m-%dT%H:%M:%S.2f").to_string() + ); +} + +#[test] +fn get_date_yesterday() { + mock_time::set_mock_time("2022-05-20T05:59:24"); + + let date = get_date(true, 21600.0, 86400.0); + + assert_eq!("2022-05-19".to_string(), date); +} + +#[test] +fn get_date_tomorrow() { + mock_time::set_mock_time("2022-05-20T23:59:30"); + + let date = get_date(false, 0.0, 86400.01); + + assert_eq!("2022-05-21".to_string(), date); +} diff --git a/src/utils/arg_parse.rs b/src/utils/arg_parse.rs index 7adb6211..09490383 100644 --- a/src/utils/arg_parse.rs +++ b/src/utils/arg_parse.rs @@ -55,6 +55,12 @@ pub struct Args { } /// Get arguments from command line, and return them. +#[cfg(not(test))] pub fn get_args() -> Args { Args::parse() } + +#[cfg(test)] +pub fn get_args() -> Args { + Args::parse_from(["-o desktop"].iter()) +} diff --git a/src/utils/config.rs b/src/utils/config.rs index ceac3a07..a2d87655 100644 --- a/src/utils/config.rs +++ b/src/utils/config.rs @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; use serde_yaml::{self}; use shlex::split; -use crate::utils::{get_args, time_to_sec}; +use crate::utils::{get_args, time_to_sec, TestConfig}; use crate::vec_strings; /// Global Config @@ -288,7 +288,24 @@ fn pre_audio_codec(add_loudnorm: bool) -> Vec { codec } -pub fn init_config() { +#[cfg(not(test))] +pub fn init_config(_: Option) { let config = GlobalConfig::new(); INSTANCE.set(config).unwrap(); } + +#[cfg(test)] +pub fn init_config(test_config: Option) { + let mut config = GlobalConfig::new(); + config.out.mode = "desktop".into(); + if let Some(cfg) = test_config { + config.logging.log_to_file = cfg.log_to_file; + config.mail.recipient = cfg.mail_recipient; + config.playlist.day_start = cfg.start.clone(); + config.playlist.start_sec = Some(time_to_sec(&cfg.start)); + config.playlist.length = cfg.length.clone(); + config.playlist.length_sec = Some(time_to_sec(&cfg.length)); + }; + + INSTANCE.set(config).unwrap(); +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index f57b9875..84339a81 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -506,6 +506,15 @@ pub fn validate_ffmpeg() { } } +/// In test cases we override some configuration values to fit the needs. +pub struct TestConfig { + pub mode: String, + pub start: String, + pub length: String, + pub log_to_file: bool, + pub mail_recipient: String, +} + /// Get system time, in non test case. #[cfg(not(test))] pub fn time_now() -> DateTime { @@ -529,7 +538,10 @@ pub mod mock_time { }) } - pub fn set_mock_time(time: DateTime) { + pub fn set_mock_time(date_time: &str) { + let date_obj = NaiveDateTime::parse_from_str(date_time, "%Y-%m-%dT%H:%M:%S"); + let time = Local.from_local_datetime(&date_obj.unwrap()).unwrap(); + DATE_TIME_DIFF.with(|cell| *cell.borrow_mut() = Some(Local::now() - time)); } }