Format codebase with rustfmt

This commit is contained in:
thePR0M3TH3AN
2025-05-21 11:24:49 -04:00
parent 9360efee2a
commit 567f1cd1a5
33 changed files with 780 additions and 481 deletions

View File

@@ -1,6 +1,9 @@
mod cli {
#[derive(Clone, Copy, Debug)]
pub enum Format { Text, Json }
pub enum Format {
Text,
Json,
}
}
#[path = "../src/cli/coll.rs"]
@@ -11,20 +14,41 @@ use libmarlin::db;
#[test]
fn coll_run_creates_and_adds() {
let mut conn = db::open(":memory:").unwrap();
conn.execute("INSERT INTO files(path,size,mtime) VALUES ('a.txt',0,0)", []).unwrap();
conn.execute("INSERT INTO files(path,size,mtime) VALUES ('b.txt',0,0)", []).unwrap();
conn.execute(
"INSERT INTO files(path,size,mtime) VALUES ('a.txt',0,0)",
[],
)
.unwrap();
conn.execute(
"INSERT INTO files(path,size,mtime) VALUES ('b.txt',0,0)",
[],
)
.unwrap();
let create = coll::CollCmd::Create(coll::CreateArgs{ name: "Set".into() });
let create = coll::CollCmd::Create(coll::CreateArgs { name: "Set".into() });
coll::run(&create, &mut conn, cli::Format::Text).unwrap();
let coll_id: i64 = conn.query_row("SELECT id FROM collections WHERE name='Set'", [], |r| r.get(0)).unwrap();
let coll_id: i64 = conn
.query_row("SELECT id FROM collections WHERE name='Set'", [], |r| {
r.get(0)
})
.unwrap();
let add = coll::CollCmd::Add(coll::AddArgs{ name: "Set".into(), file_pattern: "*.txt".into() });
let add = coll::CollCmd::Add(coll::AddArgs {
name: "Set".into(),
file_pattern: "*.txt".into(),
});
coll::run(&add, &mut conn, cli::Format::Text).unwrap();
let cnt: i64 = conn.query_row("SELECT COUNT(*) FROM collection_files WHERE collection_id=?1", [coll_id], |r| r.get(0)).unwrap();
let cnt: i64 = conn
.query_row(
"SELECT COUNT(*) FROM collection_files WHERE collection_id=?1",
[coll_id],
|r| r.get(0),
)
.unwrap();
assert_eq!(cnt, 2);
let list = coll::CollCmd::List(coll::ListArgs{ name: "Set".into() });
let list = coll::CollCmd::List(coll::ListArgs { name: "Set".into() });
coll::run(&list, &mut conn, cli::Format::Text).unwrap();
}

View File

