Skip to content

Commit 1745529

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

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

crates/lib/src/sync.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ 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(
43+
"PKGX_PANTRY_DIR is set but does not contain a pantry (missing projects/)",
44+
)?;
45+
}
3746
}
3847
replace(config, conn).await
3948
}

0 commit comments

Comments
 (0)