Skip to content

Commit 9b406fc

Browse files
aw-server: Refactor dirs, add tests do dirs and logging
1 parent 826b78a commit 9b406fc

File tree

4 files changed

+66
-50
lines changed

4 files changed

+66
-50
lines changed

aw-server/src/android/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ pub mod android {
4343
match DATASTORE {
4444
Some(ref ds) => ds.clone(),
4545
None => {
46-
let db_dir = dirs::db_path(false).to_str().unwrap().to_string();
46+
let db_dir = dirs::db_path(false)
47+
.expect("Failed to get db path")
48+
.to_str()
49+
.unwrap()
50+
.to_string();
4751
DATASTORE = Some(Datastore::new(db_dir, false));
4852
openDatastore()
4953
}

aw-server/src/dirs.rs

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,79 +13,82 @@ lazy_static! {
1313
));
1414
}
1515

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

25-
#[cfg(target_os = "android")]
26-
{
27-
panic!("not implemented on Android");
28-
}
24+
#[cfg(target_os = "android")]
25+
pub fn get_config_dir() -> Result<PathBuf, ()> {
26+
panic!("not implemented on Android");
2927
}
3028

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

40-
#[cfg(target_os = "android")]
41-
{
42-
return Ok(ANDROID_DATA_DIR.lock().unwrap().to_path_buf());
43-
}
37+
#[cfg(target_os = "android")]
38+
pub fn get_data_dir() -> Result<PathBuf, ()> {
39+
return Ok(ANDROID_DATA_DIR.lock().unwrap().to_path_buf());
4440
}
4541

42+
#[cfg(not(target_os = "android"))]
4643
pub fn get_cache_dir() -> Result<PathBuf, ()> {
47-
#[cfg(not(target_os = "android"))]
48-
{
49-
let mut dir = appdirs::user_cache_dir(Some("activitywatch"), None)?;
50-
dir.push("aw-server-rust");
51-
fs::create_dir_all(dir.clone()).expect("Unable to create cache dir");
52-
Ok(dir)
53-
}
44+
let mut dir = appdirs::user_cache_dir(Some("activitywatch"), None)?;
45+
dir.push("aw-server-rust");
46+
fs::create_dir_all(dir.clone()).expect("Unable to create cache dir");
47+
Ok(dir)
48+
}
5449

55-
#[cfg(target_os = "android")]
56-
{
57-
panic!("not implemented on Android");
58-
}
50+
#[cfg(target_os = "android")]
51+
pub fn get_cache_dir() -> Result<PathBuf, ()> {
52+
panic!("not implemented on Android");
5953
}
6054

55+
#[cfg(not(target_os = "android"))]
6156
pub fn get_log_dir() -> Result<PathBuf, ()> {
62-
#[cfg(not(target_os = "android"))]
63-
{
64-
let mut dir = appdirs::user_cache_dir(Some("activitywatch"), None)?;
65-
dir.push("log");
66-
dir.push("aw-server-rust");
67-
fs::create_dir_all(dir.clone()).expect("Unable to create cache dir");
68-
Ok(dir)
69-
}
57+
let mut dir = appdirs::user_cache_dir(Some("activitywatch"), None)?;
58+
dir.push("log");
59+
dir.push("aw-server-rust");
60+
fs::create_dir_all(dir.clone()).expect("Unable to create cache dir");
61+
Ok(dir)
62+
}
7063

71-
#[cfg(target_os = "android")]
72-
{
73-
panic!("not implemented on Android");
74-
}
64+
#[cfg(target_os = "android")]
65+
pub fn get_log_dir() -> Result<PathBuf, ()> {
66+
panic!("not implemented on Android");
7567
}
7668

77-
pub fn db_path(testing: bool) -> PathBuf {
78-
let mut db_path = get_data_dir().unwrap();
69+
pub fn db_path(testing: bool) -> Result<PathBuf, ()> {
70+
let mut db_path = get_data_dir()?;
7971
if testing {
8072
db_path.push("sqlite-testing.db");
8173
} else {
8274
db_path.push("sqlite.db");
8375
}
84-
db_path
76+
Ok(db_path)
8577
}
8678

8779
#[cfg(target_os = "android")]
8880
pub fn set_android_data_dir(path: &str) {
8981
let mut android_data_dir = ANDROID_DATA_DIR.lock().unwrap();
9082
*android_data_dir = PathBuf::from(path);
9183
}
84+
85+
#[test]
86+
fn test_get_dirs() {
87+
#[cfg(target_os = "android")]
88+
set_android_data_dir("/test");
89+
90+
get_cache_dir().unwrap();
91+
get_log_dir().unwrap();
92+
db_path(true).unwrap();
93+
db_path(false).unwrap();
94+
}

aw-server/src/logging.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ pub fn setup_logger(testing: bool) -> Result<(), fern::InitError> {
5959

6060
Ok(())
6161
}
62+
63+
#[test]
64+
fn test_setup_logger() {
65+
setup_logger(true).unwrap();
66+
}

aw-server/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ fn main() {
4646

4747
let config = config::create_config(testing);
4848

49-
let db_path = dirs::db_path(testing).to_str().unwrap().to_string();
49+
let db_path = dirs::db_path(testing)
50+
.expect("Failed to get db path")
51+
.to_str()
52+
.unwrap()
53+
.to_string();
5054
info!("Using DB at path {:?}", db_path);
5155

5256
let asset_path = get_asset_path();

0 commit comments

Comments
 (0)