Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
quambene committed Jan 29, 2024
1 parent 4ca8bf0 commit 9b6728e
Showing 1 changed file with 63 additions and 19 deletions.
82 changes: 63 additions & 19 deletions tests/test_configure.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
use std::path::Path;

use assert_cmd::Command;
use bogrep::{json, utils, JsonBookmarks, Settings};
use tempfile::tempdir;

fn test_configure_source(source: &str) {
let temp_dir = tempdir().unwrap();
let temp_path = temp_dir.path();
assert!(temp_path.exists(), "Missing path: {}", temp_path.display());

println!("Execute 'bogrep config --source {source}'");
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
cmd.env("BOGREP_HOME", temp_path);
cmd.args(["config", "--source", source]);
let res = cmd.output();
assert!(res.is_ok(), "Can't execute command: {}", res.unwrap_err());

let settings_path = temp_dir.path().join("settings.json");
fn test_configure_source(temp_path: &Path, source: &str, folder: Option<&str>) {
if let Some(folder) = folder {
println!("Execute 'bogrep config --source {source} --folders {folder}'");
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
cmd.env("BOGREP_HOME", temp_path);
cmd.args(["config", "--source", source, "--folders", folder]);
let res = cmd.output();
assert!(res.is_ok(), "Can't execute command: {}", res.unwrap_err());
} else {
println!("Execute 'bogrep config --source {source}'");
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
cmd.env("BOGREP_HOME", temp_path);
cmd.args(["config", "--source", source]);
let res = cmd.output();
assert!(res.is_ok(), "Can't execute command: {}", res.unwrap_err());
}

let settings_path = temp_path.join("settings.json");
assert!(
settings_path.exists(),
"Missing path: {}",
settings_path.display()
);

let bookmarks_path = temp_dir.path().join("bookmarks.json");
let bookmarks_path = temp_path.join("bookmarks.json");
assert!(
bookmarks_path.exists(),
"Missing path: {}",
bookmarks_path.display()
);

let cache_path = temp_dir.path().join("cache");
let cache_path = temp_path.join("cache");
assert!(
cache_path.exists(),
"Missing path: {}",
Expand All @@ -40,8 +47,15 @@ fn test_configure_source(source: &str) {
assert!(res.is_ok());

let settings = res.unwrap();
dbg!(&settings);
assert!(!settings.sources.is_empty());

if folder.is_some() {
for source in settings.sources {
assert!(!source.folders.is_empty());
}
}

let bookmarks = utils::read_file(&bookmarks_path).unwrap();
let res = json::deserialize::<JsonBookmarks>(&bookmarks);
assert!(res.is_ok());
Expand All @@ -52,26 +66,56 @@ fn test_configure_source(source: &str) {

#[test]
fn test_configure_source_simple() {
let temp_dir = tempdir().unwrap();
let temp_path = temp_dir.path();
assert!(temp_path.exists(), "Missing path: {}", temp_path.display());

let source = "./test_data/bookmarks_simple.txt";
test_configure_source(source);
test_configure_source(temp_path, source, None);
}

#[test]
fn test_configure_source_firefox() {
let temp_dir = tempdir().unwrap();
let temp_path = temp_dir.path();
assert!(temp_path.exists(), "Missing path: {}", temp_path.display());

let source = "./test_data/bookmarks_firefox.json";
test_configure_source(source);
test_configure_source(temp_path, source, None);
}

#[test]
fn test_configure_source_chrome() {
let temp_dir = tempdir().unwrap();
let temp_path = temp_dir.path();
assert!(temp_path.exists(), "Missing path: {}", temp_path.display());

let source = "./test_data/bookmarks_chromium.json";
test_configure_source(source);
test_configure_source(temp_path, source, None);
}

#[test]
fn test_configure_source_chrome_no_extension() {
let temp_dir = tempdir().unwrap();
let temp_path = temp_dir.path();
assert!(temp_path.exists(), "Missing path: {}", temp_path.display());

let source = "./test_data/bookmarks_chromium_no_extension";
test_configure_source(source);
test_configure_source(temp_path, source, None);
}

#[test]
fn test_configure_source_and_folders_firefox() {
let temp_dir = tempdir().unwrap();
let temp_path = temp_dir.path();
assert!(temp_path.exists(), "Missing path: {}", temp_path.display());

let source = "./test_data/bookmarks_firefox.json";
let folder = Some("dev");
test_configure_source(temp_path, source, folder);

let folder = Some("science");
test_configure_source(temp_path, source, folder);
}

#[test]
Expand Down

0 comments on commit 9b6728e

Please sign in to comment.