Skip to content

Commit

Permalink
Select sources for macos (#78)
Browse files Browse the repository at this point in the history
* Select sources for Chrome on macOS

* Select sources for Firefox on macOS

* Refactor code

* Refactor code

* Fix clippy warnings

* Fix profiles ini on macos
  • Loading branch information
quambene authored Mar 1, 2024
1 parent 6635028 commit 0477121
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 85 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Unreleased

- added
- Select sources for Chrome and Firefox on macOS
- changed
- removed

Expand Down
10 changes: 8 additions & 2 deletions src/bookmark_reader/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl SelectSource for ChromeSelector {
home_dir.join(".config/google-chrome"),
],
SourceOs::Windows => vec![home_dir.join("AppData\\Local\\Google\\Chrome\\User Data")],
SourceOs::Macos => vec![],
SourceOs::Macos => vec![home_dir.join("Library/Application Support/Google/Chrome")],
};
let bookmark_dirs = ChromiumSelector::find_profile_dirs(&browser_dirs);
let bookmark_files = bookmark_dirs
Expand Down Expand Up @@ -126,7 +126,13 @@ mod tests {
assert!(res.is_ok(), "Can't find dir: {}", res.unwrap_err());

let bookmark_dirs = res.unwrap();
assert!(bookmark_dirs.is_empty());
assert_eq!(bookmark_dirs.len(), 2);
assert!(bookmark_dirs.contains(
&temp_path.join("Library/Application Support/Google/Chrome/Default/Bookmarks")
));
assert!(bookmark_dirs.contains(
&temp_path.join("Library/Application Support/Google/Chrome/Profile 1/Bookmarks")
));
}

#[cfg(target_os = "windows")]
Expand Down
43 changes: 37 additions & 6 deletions src/bookmark_reader/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ impl SelectSource for FirefoxSelector {
home_dir.join("snap/firefox/common/.mozilla/firefox"),
],
SourceOs::Windows => vec![],
SourceOs::Macos => vec![],
SourceOs::Macos => vec![home_dir.join("Library/Application Support/Firefox")],
};

for browser_dir in browser_dirs {
let profiles_path = &browser_dir.join("profiles.ini");
let profiles_path = browser_dir.join("profiles.ini");

if profiles_path.is_file() {
let profiles_file = utils::open_file(&browser_dir.join("profiles.ini"))?;
let profiles_file = utils::open_file(&profiles_path)?;
let buf_reader = BufReader::new(profiles_file);
let lines = buf_reader.lines();

Expand Down Expand Up @@ -341,10 +341,10 @@ mod tests {
));
assert!(sources.contains(
&temp_path
.join("snap/firefox/common/.mozilla/firefox/profile1.username/bookmarkbackups")
.join("snap/firefox/common/.mozilla/firefox/profile2.username/bookmarkbackups")
));
assert!(
sources.contains(&temp_path.join(".mozilla/firefox/profile2.default/bookmarkbackups"))
sources.contains(&temp_path.join(".mozilla/firefox/profile1.default/bookmarkbackups"))
);
assert!(
sources.contains(&temp_path.join(".mozilla/firefox/profile2.username/bookmarkbackups"))
Expand All @@ -366,7 +366,13 @@ mod tests {
assert!(res.is_ok(), "{}", res.unwrap_err());

let sources = res.unwrap();
assert!(sources.is_empty());
assert_eq!(sources.len(), 2);
assert!(sources.contains(&temp_path.join(
"Library/Application Support/Firefox/Profiles/profile1.default/bookmarkbackups"
)));
assert!(sources.contains(&temp_path.join(
"Library/Application Support/Firefox/Profiles/profile2.username/bookmarkbackups"
)));
}

#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -412,6 +418,31 @@ mod tests {
);
}

