|
| 1 | +use libpkgx::config::Config; |
| 2 | +use libpkgx::sync; |
| 3 | +use rusqlite::Connection; |
| 4 | +use std::sync::Mutex; |
| 5 | +use tempfile::TempDir; |
| 6 | + |
| 7 | +// serialize tests that mutate PKGX_PANTRY_DIR |
| 8 | +static ENV_MUTEX: Mutex<()> = Mutex::new(()); |
| 9 | + |
| 10 | +fn test_config(pantry_dir: &std::path::Path, db_file: &std::path::Path) -> Config { |
| 11 | + Config { |
| 12 | + pantry_dir: pantry_dir.to_path_buf(), |
| 13 | + pantry_db_file: db_file.to_path_buf(), |
| 14 | + dist_url: "http://localhost:0".to_string(), |
| 15 | + pkgx_dir: pantry_dir.to_path_buf(), |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +#[tokio::test] |
| 20 | +async fn test_update_with_pantry_dir_rebuilds_db_when_projects_exists() { |
| 21 | + let _lock = ENV_MUTEX.lock().unwrap(); |
| 22 | + let tmp = TempDir::new().unwrap(); |
| 23 | + let pantry_dir = tmp.path().join("pantry"); |
| 24 | + std::fs::create_dir_all(pantry_dir.join("projects")).unwrap(); |
| 25 | + let db_file = tmp.path().join("pantry.2.db"); |
| 26 | + |
| 27 | + let config = test_config(&pantry_dir, &db_file); |
| 28 | + let mut conn = Connection::open(&db_file).unwrap(); |
| 29 | + |
| 30 | + std::env::set_var("PKGX_PANTRY_DIR", &pantry_dir); |
| 31 | + let result = sync::update(&config, &mut conn).await; |
| 32 | + std::env::remove_var("PKGX_PANTRY_DIR"); |
| 33 | + |
| 34 | + assert!( |
| 35 | + result.is_ok(), |
| 36 | + "update should succeed when projects/ exists" |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +#[tokio::test] |
| 41 | +async fn test_update_with_pantry_dir_errors_when_projects_missing() { |
| 42 | + let _lock = ENV_MUTEX.lock().unwrap(); |
| 43 | + let tmp = TempDir::new().unwrap(); |
| 44 | + let pantry_dir = tmp.path().join("pantry"); |
| 45 | + std::fs::create_dir_all(&pantry_dir).unwrap(); |
| 46 | + let db_file = tmp.path().join("pantry.2.db"); |
| 47 | + |
| 48 | + let config = test_config(&pantry_dir, &db_file); |
| 49 | + let mut conn = Connection::open(&db_file).unwrap(); |
| 50 | + |
| 51 | + std::env::set_var("PKGX_PANTRY_DIR", &pantry_dir); |
| 52 | + let result = sync::update(&config, &mut conn).await; |
| 53 | + std::env::remove_var("PKGX_PANTRY_DIR"); |
| 54 | + |
| 55 | + assert!( |
| 56 | + result.is_err(), |
| 57 | + "update should fail when projects/ is missing" |
| 58 | + ); |
| 59 | + let err = result.unwrap_err().to_string(); |
| 60 | + assert!( |
| 61 | + err.contains("missing projects/"), |
| 62 | + "error should mention missing projects/, got: {err}" |
| 63 | + ); |
| 64 | +} |
0 commit comments