Skip to content

Commit aa2266b

Browse files
committed
Fixes.
1 parent 0c9f59a commit aa2266b

3 files changed

Lines changed: 25 additions & 22 deletions

File tree

crates/cache-storage/src/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ mod tests {
256256
let items: Vec<usize> = (0..120).collect();
257257
let batches = chunk_into_batches(items, |n| *n);
258258

259-
assert_eq!(item_counts(&batches), vec![40, 40, 40]);
259+
assert_eq!(item_counts(&batches), vec![60, 60]);
260260
}
261261

262262
#[test]

crates/cas/src/cas.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ impl CasStore {
359359
fn commit_temp_file(&self, hash: &ContentHash, guard: &mut TempGuard) -> miette::Result<()> {
360360
let dest = self.object_path(hash);
361361

362+
if let Some(shard) = dest.parent() {
363+
std::fs::create_dir_all(shard).into_diagnostic()?;
364+
}
365+
362366
std::fs::rename(&guard.path, &dest).into_diagnostic()?;
363367

364368
guard.committed = true;

crates/cas/tests/cas_test.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -349,18 +349,20 @@ mod cas {
349349
}
350350

351351
#[test]
352-
fn returns_false_when_already_present() {
352+
fn writes_unconditionally_when_present() {
353353
let sandbox = create_empty_sandbox();
354354
let store = create_store(&sandbox);
355355

356356
let data = b"already there";
357357
let hash = ContentHash::hash_bytes(data).unwrap();
358358

359-
// First write commits to disk.
359+
// `write` is an unconditional primitive — the existence check lives
360+
// in the `store_*` callers — so it commits and reports true every
361+
// time, leaving no temp behind.
360362
assert!(store.write(&hash, data).unwrap());
361-
362-
// Second write short-circuits — the existence check makes it a no-op.
363-
assert!(!store.write(&hash, data).unwrap());
363+
assert!(store.write(&hash, data).unwrap());
364+
assert!(store.contains_object(&hash));
365+
assert_eq!(count_temp_files(&store), 0);
364366
}
365367
}
366368

@@ -384,7 +386,7 @@ mod cas {
384386
}
385387

386388
#[test]
387-
fn returns_false_when_already_present() {
389+
fn writes_unconditionally_when_present() {
388390
let sandbox = create_empty_sandbox();
389391
let store = create_store(&sandbox);
390392

@@ -393,17 +395,18 @@ mod cas {
393395
std::fs::write(&source, data).unwrap();
394396
let hash = ContentHash::hash_bytes(data).unwrap();
395397

398+
// Unconditional primitive: `store_file` does the existence check, so
399+
// write_file itself reflinks and commits on every call.
396400
assert!(store.write_file(&hash, &source).unwrap());
397-
398-
// Second call short-circuits on the existence check; the source
399-
// is never touched again.
400-
assert!(!store.write_file(&hash, &source).unwrap());
401+
assert!(store.write_file(&hash, &source).unwrap());
402+
assert_eq!(store.read(&hash).unwrap(), data);
401403
}
402404

403405
#[test]
404-
fn warm_cache_creates_no_temp_file() {
405-
// Like `store_file`, an object already in the store must short
406-
// circuit before any temp file is staged.
406+
fn leaves_no_temp_file_behind() {
407+
// The warm-cache short-circuit now lives in `store_file`; the
408+
// write_file primitive always stages a temp but renames it into the
409+
// store, never leaving one behind.
407410
let sandbox = create_empty_sandbox();
408411
let store = create_store(&sandbox);
409412

@@ -412,15 +415,11 @@ mod cas {
412415
std::fs::write(&source, data).unwrap();
413416
let hash = ContentHash::hash_bytes(data).unwrap();
414417

415-
assert!(store.write_file(&hash, &source).unwrap());
416-
417-
std::fs::remove_dir_all(&store.temp_dir).unwrap();
418+
store.write_file(&hash, &source).unwrap();
419+
store.write_file(&hash, &source).unwrap();
418420

419-
assert!(!store.write_file(&hash, &source).unwrap());
420-
assert!(
421-
!store.temp_dir.exists(),
422-
"warm-cache write_file must not create a temp file"
423-
);
421+
assert!(store.contains_object(&hash));
422+
assert_eq!(count_temp_files(&store), 0);
424423
}
425424
}
426425

0 commit comments

Comments
 (0)