#[cfg(not(any(target_os = "windows")))]
#[test]
fn test_find_source_file_macos() {
let temp_dir = tempdir().unwrap();
let temp_path = temp_dir.path();
assert!(temp_path.exists(), "Missing path: {}", temp_path.display());

let bookmark_dir = temp_path
.join("Library/Application Support/Firefox/Profiles/profile1.default/bookmarkbackups");
fs::create_dir_all(&bookmark_dir).unwrap();
utils::create_file(&bookmark_dir.join("bookmarks1.jsonlz4")).unwrap();
utils::create_file(&bookmark_dir.join("bookmarks2.jsonlz4")).unwrap();

let selector = FirefoxSelector;
let res = selector.find_source_file(&bookmark_dir);
assert!(res.is_ok(), "Can't find dir: {}", res.unwrap_err());

let bookmark_file = res.unwrap();
assert_eq!(
bookmark_file.unwrap(),
// We are expecting the file which was created more recently.
temp_path.join("Library/Application Support/Firefox/Profiles/profile1.default/bookmarkbackups/bookmarks2.jsonlz4")
);
}

#[test]
fn test_read_and_parse() {
let source_path = Path::new("test_data/bookmarks_firefox.json");
Expand Down
2 changes: 1 addition & 1 deletion src/bookmark_reader/source_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ mod tests {
test_utils::tests::create_test_files(temp_path, &source_os);

let sources = SourceReader::select_sources(temp_path, &source_os).unwrap();
assert_eq!(sources.len(), 1);
assert_eq!(sources.len(), 5);
}

#[cfg(target_os = "windows")]
Expand Down
152 changes: 76 additions & 76 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,43 @@ pub mod tests {
use crate::bookmark_reader::SourceOs;
use std::fs::{self, File};

fn create_browser_dirs(browser_dir: &Path) {
const PROFILES_INI_LINUX: &str = r#"
[Profile2]
Name=bene
IsRelative=1
Path=profile3.username
[Profile1]
Name=bene
IsRelative=1
Path=profile2.username
Default=1
[Profile0]
Name=default
IsRelative=1
Path=profile1.default
"#;

const PROFILES_INI_MACOS: &str = r#"
[Profile2]
Name=bene
IsRelative=1
Path=Profiles/profile3.username
[Profile1]
Name=bene
IsRelative=1
Path=Profiles/profile2.username
Default=1
[Profile0]
Name=default
IsRelative=1
Path=Profiles/profile1.default
"#;

fn create_chromium_dirs(browser_dir: &Path) {
let default_profile_dir = browser_dir.join("Default");
let profile_dir = browser_dir.join("Profile 1");

Expand All @@ -65,105 +101,69 @@ pub mod tests {
File::create(profile_file).unwrap();
}

fn create_chromium_dirs_linux(home_dir: &Path) {
let browser_dir = home_dir.join("snap/chromium/common/chromium");
create_browser_dirs(&browser_dir);
}

fn create_chrome_dirs_linux(home_dir: &Path) {
let browser_dir = home_dir.join(".config/google-chrome");
create_browser_dirs(&browser_dir);
}

fn create_chrome_dirs_windows(home_dir: &Path) {
let browser_dir = home_dir.join("AppData\\Local\\Google\\Chrome\\User Data");
create_browser_dirs(&browser_dir);
}

fn create_edge_dirs_linux(home_dir: &Path) {
let browser_dir = home_dir.join(".config/microsoft-edge");
create_browser_dirs(&browser_dir);
}

fn create_edge_dirs_windows(home_dir: &Path) {
let browser_dir = home_dir.join("AppData\\Local\\Microsoft\\Edge\\User Data");
create_browser_dirs(&browser_dir);
}

fn create_safari_dirs_macos(home_dir: &Path) {
let safari_dir = home_dir.join("Library/Safari");
fs::create_dir_all(&safari_dir).unwrap();
let safari_file = safari_dir.join("Bookmarks.plist");
utils::create_file(&safari_file).unwrap();
fn create_safari_dirs(browser_dir: &Path) {
fs::create_dir_all(browser_dir).unwrap();
utils::create_file(&browser_dir.join("Bookmarks.plist")).unwrap();
}

fn create_firefox_dirs_linux(home_dir: &Path) {
let browser_dir = home_dir.join("snap/firefox/common/.mozilla/firefox");
fn create_firefox_dirs_linux(browser_dir: &Path) {
let profile_dir1 = browser_dir.join("profile1.default/bookmarkbackups");
let profile_dir2 = browser_dir.join("profile1.username/bookmarkbackups");
let profile_dir2 = browser_dir.join("profile2.username/bookmarkbackups");
fs::create_dir_all(&profile_dir1).unwrap();
fs::create_dir_all(&profile_dir2).unwrap();
utils::create_file(&profile_dir1.join("bookmarks.jsonlz4")).unwrap();
utils::create_file(&profile_dir2.join("bookmarks.jsonlz4")).unwrap();
let mut file = utils::create_file(&browser_dir.join("profiles.ini")).unwrap();
let content = r#"
[Profile2]
Name=bene
IsRelative=1
Path=profile3.username
[Profile1]
Name=bene
IsRelative=1
Path=profile1.username
Default=1
[Profile0]
Name=default
IsRelative=1
Path=profile1.default
"#;
file.write_all(content.as_bytes()).unwrap();
file.write_all(PROFILES_INI_LINUX.as_bytes()).unwrap();
file.flush().unwrap();
}

let browser_dir = home_dir.join(".mozilla/firefox");
let profile_dir1 = browser_dir.join("profile2.default/bookmarkbackups");
let profile_dir2 = browser_dir.join("profile2.username/bookmarkbackups");
fn create_firefox_dirs_macos(browser_dir: &Path) {
let profile_dir1 = browser_dir.join("Profiles/profile1.default/bookmarkbackups");
let profile_dir2 = browser_dir.join("Profiles/profile2.username/bookmarkbackups");
fs::create_dir_all(&profile_dir1).unwrap();
fs::create_dir_all(&profile_dir2).unwrap();
utils::create_file(&profile_dir1.join("bookmarks.jsonlz4")).unwrap();
utils::create_file(&profile_dir2.join("bookmarks.jsonlz4")).unwrap();
let mut file = File::create(browser_dir.join("profiles.ini")).unwrap();
let content = r#"
[Profile1]
Name=bene
IsRelative=1
Path=profile2.username
Default=1
[Profile0]
Name=default
IsRelative=1
Path=profile2.default
"#;
file.write_all(content.as_bytes()).unwrap();
let mut file = utils::create_file(&browser_dir.join("profiles.ini")).unwrap();
file.write_all(PROFILES_INI_MACOS.as_bytes()).unwrap();
file.flush().unwrap();
}

pub fn create_test_files(home_dir: &Path, source_os: &SourceOs) {
match source_os {
SourceOs::Linux => {
create_firefox_dirs_linux(home_dir);
create_chromium_dirs_linux(home_dir);
create_chrome_dirs_linux(home_dir);
create_edge_dirs_linux(home_dir);
let browser_dir = home_dir.join("snap/firefox/common/.mozilla/firefox");
create_firefox_dirs_linux(&browser_dir);

let browser_dir = home_dir.join(".mozilla/firefox");
create_firefox_dirs_linux(&browser_dir);

let browser_dir = home_dir.join("snap/chromium/common/chromium");
create_chromium_dirs(&browser_dir);

let browser_dir = home_dir.join(".config/google-chrome");
create_chromium_dirs(&browser_dir);

let browser_dir = home_dir.join(".config/microsoft-edge");
create_chromium_dirs(&browser_dir);
}
SourceOs::Macos => {
create_safari_dirs_macos(home_dir);
let browser_dir = home_dir.join("Library/Safari");
create_safari_dirs(&browser_dir);

let browser_dir = home_dir.join("Library/Application Support/Firefox");
create_firefox_dirs_macos(&browser_dir);

let browser_dir = home_dir.join("Library/Application Support/Google/Chrome");
create_chromium_dirs(&browser_dir);
}
SourceOs::Windows => {
create_chrome_dirs_windows(home_dir);
create_edge_dirs_windows(home_dir);
let browser_dir = home_dir.join("AppData\\Local\\Google\\Chrome\\User Data");
create_chromium_dirs(&browser_dir);

let browser_dir = home_dir.join("AppData\\Local\\Microsoft\\Edge\\User Data");
create_chromium_dirs(&browser_dir);
}
}
}
Expand Down

0 comments on commit 0477121

Please sign in to comment.