Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

refactor(app): the snapshot swap hands the previous folder back (#1385)

吴佳浩Alben5e58fe260310
diff --git a/src/app.rs b/src/app.rs
index 2ef09e33..5879518d 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -2534,9 +2534,9 @@ impl PendingSnapshot {
self.dir.as_deref()
}
- fn commit(mut self) -> Result<()> {
+ fn commit(mut self) -> Result<Option<PathBuf>> {
let Some(dir) = self.dir.take() else {
- return Ok(());
+ return Ok(None);
};
let mut name = self.dest.file_name().unwrap_or_default().to_os_string();
name.push(".old");
@@ -2549,12 +2549,13 @@ impl PendingSnapshot {
/// Puts `staged` where `dest` is, keeping whatever was there under `old` until the swap succeeds.
/// A failed swap puts the previous folder back; if that fails too, both paths are named, so `dest`
/// is never silently left missing (issue #1118).
-fn swap_in(staged: &Path, dest: &Path, old: &Path) -> Result<()> {
+fn swap_in(staged: &Path, dest: &Path, old: &Path) -> Result<Option<PathBuf>> {
let moved = std::fs::rename(dest, old).is_ok();
let renamed = std::fs::rename(staged, dest);
if renamed.is_ok() {
- let _ = std::fs::remove_dir_all(old);
- return Ok(());
+ // The previous folder stays until the caller says the rest of the publish worked, so a
+ // failure after this point can still put it back (issue #1118).
+ return Ok(moved.then(|| old.to_path_buf()));
}
let renamed = renamed.with_context(|| format!("replacing {}", dest.display()));
if moved && let Err(restore) = std::fs::rename(old, dest) {
@@ -2573,7 +2574,7 @@ fn swap_in(staged: &Path, dest: &Path, old: &Path) -> Result<()> {
);
}
let _ = std::fs::remove_dir_all(staged);
- renamed
+ renamed.map(|()| None)
}
impl Drop for PendingSnapshot {
@@ -2830,16 +2831,23 @@ pub fn record(opts: &RecordOptions) -> Result<RecordOutcome> {
outcome.trycmd_test = save_config(&config)?;
// Same as the CLI path: a failure says what was already published (issue #1118).
let staged_snapshot = snapshot.path().is_some();
- snapshot.commit().with_context(|| {
+ let previous_snapshot = snapshot.commit().with_context(|| {
format!("{CONFIG_FILE} is published, but the captured-file snapshot is not")
})?;
- check_recording_complete(dropped, outcome.trycmd_test.as_deref()).with_context(|| {
- if staged_snapshot {
- "the snapshot is published, but the recording is incomplete".to_string()
- } else {
- format!("{CONFIG_FILE} is published, but the recording is incomplete")
- }
- })?;
+ // The swap hands the previous snapshot back, so this branch drops it once the recording is
+ // known to be complete (review of #1385).
+ let complete = check_recording_complete(dropped, outcome.trycmd_test.as_deref())
+ .with_context(|| {
+ if staged_snapshot {
+ "the snapshot is published, but the recording is incomplete".to_string()
+ } else {
+ format!("{CONFIG_FILE} is published, but the recording is incomplete")
+ }
+ });
+ if let Some(previous) = previous_snapshot {
+ let _ = std::fs::remove_dir_all(previous);
+ }
+ complete?;
if config.http.as_ref().is_some_and(|h| h.fresh.is_empty()) {
let _ = std::fs::remove_dir_all(
cases_dir(&repo)
@@ -3077,7 +3085,7 @@ pub fn record(opts: &RecordOptions) -> Result<RecordOutcome> {
outcome.trycmd_test = save_config(&config)?;
// The name on disk, not "config.json": `app record` writes `rust-anything.json` (review of #1290).
let staged_snapshot = snapshot.path().is_some();
- snapshot.commit().with_context(|| {
+ let previous_snapshot = snapshot.commit().with_context(|| {
format!("{CONFIG_FILE} is published, but the fresh-file snapshot is not")
})?;
meta.save(&dir).with_context(|| {
@@ -3089,6 +3097,11 @@ pub fn record(opts: &RecordOptions) -> Result<RecordOutcome> {
format!("{CONFIG_FILE} is published, but the CLI metadata is not")
}
})?;
+ // The swap handed the previous snapshot back instead of removing it, so the publish drops it
+ // once every step has succeeded.
+ if let Some(previous) = previous_snapshot {
+ let _ = std::fs::remove_dir_all(previous);
+ }
check_recording_complete(dropped, outcome.trycmd_test.as_deref())?;
if config.fresh.is_empty() {
let fresh_dir = dir.join(FRESH_DIR);
@@ -4775,14 +4788,18 @@ mod tests {
std::fs::write(staged.join("seed.json"), "new").unwrap();
let swapped = swap_in(&staged, &dest, &old);
let kept = std::fs::read_to_string(dest.join("seed.json")).ok();
- let leftover = old.exists() || staged.exists();
+ // The previous folder is handed back rather than removed, so a later step can put it back.
+ let handed_back = swapped.as_ref().ok().cloned().flatten();
+ let staged_left = staged.exists();
let _ = std::fs::remove_dir_all(&dir);
assert!(swapped.is_ok(), "{swapped:?}");
assert_eq!(kept.as_deref(), Some("new"));
- assert!(
- !leftover,
- "neither the staged nor the old folder is left behind"
+ assert_eq!(
+ handed_back.as_deref(),
+ Some(old.as_path()),
+ "the previous folder comes back to the caller"
);
+ assert!(!staged_left, "the staged folder is not left behind");
}
#[test]