Skip to content

Commit

Permalink
Implement source selector (part 2) (#76)
Browse files Browse the repository at this point in the history
* Improve documentation

* Implement dry run

* Select source for windows

* Fix clippy warnings

* Fix tests on windows

* Fix tests on windows II
  • Loading branch information
quambene authored Feb 27, 2024
1 parent 38abcdb commit 280ec75
Show file tree
Hide file tree
Showing 18 changed files with 739 additions and 209 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### Unreleased

- added
- Implement dry mode for import: `bogrep import --dry-run`
- Implement running in dry mode
- changed
- Select sources for `bogrep import` if no sources are configured
- Update rust toolchain to 1.76
Expand Down
9 changes: 7 additions & 2 deletions benches/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bogrep::{
errors::BogrepError, html, Action, BookmarkProcessor, Cache, CacheMode, Caching, Fetch,
MockClient, Settings, TargetBookmark, TargetBookmarks,
MockClient, ProcessReport, Settings, TargetBookmark, TargetBookmarks,
};
use chrono::Utc;
use criterion::{criterion_group, criterion_main, Criterion};
Expand Down Expand Up @@ -82,7 +82,12 @@ async fn fetch_concurrently(max_concurrent_requests: usize) {
let mut bookmarks = TargetBookmarks::new(bookmarks);
assert_eq!(bookmarks.len(), 10000);

let bookmark_processor = BookmarkProcessor::new(client.clone(), cache.clone(), settings);
let bookmark_processor = BookmarkProcessor::new(
client.clone(),
cache.clone(),
settings,
ProcessReport::default(),
);
bookmark_processor
.process_bookmarks(bookmarks.values_mut().collect())
.await
Expand Down
9 changes: 9 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ pub struct FetchArgs {
/// If an URL is missing in the bookmarks, it will be imported.
#[arg(long, num_args = 0.., value_delimiter = ' ')]
pub urls: Vec<String>,
/// Run command in dry mode.
#[arg(short = 'n', long = "dry-run")]
pub dry_run: bool,
}

/// Describes the arguments for the `init` subcommand.
Expand All @@ -136,6 +139,9 @@ pub struct InitArgs {
/// Cache the fetched bookmarks as text, HTML or markdown file.
#[arg(short, long, value_enum)]
pub mode: Option<CacheMode>,
/// Run command in dry mode.
#[arg(short = 'n', long = "dry-run")]
pub dry_run: bool,
}

/// Describes the arguments for the `update` subcommand.
Expand All @@ -144,6 +150,9 @@ pub struct UpdateArgs {
/// Cache the fetched bookmarks as text, HTML or markdown file.
#[arg(short, long, value_enum)]
pub mode: Option<CacheMode>,
/// Run command in dry mode.
#[arg(short = 'n', long = "dry-run")]
pub dry_run: bool,
}

/// Describes the arguments for the `clean` subcommand.
Expand Down
85 changes: 72 additions & 13 deletions src/bookmark_reader/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ impl SelectSource for ChromeSelector {
SourceType::Chrome
}

fn source_os(&self) -> SourceOs {
SourceOs::Linux
}

fn extension(&self) -> Option<&str> {
Some("json")
}

fn find_sources(&self, home_dir: &Path) -> Result<Vec<PathBuf>, anyhow::Error> {
fn find_sources(
&self,
home_dir: &Path,
source_os: &SourceOs,
) -> Result<Vec<PathBuf>, anyhow::Error> {
debug!("Find sources for {}", self.name());
let browser_dirs = [
// apt package
home_dir.join(".config/google-chrome"),
];

let browser_dirs = match source_os {
SourceOs::Linux => vec![
// apt package
home_dir.join(".config/google-chrome"),
],
SourceOs::Windows => vec![home_dir.join("AppData\\Local\\Google\\Chrome\\User Data")],
SourceOs::Macos => vec![],
};
let bookmark_dirs = ChromiumSelector::find_profile_dirs(&browser_dirs);
let bookmark_files = bookmark_dirs
.into_iter()
Expand Down Expand Up @@ -67,23 +72,35 @@ mod tests {
assert!(temp_path.exists(), "Missing path: {}", temp_path.display());

let selector = ChromeSelector;
let res = selector.find_sources(temp_path);

let res = selector.find_sources(temp_path, &SourceOs::Linux);
assert!(res.is_ok(), "{}", res.unwrap_err());
let sources = res.unwrap();
assert!(sources.is_empty());

let res = selector.find_sources(temp_path, &SourceOs::Macos);
assert!(res.is_ok(), "{}", res.unwrap_err());
let sources = res.unwrap();
assert!(sources.is_empty());

let res = selector.find_sources(temp_path, &SourceOs::Windows);
assert!(res.is_ok(), "{}", res.unwrap_err());
let sources = res.unwrap();
assert!(sources.is_empty());
}

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

tests::create_test_files(temp_path);
tests::create_test_files(temp_path, &source_os);

let selector = ChromeSelector;
let res: Result<Vec<PathBuf>, anyhow::Error> = selector.find_sources(temp_path);
let res: Result<Vec<PathBuf>, anyhow::Error> = selector.find_sources(temp_path, &source_os);
assert!(res.is_ok(), "Can't find dir: {}", res.unwrap_err());

let bookmark_dirs = res.unwrap();
Expand All @@ -93,4 +110,46 @@ mod tests {
bookmark_dirs.contains(&temp_path.join(".config/google-chrome/Profile 1/Bookmarks"))
);
}

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

tests::create_test_files(temp_path, &source_os);

let selector = ChromeSelector;
let res: Result<Vec<PathBuf>, anyhow::Error> = selector.find_sources(temp_path, &source_os);
assert!(res.is_ok(), "Can't find dir: {}", res.unwrap_err());

let bookmark_dirs = res.unwrap();
assert!(bookmark_dirs.is_empty());
}

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

tests::create_test_files(temp_path, &source_os);

let selector = ChromeSelector;
let res: Result<Vec<PathBuf>, anyhow::Error> = selector.find_sources(temp_path, &source_os);
assert!(res.is_ok(), "Can't find dir: {}", res.unwrap_err());

let bookmark_dirs = res.unwrap();
assert_eq!(bookmark_dirs.len(), 2);
assert!(bookmark_dirs.contains(
&temp_path.join("AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks")
));
assert!(bookmark_dirs.contains(
&temp_path.join("AppData\\Local\\Google\\Chrome\\User Data\\Profile 1\\Bookmarks")
));
}
}
79 changes: 66 additions & 13 deletions src/bookmark_reader/chromium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,25 @@ impl SelectSource for ChromiumSelector {
SourceType::Chromium
}

