From ab72fdfcb12593ae689c8c97ada1d48e72f2c831 Mon Sep 17 00:00:00 2001 From: soar0216 Date: Sat, 25 Apr 2026 09:42:12 +0800 Subject: [PATCH] =?UTF-8?q?test(export):=20=E6=B7=BB=E5=8A=A0=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E5=8A=9F=E8=83=BD=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加对zoxide导出功能的测试,包括JSON和CSV格式的导出验证 重构测试代码,提取公共测试数据和辅助函数 --- src/cmd/export.rs | 50 +++++++++++++++++++++------- tests/export.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 tests/export.rs diff --git a/src/cmd/export.rs b/src/cmd/export.rs index 26851a3..fdb8084 100644 --- a/src/cmd/export.rs +++ b/src/cmd/export.rs @@ -53,14 +53,16 @@ mod tests { use super::*; use crate::db::Dir; + const TEST_ENTRIES: [(&str, f64, u64); 3] = [ + ("/home/alice/projects/zoxide", 42.5, 1714000000), + ("/home/alice/downloads", 7.0, 1713000000), + (r#"/tmp"quotes,commas""#, 1.0, 1712000000), + ]; + fn create_test_db() -> tempfile::TempDir { let data_dir = tempfile::tempdir().unwrap(); let mut db = Database::open_dir(data_dir.path()).unwrap(); - for (path, rank, last_accessed) in [ - ("/home/alice/projects/zoxide", 42.5, 1714000000), - ("/home/alice/downloads", 7.0, 1713000000), - (r#"/tmp"quotes,commas""#, 1.0, 1712000000), - ] { + for (path, rank, last_accessed) in TEST_ENTRIES { db.add_unchecked(path, rank, last_accessed); } db.save().unwrap(); @@ -73,6 +75,14 @@ mod tests { } } + fn find_entry_by_path<'a>(entries: &'a [Dir], path: &str) -> &'a Dir<'a> { + entries.iter().find(|d| d.path == path).unwrap() + } + + fn find_record_by_path<'a>(records: &'a [csv::StringRecord], path: &str) -> &'a csv::StringRecord { + records.iter().find(|r| r.get(0).unwrap() == path).unwrap() + } + #[test] fn export_json() { let data_dir = create_test_db(); @@ -89,9 +99,18 @@ mod tests { let result: Vec = serde_json::from_str(&content).unwrap(); assert_eq!(result.len(), 3); - assert!(result.iter().any(|d| d.path == "/home/alice/projects/zoxide")); - assert!(result.iter().any(|d| d.path == "/home/alice/downloads")); - assert!(result.iter().any(|d| d.path == r#"/tmp"quotes,commas""#)); + + let entry1 = find_entry_by_path(&result, "/home/alice/projects/zoxide"); + assert!((entry1.rank - 42.5).abs() < 0.001); + assert_eq!(entry1.last_accessed, 1714000000); + + let entry2 = find_entry_by_path(&result, "/home/alice/downloads"); + assert!((entry2.rank - 7.0).abs() < 0.001); + assert_eq!(entry2.last_accessed, 1713000000); + + let entry3 = find_entry_by_path(&result, r#"/tmp"quotes,commas""#); + assert!((entry3.rank - 1.0).abs() < 0.001); + assert_eq!(entry3.last_accessed, 1712000000); } #[test] @@ -115,10 +134,17 @@ mod tests { let records: Vec = rdr.records().map(|r| r.unwrap()).collect(); assert_eq!(records.len(), 3); - let paths: Vec<&str> = records.iter().map(|r| r.get(0).unwrap()).collect(); - assert!(paths.contains(&"/home/alice/projects/zoxide")); - assert!(paths.contains(&"/home/alice/downloads")); - assert!(paths.contains(&r#"/tmp"quotes,commas""#)); + let record1 = find_record_by_path(&records, "/home/alice/projects/zoxide"); + assert_eq!(record1.get(1).unwrap().parse::().unwrap(), 42.5); + assert_eq!(record1.get(2).unwrap().parse::().unwrap(), 1714000000); + + let record2 = find_record_by_path(&records, "/home/alice/downloads"); + assert_eq!(record2.get(1).unwrap().parse::().unwrap(), 7.0); + assert_eq!(record2.get(2).unwrap().parse::().unwrap(), 1713000000); + + let record3 = find_record_by_path(&records, r#"/tmp"quotes,commas""#); + assert_eq!(record3.get(1).unwrap().parse::().unwrap(), 1.0); + assert_eq!(record3.get(2).unwrap().parse::().unwrap(), 1712000000); } #[test] diff --git a/tests/export.rs b/tests/export.rs new file mode 100644 index 0000000..b4be4c3 --- /dev/null +++ b/tests/export.rs @@ -0,0 +1,85 @@ +use std::fs; + +use assert_cmd::Command; +use tempfile::TempDir; + +fn zoxide() -> Command { + Command::cargo_bin("zoxide").unwrap() +} + +#[test] +fn export_missing_format() { + let out_dir = TempDir::new().unwrap(); + let out_file = out_dir.path().join("export.json"); + + zoxide() + .args(["export", "--out", out_file.to_str().unwrap()]) + .assert() + .failure(); +} + +#[test] +fn export_invalid_format() { + let out_dir = TempDir::new().unwrap(); + let out_file = out_dir.path().join("export.json"); + + zoxide() + .args(["export", "--format", "xml", "--out", out_file.to_str().unwrap()]) + .assert() + .failure(); +} + +#[test] +fn export_missing_out() { + zoxide() + .args(["export", "--format", "json"]) + .assert() + .failure(); +} + +#[test] +fn export_valid_json() { + let data_dir = TempDir::new().unwrap(); + let test_dir = TempDir::new().unwrap(); + let out_dir = TempDir::new().unwrap(); + let out_file = out_dir.path().join("export.json"); + + zoxide() + .env("_ZO_DATA_DIR", data_dir.path()) + .args(["add", test_dir.path().to_str().unwrap()]) + .assert() + .success(); + + zoxide() + .env("_ZO_DATA_DIR", data_dir.path()) + .args(["export", "--format", "json", "--out", out_file.to_str().unwrap()]) + .assert() + .success(); + + assert!(out_file.exists()); +} + +#[test] +fn export_valid_csv() { + let data_dir = TempDir::new().unwrap(); + let test_dir = TempDir::new().unwrap(); + let out_dir = TempDir::new().unwrap(); + let out_file = out_dir.path().join("export.csv"); + + zoxide() + .env("_ZO_DATA_DIR", data_dir.path()) + .args(["add", test_dir.path().to_str().unwrap()]) + .assert() + .success(); + + zoxide() + .env("_ZO_DATA_DIR", data_dir.path()) + .args(["export", "--format", "csv", "--out", out_file.to_str().unwrap()]) + .assert() + .success(); + + assert!(out_file.exists()); + + let content = fs::read_to_string(&out_file).unwrap(); + assert!(content.contains("path,rank,last_accessed")); +}