Skip to content

Commit 6f8f53e

Browse files
committed
address review concerns about bad pantry paths.
1 parent cf08f29 commit 6f8f53e

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cli/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ console = { version = "0.16", default-features = false, features = [
2020
"ansi-parsing",
2121
] }
2222

23+
[dev-dependencies]
24+
tempfile = "3"
25+
2326
[target.'cfg(not(target_os = "macos"))'.dependencies]
2427
rusqlite = { workspace = true, features = ["bundled"] }
2528
native-tls = { version = "0.2", features = ["vendored"] }

crates/cli/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
mod main;
2+
mod sync;

crates/cli/src/tests/sync.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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!(result.is_ok(), "update should succeed when projects/ exists");
35+
}
36+
37+
#[tokio::test]
38+
async fn test_update_with_pantry_dir_errors_when_projects_missing() {
39+
let _lock = ENV_MUTEX.lock().unwrap();
40+
let tmp = TempDir::new().unwrap();
41+
let pantry_dir = tmp.path().join("pantry");
42+
std::fs::create_dir_all(&pantry_dir).unwrap();
43+
let db_file = tmp.path().join("pantry.2.db");
44+
45+
let config = test_config(&pantry_dir, &db_file);
46+
let mut conn = Connection::open(&db_file).unwrap();
47+
48+
std::env::set_var("PKGX_PANTRY_DIR", &pantry_dir);
49+
let result = sync::update(&config, &mut conn).await;
50+
std::env::remove_var("PKGX_PANTRY_DIR");
51+
52+
assert!(result.is_err(), "update should fail when projects/ is missing");
53+
let err = result.unwrap_err().to_string();
54+
assert!(
55+
err.contains("missing projects/"),
56+
"error should mention missing projects/, got: {err}"
57+
);
58+
}

crates/lib/src/sync.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ pub async fn ensure(config: &Config, conn: &mut Connection) -> Result<(), Box<dy
3333

3434
pub async fn update(config: &Config, conn: &mut Connection) -> Result<(), Box<dyn Error>> {
3535
if std::env::var("PKGX_PANTRY_DIR").is_ok() {
36-
return ensure(config, conn).await;
36+
if config.pantry_dir.join("projects").is_dir() {
37+
let lockfile = lock(config)?;
38+
pantry_db::cache(config, conn)?;
39+
FileExt::unlock(&lockfile)?;
40+
return Ok(());
41+
} else {
42+
return Err("PKGX_PANTRY_DIR is set but does not contain a pantry (missing projects/)")?;
43+
}
3744
}
3845
replace(config, conn).await
3946
}

0 commit comments

Comments
 (0)