Skip to content

Commit

Permalink
aw-server: Refactor dirs, add tests do dirs and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
johan-bjareholt committed May 29, 2020
1 parent 826b78a commit 9b406fc
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 50 deletions.
6 changes: 5 additions & 1 deletion aw-server/src/android/mod.rs
Expand Up @@ -43,7 +43,11 @@ pub mod android {
match DATASTORE {
Some(ref ds) => ds.clone(),
None => {
let db_dir = dirs::db_path(false).to_str().unwrap().to_string();
let db_dir = dirs::db_path(false)
.expect("Failed to get db path")
.to_str()
.unwrap()
.to_string();
DATASTORE = Some(Datastore::new(db_dir, false));
openDatastore()
}
Expand Down
99 changes: 51 additions & 48 deletions aw-server/src/dirs.rs
Expand Up @@ -13,79 +13,82 @@ lazy_static! {
));
}

#[cfg(not(target_os = "android"))]
pub fn get_config_dir() -> Result<PathBuf, ()> {
#[cfg(not(target_os = "android"))]
{
let mut dir = appdirs::user_config_dir(Some("activitywatch"), None, false)?;
dir.push("aw-server-rust");
fs::create_dir_all(dir.clone()).expect("Unable to create config dir");
Ok(dir)
}
let mut dir = appdirs::user_config_dir(Some("activitywatch"), None, false)?;
dir.push("aw-server-rust");
fs::create_dir_all(dir.clone()).expect("Unable to create config dir");
Ok(dir)
}

#[cfg(target_os = "android")]
{
panic!("not implemented on Android");
}
#[cfg(target_os = "android")]
pub fn get_config_dir() -> Result<PathBuf, ()> {
panic!("not implemented on Android");
}

#[cfg(not(target_os = "android"))]
pub fn get_data_dir() -> Result<PathBuf, ()> {
#[cfg(not(target_os = "android"))]
{
let mut dir = appdirs::user_data_dir(Some("activitywatch"), None, false)?;
dir.push("aw-server-rust");
fs::create_dir_all(dir.clone()).expect("Unable to create data dir");
Ok(dir)
}
let mut dir = appdirs::user_data_dir(Some("activitywatch"), None, false)?;
dir.push("aw-server-rust");
fs::create_dir_all(dir.clone()).expect("Unable to create data dir");
Ok(dir)
}

#[cfg(target_os = "android")]
{
return Ok(ANDROID_DATA_DIR.lock().unwrap().to_path_buf());
}
#[cfg(target_os = "android")]
pub fn get_data_dir() -> Result<PathBuf, ()> {
return Ok(ANDROID_DATA_DIR.lock().unwrap().to_path_buf());
}

#[cfg(not(target_os = "android"))]
pub fn get_cache_dir() -> Result<PathBuf, ()> {
#[cfg(not(target_os = "android"))]
{
let mut dir = appdirs::user_cache_dir(Some("activitywatch"), None)?;
dir.push("aw-server-rust");
fs::create_dir_all(dir.clone()).expect("Unable to create cache dir");
Ok(dir)
}
let mut dir = appdirs::user_cache_dir(Some("activitywatch"), None)?;
dir.push("aw-server-rust");
fs::create_dir_all(dir.clone()).expect("Unable to create cache dir");
Ok(dir)
}

#[cfg(target_os = "android")]
{
panic!("not implemented on Android");
}
#[cfg(target_os = "android")]
pub fn get_cache_dir() -> Result<PathBuf, ()> {
panic!("not implemented on Android");
}

#[cfg(not(target_os = "android"))]
pub fn get_log_dir() -> Result<PathBuf, ()> {
#[cfg(not(target_os = "android"))]
{
let mut dir = appdirs::user_cache_dir(Some("activitywatch"), None)?;
dir.push("log");
dir.push("aw-server-rust");
fs::create_dir_all(dir.clone()).expect("Unable to create cache dir");
Ok(dir)
}
let mut dir = appdirs::user_cache_dir(Some("activitywatch"), None)?;
dir.push("log");
dir.push("aw-server-rust");
fs::create_dir_all(dir.clone()).expect("Unable to create cache dir");
Ok(dir)
}

#[cfg(target_os = "android")]
{
panic!("not implemented on Android");
}
#[cfg(target_os = "android")]
pub fn get_log_dir() -> Result<PathBuf, ()> {
panic!("not implemented on Android");
}

pub fn db_path(testing: bool) -> PathBuf {
let mut db_path = get_data_dir().unwrap();
pub fn db_path(testing: bool) -> Result<PathBuf, ()> {
let mut db_path = get_data_dir()?;
if testing {
db_path.push("sqlite-testing.db");
} else {
db_path.push("sqlite.db");
}
db_path
Ok(db_path)
}

#[cfg(target_os = "android")]
pub fn set_android_data_dir(path: &str) {
let mut android_data_dir = ANDROID_DATA_DIR.lock().unwrap();
*android_data_dir = PathBuf::from(path);
}

#[test]
fn test_get_dirs() {
#[cfg(target_os = "android")]
set_android_data_dir("/test");

get_cache_dir().unwrap();
get_log_dir().unwrap();
db_path(true).unwrap();
db_path(false).unwrap();
}
5 changes: 5 additions & 0 deletions aw-server/src/logging.rs
Expand Up @@ -59,3 +59,8 @@ pub fn setup_logger(testing: bool) -> Result<(), fern::InitError> {

Ok(())
}

#[test]
fn test_setup_logger() {
setup_logger(true).unwrap();
}
6 changes: 5 additions & 1 deletion aw-server/src/main.rs
Expand Up @@ -46,7 +46,11 @@ fn main() {

let config = config::create_config(testing);

let db_path = dirs::db_path(testing).to_str().unwrap().to_string();
let db_path = dirs::db_path(testing)
.expect("Failed to get db path")
.to_str()
.unwrap()
.to_string();
info!("Using DB at path {:?}", db_path);

let asset_path = get_asset_path();
Expand Down

0 comments on commit 9b406fc

Please sign in to comment.