@@ -1,6 +1,9 @@
mod cli {
#[derive(Clone, Copy, Debug)]
pub enum Format { Text, Json }
pub enum Format {
Text,
Json,
}
}
#[path = "../src/cli/link.rs"]
@@ -11,27 +14,43 @@ use libmarlin::db;
#[test]
fn link_run_add_and_rm() {
let mut conn = db::open(":memory:").unwrap();
conn.execute("INSERT INTO files(path,size,mtime) VALUES ('foo.txt',0,0)", []).unwrap();
conn.execute("INSERT INTO files(path,size,mtime) VALUES ('bar.txt',0,0)", []).unwrap();
conn.execute(
"INSERT INTO files(path,size,mtime) VALUES ('foo.txt',0,0)",
[],
)
.unwrap();
conn.execute(
"INSERT INTO files(path,size,mtime) VALUES ('bar.txt',0,0)",
[],
)
.unwrap();
let add = link::LinkCmd::Add(link::LinkArgs {
from: "foo.txt".into(),
to: "bar.txt".into(),
to: "bar.txt".into(),
r#type: None,
});
link::run(&add, &mut conn, cli::Format::Text).unwrap();
let count: i64 = conn.query_row("SELECT COUNT(*) FROM links", [], |r| r.get(0)).unwrap();
let count: i64 = conn
.query_row("SELECT COUNT(*) FROM links", [], |r| r.get(0))
.unwrap();
assert_eq!(count, 1);
let list = link::LinkCmd::List(link::ListArgs { pattern: "foo.txt".into(), direction: None, r#type: None });
let list = link::LinkCmd::List(link::ListArgs {
pattern: "foo.txt".into(),
direction: None,
r#type: None,
});
link::run(&list, &mut conn, cli::Format::Text).unwrap();
let rm = link::LinkCmd::Rm(link::LinkArgs {
from: "foo.txt".into(),
to: "bar.txt".into(),
to: "bar.txt".into(),
r#type: None,
});
link::run(&rm, &mut conn, cli::Format::Text).unwrap();
let remaining: i64 = conn.query_row("SELECT COUNT(*) FROM links", [], |r| r.get(0)).unwrap();
let remaining: i64 = conn
.query_row("SELECT COUNT(*) FROM links", [], |r| r.get(0))
.unwrap();
assert_eq!(remaining, 0);
}

View File

@@ -1,6 +1,9 @@
mod cli {
#[derive(Clone, Copy, Debug)]
pub enum Format { Text, Json }
pub enum Format {
Text,
Json,
}
}
#[path = "../src/cli/view.rs"]
@@ -11,17 +14,30 @@ use libmarlin::db;
#[test]
fn view_run_save_and_exec() {
let mut conn = db::open(":memory:").unwrap();
conn.execute("INSERT INTO files(path,size,mtime) VALUES ('TODO.txt',0,0)", []).unwrap();
conn.execute(
"INSERT INTO files(path,size,mtime) VALUES ('TODO.txt',0,0)",
[],
)
.unwrap();
let save = view::ViewCmd::Save(view::ArgsSave { view_name: "tasks".into(), query: "TODO".into() });
let save = view::ViewCmd::Save(view::ArgsSave {
view_name: "tasks".into(),
query: "TODO".into(),
});
view::run(&save, &mut conn, cli::Format::Text).unwrap();
let stored: String = conn.query_row("SELECT query FROM views WHERE name='tasks'", [], |r| r.get(0)).unwrap();
let stored: String = conn
.query_row("SELECT query FROM views WHERE name='tasks'", [], |r| {
r.get(0)
})
.unwrap();
assert_eq!(stored, "TODO");
let list = view::ViewCmd::List;
view::run(&list, &mut conn, cli::Format::Text).unwrap();
let exec = view::ViewCmd::Exec(view::ArgsExec { view_name: "tasks".into() });
let exec = view::ViewCmd::Exec(view::ArgsExec {
view_name: "tasks".into(),
});
view::run(&exec, &mut conn, cli::Format::Text).unwrap();
}

View File

@@ -25,8 +25,8 @@ fn spawn_demo_tree(root: &PathBuf) {
fs::write(root.join("Projects/Alpha/draft2.md"), "- [x] TODO foo\n").unwrap();
fs::write(root.join("Projects/Beta/final.md"), "done\n").unwrap();
fs::write(root.join("Projects/Gamma/TODO.txt"), "TODO bar\n").unwrap();
fs::write(root.join("Logs/app.log"), "ERROR omg\n").unwrap();
fs::write(root.join("Reports/Q1.pdf"), "PDF\n").unwrap();
fs::write(root.join("Logs/app.log"), "ERROR omg\n").unwrap();
fs::write(root.join("Reports/Q1.pdf"), "PDF\n").unwrap();
}
/// Shorthand for “run and must succeed”.
@@ -38,7 +38,7 @@ fn ok(cmd: &mut Command) -> assert_cmd::assert::Assert {
fn full_cli_flow() -> Result<(), Box<dyn std::error::Error>> {
/* ── 1 ░ sandbox ───────────────────────────────────────────── */
let tmp = tempdir()?; // wiped on drop
let tmp = tempdir()?; // wiped on drop
let demo_dir = tmp.path().join("marlin_demo");
spawn_demo_tree(&demo_dir);
@@ -53,9 +53,7 @@ fn full_cli_flow() -> Result<(), Box<dyn std::error::Error>> {
/* ── 2 ░ init ( auto-scan cwd ) ───────────────────────────── */
ok(marlin()
.current_dir(&demo_dir)
.arg("init"));
ok(marlin().current_dir(&demo_dir).arg("init"));
/* ── 3 ░ tag & attr demos ─────────────────────────────────── */
@@ -74,12 +72,14 @@ fn full_cli_flow() -> Result<(), Box<dyn std::error::Error>> {
/* ── 4 ░ quick search sanity checks ───────────────────────── */
marlin()
.arg("search").arg("TODO")
.arg("search")
.arg("TODO")
.assert()
.stdout(predicate::str::contains("TODO.txt"));
marlin()
.arg("search").arg("attr:reviewed=yes")
.arg("search")
.arg("attr:reviewed=yes")
.assert()
.stdout(predicate::str::contains("Q1.pdf"));
@@ -92,31 +92,29 @@ fn full_cli_flow() -> Result<(), Box<dyn std::error::Error>> {
ok(marlin().arg("scan").arg(&demo_dir));
ok(marlin()
.arg("link").arg("add")
.arg(&foo).arg(&bar));
ok(marlin().arg("link").arg("add").arg(&foo).arg(&bar));
marlin()
.arg("link").arg("backlinks").arg(&bar)
.arg("link")
.arg("backlinks")
.arg(&bar)
.assert()
.stdout(predicate::str::contains("foo.txt"));
/* ── 6 ░ backup → delete DB → restore ────────────────────── */
let backup_path = String::from_utf8(
marlin().arg("backup").output()?.stdout
)?;
let backup_path = String::from_utf8(marlin().arg("backup").output()?.stdout)?;
let backup_file = backup_path.split_whitespace().last().unwrap();
fs::remove_file(&db_path)?; // simulate corruption
ok(marlin().arg("restore").arg(backup_file)); // restore
fs::remove_file(&db_path)?; // simulate corruption
ok(marlin().arg("restore").arg(backup_file)); // restore
// Search must still work afterwards
marlin()
.arg("search").arg("TODO")
.arg("search")
.arg("TODO")
.assert()
.stdout(predicate::str::contains("TODO.txt"));
Ok(())
}

View File

@@ -13,7 +13,11 @@ use util::marlin;
fn link_non_indexed_should_fail() {
let tmp = tempdir().unwrap();
marlin(&tmp).current_dir(tmp.path()).arg("init").assert().success();
marlin(&tmp)
.current_dir(tmp.path())
.arg("init")
.assert()
.success();
std::fs::write(tmp.path().join("foo.txt"), "").unwrap();
std::fs::write(tmp.path().join("bar.txt"), "").unwrap();
@@ -21,9 +25,10 @@ fn link_non_indexed_should_fail() {
marlin(&tmp)
.current_dir(tmp.path())
.args([
"link", "add",
"link",
"add",
&tmp.path().join("foo.txt").to_string_lossy(),
&tmp.path().join("bar.txt").to_string_lossy()
&tmp.path().join("bar.txt").to_string_lossy(),
])
.assert()
.failure()
@@ -35,16 +40,19 @@ fn link_non_indexed_should_fail() {
#[test]
fn attr_set_on_non_indexed_file_should_warn() {
let tmp = tempdir().unwrap();
marlin(&tmp).current_dir(tmp.path()).arg("init").assert().success();
marlin(&tmp)
.current_dir(tmp.path())
.arg("init")
.assert()
.success();
let ghost = tmp.path().join("ghost.txt");
std::fs::write(&ghost, "").unwrap();
marlin(&tmp)
.args(["attr","set",
&ghost.to_string_lossy(),"foo","bar"])
.args(["attr", "set", &ghost.to_string_lossy(), "foo", "bar"])
.assert()
.success() // exits 0
.success() // exits 0
.stderr(str::contains("not indexed"));
}
@@ -52,14 +60,18 @@ fn attr_set_on_non_indexed_file_should_warn() {
#[test]
fn coll_add_unknown_collection_should_fail() {
let tmp = tempdir().unwrap();
let tmp = tempdir().unwrap();
let file = tmp.path().join("doc.txt");
std::fs::write(&file, "").unwrap();
marlin(&tmp).current_dir(tmp.path()).arg("init").assert().success();
marlin(&tmp)
.current_dir(tmp.path())
.arg("init")
.assert()
.success();
marlin(&tmp)
.args(["coll","add","nope",&file.to_string_lossy()])
.args(["coll", "add", "nope", &file.to_string_lossy()])
.assert()
.failure();
}
@@ -68,7 +80,7 @@ fn coll_add_unknown_collection_should_fail() {
#[test]
fn restore_with_nonexistent_backup_should_fail() {
let tmp = tempdir().unwrap();
let tmp = tempdir().unwrap();
// create an empty DB first
marlin(&tmp).arg("init").assert().success();
@@ -79,4 +91,3 @@ fn restore_with_nonexistent_backup_should_fail() {
.failure()
.stderr(str::contains("Failed to restore"));
}

View File

@@ -5,7 +5,7 @@
mod util;
use util::marlin;
use predicates::{prelude::*, str}; // brings `PredicateBooleanExt::and`
use predicates::{prelude::*, str}; // brings `PredicateBooleanExt::and`
use std::fs;
use tempfile::tempdir;
@@ -13,15 +13,20 @@ use tempfile::tempdir;
#[test]
fn tag_should_add_hierarchical_tag_and_search_finds_it() {
let tmp = tempdir().unwrap();
let tmp = tempdir().unwrap();
let file = tmp.path().join("foo.md");
fs::write(&file, "# test\n").unwrap();
marlin(&tmp).current_dir(tmp.path()).arg("init").assert().success();
marlin(&tmp)
.current_dir(tmp.path())
.arg("init")
.assert()
.success();
marlin(&tmp)
.args(["tag", file.to_str().unwrap(), "project/md"])
.assert().success();
.assert()
.success();
marlin(&tmp)
.args(["search", "tag:project/md"])
@@ -34,15 +39,20 @@ fn tag_should_add_hierarchical_tag_and_search_finds_it() {
#[test]
fn attr_set_then_ls_roundtrip() {
let tmp = tempdir().unwrap();
let tmp = tempdir().unwrap();
let file = tmp.path().join("report.pdf");
fs::write(&file, "%PDF-1.4\n").unwrap();
marlin(&tmp).current_dir(tmp.path()).arg("init").assert().success();
marlin(&tmp)
.current_dir(tmp.path())
.arg("init")
.assert()
.success();
marlin(&tmp)
.args(["attr", "set", file.to_str().unwrap(), "reviewed", "yes"])
.assert().success();
.assert()
.success();
marlin(&tmp)
.args(["attr", "ls", file.to_str().unwrap()])
@@ -62,11 +72,21 @@ fn coll_create_add_and_list() {
fs::write(&a, "").unwrap();
fs::write(&b, "").unwrap();
marlin(&tmp).current_dir(tmp.path()).arg("init").assert().success();
marlin(&tmp)
.current_dir(tmp.path())
.arg("init")
.assert()
.success();
marlin(&tmp).args(["coll", "create", "Set"]).assert().success();
marlin(&tmp)
.args(["coll", "create", "Set"])
.assert()
.success();
for f in [&a, &b] {
marlin(&tmp).args(["coll", "add", "Set", f.to_str().unwrap()]).assert().success();
marlin(&tmp)
.args(["coll", "add", "Set", f.to_str().unwrap()])
.assert()
.success();
}
marlin(&tmp)
@@ -80,15 +100,22 @@ fn coll_create_add_and_list() {
#[test]
fn view_save_list_and_exec() {
let tmp = tempdir().unwrap();
let tmp = tempdir().unwrap();
let todo = tmp.path().join("TODO.txt");
fs::write(&todo, "remember the milk\n").unwrap();
marlin(&tmp).current_dir(tmp.path()).arg("init").assert().success();
marlin(&tmp)
.current_dir(tmp.path())
.arg("init")
.assert()
.success();
// save & list
marlin(&tmp).args(["view", "save", "tasks", "milk"]).assert().success();
marlin(&tmp)
.args(["view", "save", "tasks", "milk"])
.assert()
.success();
marlin(&tmp)
.args(["view", "list"])
.assert()
@@ -118,24 +145,30 @@ fn link_add_rm_and_list() {
let mc = || marlin(&tmp);
mc().current_dir(tmp.path()).arg("init").assert().success();
mc().args(["scan", tmp.path().to_str().unwrap()]).assert().success();
mc().args(["scan", tmp.path().to_str().unwrap()])
.assert()
.success();
// add
mc().args(["link", "add", foo.to_str().unwrap(), bar.to_str().unwrap()])
.assert().success();
.assert()
.success();
// list (outgoing default)
mc().args(["link", "list", foo.to_str().unwrap()])
.assert().success()
.assert()
.success()
.stdout(str::contains("foo.txt").and(str::contains("bar.txt")));
// remove
mc().args(["link", "rm", foo.to_str().unwrap(), bar.to_str().unwrap()])
.assert().success();
.assert()
.success();
// list now empty
mc().args(["link", "list", foo.to_str().unwrap()])
.assert().success()
.assert()
.success()
.stdout(str::is_empty());
}
@@ -154,19 +187,24 @@ fn scan_with_multiple_paths_indexes_all() {
fs::write(&f1, "").unwrap();
fs::write(&f2, "").unwrap();
marlin(&tmp).current_dir(tmp.path()).arg("init").assert().success();
marlin(&tmp)
.current_dir(tmp.path())
.arg("init")
.assert()
.success();
// multi-path scan
marlin(&tmp)
.args(["scan", dir_a.to_str().unwrap(), dir_b.to_str().unwrap()])
.assert().success();
.assert()
.success();
// both files findable
for term in ["one.txt", "two.txt"] {
marlin(&tmp).args(["search", term])
marlin(&tmp)
.args(["search", term])
.assert()
.success()
.stdout(str::contains(term));
}
}

View File

@@ -1,9 +1,9 @@
//! tests/util.rs
//! Small helpers shared across integration tests.
use assert_cmd::Command;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
use assert_cmd::Command;
/// Absolute path to the freshly-built `marlin` binary.
pub fn bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_marlin"))

View File

@@ -2,11 +2,11 @@ use std::thread;
use std::time::Duration;
use tempfile::tempdir;
use marlin_cli::cli::{watch, Format};
use marlin_cli::cli::watch::WatchCmd;
use libc;
use libmarlin::watcher::WatcherState;
use libmarlin::{self as marlin, db};
use libc;
use marlin_cli::cli::watch::WatchCmd;
use marlin_cli::cli::{watch, Format};
#[test]
fn watch_start_and_stop_quickly() {
@@ -20,7 +20,10 @@ fn watch_start_and_stop_quickly() {
let mut conn = db::open(&db_path).unwrap();
let path = tmp.path().to_path_buf();
let cmd = WatchCmd::Start { path: path.clone(), debounce_ms: 50 };
let cmd = WatchCmd::Start {
path: path.clone(),
debounce_ms: 50,
};
// send SIGINT shortly after watcher starts
let t = thread::spawn(|| {