From 6a359c96d349fbb1dd0e5087a6068962b8175c80 Mon Sep 17 00:00:00 2001 From: quambene Date: Sat, 21 Dec 2024 05:25:27 +0100 Subject: [PATCH] Test init (#96) * Test init * Improve error message * Update dependencies * Clean up configure * Add init args * Fix test * Update changelog * Fix unused import * Add documentation --- CHANGELOG.md | 2 + Cargo.lock | 127 +++++++++++++++++-------------------------- src/args.rs | 10 +++- src/cmd/configure.rs | 21 ++----- src/cmd/init.rs | 15 ++++- src/main.rs | 2 +- tests/test_init.rs | 29 ++++++++++ 7 files changed, 109 insertions(+), 97 deletions(-) create mode 100644 tests/test_init.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c9a82b..6651e79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - Fix `bogrep init` for empty folders - Fix `--replace` for `bogrep fetch --urls ` - Fix line breaks in text for paragraphs from HTML +- added + - Add `InitArgs` to `init()` ### v0.10.0 diff --git a/Cargo.lock b/Cargo.lock index f726d7c..ce770c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -270,9 +270,9 @@ dependencies = [ [[package]] name = "bstr" -version = "1.11.0" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a68f1f47cdf0ec8ee4b941b2eee2a80cb796db73118c0dd09ac63fbe405be22" +checksum = "786a307d683a5bf92e6fd5fd69a7eb613751668d1d8d67d802846dfe367c62c8" dependencies = [ "memchr", "regex-automata", @@ -305,9 +305,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.2.3" +version = "1.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27f657647bcff5394bf56c7317665bbf790a137a50eaaa5c6bfbb9e27a518f2d" +checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e" dependencies = [ "shlex", ] @@ -337,9 +337,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.38" +version = "0.4.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" dependencies = [ "android-tzdata", "iana-time-zone", @@ -433,12 +433,12 @@ checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" [[package]] name = "colored" -version = "2.1.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" dependencies = [ "lazy_static", - "windows-sys 0.48.0", + "windows-sys 0.59.0", ] [[package]] @@ -516,9 +516,9 @@ dependencies = [ [[package]] name = "crossbeam-deque" -version = "0.8.5" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" dependencies = [ "crossbeam-epoch", "crossbeam-utils", @@ -535,9 +535,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.20" +version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" [[package]] name = "crunchy" @@ -757,9 +757,9 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "float-cmp" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +checksum = "b09cf3155332e944990140d967ff5eceb70df778b34f77d8075db46e4704e6d8" dependencies = [ "num-traits", ] @@ -1021,9 +1021,9 @@ checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" [[package]] name = "hickory-proto" -version = "0.24.1" +version = "0.24.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07698b8420e2f0d6447a436ba999ec85d8fbf2a398bbd737b82cac4a2e96e512" +checksum = "447afdcdb8afb9d0a852af6dc65d9b285ce720ed7a59e42a8bf2e931c67bc1b5" dependencies = [ "async-trait", "cfg-if", @@ -1032,7 +1032,7 @@ dependencies = [ "futures-channel", "futures-io", "futures-util", - "idna 0.4.0", + "idna", "ipnet", "once_cell", "rand 0.8.5", @@ -1045,9 +1045,9 @@ dependencies = [ [[package]] name = "hickory-resolver" -version = "0.24.1" +version = "0.24.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28757f23aa75c98f254cf0405e6d8c25b831b32921b050a66692427679b1f243" +checksum = "0a2e2aba9c389ce5267d31cf1e4dace82390ae276b0b364ea55630b1fa1b44b4" dependencies = [ "cfg-if", "futures-util", @@ -1180,9 +1180,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hyper" -version = "0.14.31" +version = "0.14.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c08302e8fa335b151b788c775ff56e7a03ae64ff85c548ee820fecb70356e85" +checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7" dependencies = [ "bytes", "futures-channel", @@ -1356,16 +1356,6 @@ dependencies = [ "syn 2.0.90", ] -[[package]] -name = "idna" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" -dependencies = [ - "unicode-bidi", - "unicode-normalization", -] - [[package]] name = "idna" version = "1.0.3" @@ -1509,9 +1499,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.167" +version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" [[package]] name = "libredox" @@ -1654,9 +1644,9 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" [[package]] name = "miniz_oxide" -version = "0.8.0" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +checksum = "4ffbe83022cedc1d264172192511ae958937694cd57ce297164951b8b3568394" dependencies = [ "adler2", ] @@ -2032,9 +2022,9 @@ checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" [[package]] name = "predicates" -version = "3.1.2" +version = "3.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e9086cc7640c29a356d1a29fd134380bee9d8f79a17410aa76e7ad295f42c97" +checksum = "a5d19ee57562043d37e82899fade9a22ebab7be9cef5026b07fda9cdd4293573" dependencies = [ "anstyle", "difflib", @@ -2046,15 +2036,15 @@ dependencies = [ [[package]] name = "predicates-core" -version = "1.0.8" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae8177bee8e75d6846599c6b9ff679ed51e882816914eec639944d7c9aa11931" +checksum = "727e462b119fe9c93fd0eb1429a5f7647394014cf3c04ab2c0350eeb09095ffa" [[package]] name = "predicates-tree" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41b740d195ed3166cd147c8047ec98db0e22ec019eb8eeb76d343b795304fb13" +checksum = "72dd2d6d381dfb73a193c7fca536518d7caee39fc8503f74e7dc0be0531b425c" dependencies = [ "predicates-core", "termtree", @@ -2310,15 +2300,15 @@ dependencies = [ "log", "markup5ever_rcdom", "regex", - "thiserror 2.0.7", + "thiserror 2.0.8", "url", ] [[package]] name = "redox_syscall" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" dependencies = [ "bitflags 2.6.0", ] @@ -2518,9 +2508,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.12.1" +version = "2.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa39c7303dc58b5543c94d22c1766b0d31f2ee58306363ea622b10bbc075eaa2" +checksum = "1863fd3768cd83c56a7f60faa4dc0d403f1b6df0a38c3c25f44b7894e45370d5" dependencies = [ "core-foundation-sys", "libc", @@ -2547,18 +2537,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.215" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" +checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.215" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" +checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" dependencies = [ "proc-macro2", "quote", @@ -2834,9 +2824,9 @@ dependencies = [ [[package]] name = "termtree" -version = "0.4.1" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" +checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683" [[package]] name = "thiserror" @@ -2849,11 +2839,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.7" +version = "2.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93605438cbd668185516ab499d589afb7ee1859ea3d5fc8f6b0755e1c7443767" +checksum = "08f5383f3e0071702bf93ab5ee99b52d26936be9dedd9413067cbdcddcb6141a" dependencies = [ - "thiserror-impl 2.0.7", + "thiserror-impl 2.0.8", ] [[package]] @@ -2869,9 +2859,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.7" +version = "2.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1d8749b4531af2117677a5fcd12b1348a3fe2b81e36e61ffeac5c4aa3273e36" +checksum = "f2f357fcec90b3caef6623a099691be676d033b40a058ac95d2a6ade6fa0c943" dependencies = [ "proc-macro2", "quote", @@ -2931,9 +2921,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.8.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +checksum = "022db8904dfa342efe721985167e9fcd16c29b226db4397ed752a761cfce81e8" dependencies = [ "tinyvec_macros", ] @@ -3038,27 +3028,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" -[[package]] -name = "unicode-bidi" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893" - [[package]] name = "unicode-ident" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" -[[package]] -name = "unicode-normalization" -version = "0.1.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" -dependencies = [ - "tinyvec", -] - [[package]] name = "unicode-width" version = "0.1.14" @@ -3072,7 +3047,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" dependencies = [ "form_urlencoded", - "idna 1.0.3", + "idna", "percent-encoding", "serde", ] diff --git a/src/args.rs b/src/args.rs index a208919..b06b78e 100644 --- a/src/args.rs +++ b/src/args.rs @@ -29,7 +29,7 @@ pub struct Args { #[derive(Subcommand, Debug)] pub enum Subcommands { /// Initialize the source files to import the bookmarks. - Init, + Init(InitArgs), /// Configure the settings. Config(ConfigArgs), /// Synchronize source and target bookmarks. Fetch and cache websites for @@ -47,6 +47,14 @@ pub enum Subcommands { Remove(RemoveArgs), } +/// Describes the arguments for the `init` subcommand. +#[derive(ClapArgs, Debug)] +pub struct InitArgs { + /// Run command in dry mode. + #[arg(short = 'n', long = "dry-run")] + pub dry_run: bool, +} + /// Describes the arguments for the `config` subcommand. #[derive(ClapArgs, Debug)] pub struct ConfigArgs { diff --git a/src/cmd/configure.rs b/src/cmd/configure.rs index c780dec..31fae45 100644 --- a/src/cmd/configure.rs +++ b/src/cmd/configure.rs @@ -1,6 +1,6 @@ use crate::{ - bookmark_reader::SourceReader, bookmarks::RawSource, cmd::init_sources, config, json, - settings::SettingsArgs, utils, Config, ConfigArgs, Settings, + bookmark_reader::SourceReader, bookmarks::RawSource, json, settings::SettingsArgs, utils, + Config, ConfigArgs, Settings, }; use anyhow::{anyhow, Context}; use log::{debug, warn}; @@ -8,26 +8,13 @@ use std::{fs, io::Write}; /// Configure the source files to import the bookmarks, the cache mode, or the /// ignoure urls . -pub fn configure(config: Config, args: ConfigArgs) -> Result<(), anyhow::Error> { +pub fn configure(mut config: Config, args: ConfigArgs) -> Result<(), anyhow::Error> { debug!("{args:?}"); if args.dry_run { println!("Running in dry mode ...") } - let mut config = config; - let home_dir = dirs::home_dir().ok_or(anyhow!("Missing home dir"))?; - - if config.settings.sources.is_empty() { - if let Some(source_os) = utils::get_supported_os() { - init_sources(&mut config.settings, &home_dir, &source_os)?; - - if !args.dry_run { - utils::write_settings(&config.settings_path, &config.settings)?; - } - } - } - let source_path = args .set_source .source @@ -109,7 +96,7 @@ fn configure_settings( if settings_args.max_open_files.is_some() && settings_args.max_concurrent_requests.is_some() { #[cfg(not(any(target_os = "windows")))] - config::set_file_descriptor_limit( + crate::config::set_file_descriptor_limit( settings.max_open_files + settings.max_concurrent_requests as u64, )?; } diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 1ae9cbc..e6e2484 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,4 +1,5 @@ use crate::{ + args::InitArgs, bookmark_reader::{SourceOs, SourceReader}, bookmarks::RawSource, errors::BogrepError, @@ -12,13 +13,16 @@ use std::{ path::Path, }; -pub fn init(mut config: Config) -> Result<(), anyhow::Error> { +pub fn init(mut config: Config, args: InitArgs) -> Result<(), anyhow::Error> { let home_dir = dirs::home_dir().ok_or(anyhow!("Missing home dir"))?; if config.settings.sources.is_empty() { if let Some(source_os) = utils::get_supported_os() { init_sources(&mut config.settings, &home_dir, &source_os)?; - utils::write_settings(&config.settings_path, &config.settings)?; + + if !args.dry_run { + utils::write_settings(&config.settings_path, &config.settings)?; + } } } else { println!("Bookmark sources already configured"); @@ -35,6 +39,13 @@ pub fn init_sources( ) -> Result<(), anyhow::Error> { let sources = SourceReader::select_sources(home_dir, source_os)?; + if sources.is_empty() { + return Err(anyhow!( + "Found no sources in {}. Use `bogrep config --source` to configure custom sources.", + home_dir.display() + )); + } + println!("Found sources:"); for (index, source) in sources.iter().enumerate() { println!("{}: {}", index + 1, source.path.display()); diff --git a/src/main.rs b/src/main.rs index 909cb7e..4ab8956 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ async fn main() -> Result<(), anyhow::Error> { async fn run_app(args: Args, config: Config) -> Result<(), anyhow::Error> { if let Some(subcommands) = args.subcommands { match subcommands { - Subcommands::Init => cmd::init(config)?, + Subcommands::Init(args) => cmd::init(config, args)?, Subcommands::Config(args) => cmd::configure(config, args)?, Subcommands::Import(args) => cmd::import(config, args).await?, Subcommands::Sync(args) => cmd::sync(&config, &args).await?, diff --git a/tests/test_init.rs b/tests/test_init.rs new file mode 100644 index 0000000..f4f51b1 --- /dev/null +++ b/tests/test_init.rs @@ -0,0 +1,29 @@ +use assert_cmd::Command; +use predicates::str; +use std::{env, fs, path::Path}; +use tempfile::tempdir; + +#[cfg(target_os = "linux")] +#[test] +fn test_init() { + let temp_dir = tempdir().unwrap(); + let temp_path = temp_dir.path(); + assert!(temp_path.exists(), "Missing path: {}", temp_path.display()); + let home_path = temp_path.join("home"); + let source_file = Path::new("./test_data/bookmarks_chromium_no_extension"); + let bookmark_dir = home_path.join("snap/chromium/common/chromium/Default"); + let bookmark_file = bookmark_dir.join("Bookmarks"); + + fs::create_dir_all(&bookmark_dir).unwrap(); + fs::copy(source_file, bookmark_file).unwrap(); + + println!("Execute 'bogrep init'"); + let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + cmd.env("BOGREP_HOME", temp_path) + .env("HOME", home_path) + .args(["init"]) + .write_stdin("yes") + .assert() + .success() + .stdout(str::contains("Found sources:")); +}