fn source_os(&self) -> super::SourceOs {
SourceOs::Linux
}

fn extension(&self) -> Option<&str> {
Some("json")
}

fn find_sources(&self, home_dir: &Path) -> Result<Vec<PathBuf>, anyhow::Error> {
fn find_sources(
&self,
home_dir: &Path,
source_os: &SourceOs,
) -> Result<Vec<PathBuf>, anyhow::Error> {
debug!("Find sources for {}", self.name());
let browser_dirs = [
// snap package
home_dir.join("snap/chromium/common/chromium"),
];

let browser_dirs = match source_os {
SourceOs::Linux => vec![
// snap package
home_dir.join("snap/chromium/common/chromium"),
],
SourceOs::Windows => vec![],
SourceOs::Macos => vec![],
};
let bookmark_dirs = ChromiumSelector::find_profile_dirs(&browser_dirs);
let bookmark_files = bookmark_dirs
.into_iter()
Expand Down Expand Up @@ -256,23 +261,35 @@ mod tests {
assert!(temp_path.exists(), "Missing path: {}", temp_path.display());

let selector = ChromiumSelector;
let res = selector.find_sources(temp_path);

let res = selector.find_sources(temp_path, &SourceOs::Linux);
assert!(res.is_ok(), "{}", res.unwrap_err());
let sources = res.unwrap();
assert!(sources.is_empty());

let res = selector.find_sources(temp_path, &SourceOs::Macos);
assert!(res.is_ok(), "{}", res.unwrap_err());
let sources = res.unwrap();
assert!(sources.is_empty());

let res = selector.find_sources(temp_path, &SourceOs::Windows);
assert!(res.is_ok(), "{}", res.unwrap_err());
let sources = res.unwrap();
assert!(sources.is_empty());
}

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

tests::create_test_files(temp_path);
tests::create_test_files(temp_path, &source_os);

let selector = ChromiumSelector;
let res = selector.find_sources(temp_path);
let res = selector.find_sources(temp_path, &source_os);
assert!(res.is_ok(), "Can't find dir: {}", res.unwrap_err());

let bookmark_dirs = res.unwrap();
Expand All @@ -283,6 +300,42 @@ mod tests {
.contains(&temp_path.join("snap/chromium/common/chromium/Profile 1/Bookmarks")));
}

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

tests::create_test_files(temp_path, &source_os);

let selector = ChromiumSelector;
let res = selector.find_sources(temp_path, &source_os);
assert!(res.is_ok(), "Can't find dir: {}", res.unwrap_err());

let bookmark_dirs = res.unwrap();
assert!(bookmark_dirs.is_empty());
}

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

tests::create_test_files(temp_path, &source_os);

let selector = ChromiumSelector;
let res = selector.find_sources(temp_path, &source_os);
assert!(res.is_ok(), "Can't find dir: {}", res.unwrap_err());

let bookmark_dirs = res.unwrap();
assert!(bookmark_dirs.is_empty());
}

#[test]
fn test_read_and_parse() {
let source_path = Path::new("test_data/bookmarks_chromium.json");
Expand Down
Loading

0 comments on commit 280ec75

Please sign in to comment.