#![cfg(feature = "accept")]
use std::fs;
use std::path::PathBuf;
use test_better_core::{OrFail, TestResult};
use test_better_matchers::{check, eq, is_false, is_true};
use test_better_snapshot::apply_patches_from;
fn scratch_dir(tag: &str) -> PathBuf {
std::env::temp_dir().join(format!("test-better-accept-{}-{}", std::process::id(), tag))
}
#[test]
fn a_pending_patch_rewrites_the_literal_and_is_consumed() -> TestResult {
let root = scratch_dir("rewrite");
let pending = root.join("pending");
fs::create_dir_all(&pending).or_fail()?;
let fixture = root.join("fixture.rs");
let original =
"fn check() {\n check!(render()).matches_inline_snapshot(r#\"old value\"#)?;\n}\n";
fs::write(&fixture, original).or_fail()?;
fs::write(pending.join("1-0.patch"), "fixture.rs\n2:4\nnew value").or_fail()?;
let applied = apply_patches_from(&pending, &root).or_fail()?;
check!(applied.len()).satisfies(eq(1usize))?;
check!(applied[0].patches).satisfies(eq(1usize))?;
let rewritten = fs::read_to_string(&fixture).or_fail()?;
check!(rewritten.contains("r#\"new value\"#")).satisfies(is_true())?;
check!(rewritten.contains("old value")).satisfies(is_false())?;
check!(rewritten.starts_with("fn check() {\n")).satisfies(is_true())?;
check!(rewritten.ends_with("?;\n}\n")).satisfies(is_true())?;
check!(pending.join("1-0.patch").exists()).satisfies(is_false())?;
let second = apply_patches_from(&pending, &root).or_fail()?;
check!(second.is_empty()).satisfies(is_true())?;
let _ = fs::remove_dir_all(&root);
Ok(())
}
#[test]
fn a_missing_pending_directory_is_a_no_op() -> TestResult {
let root = scratch_dir("absent");
let applied = apply_patches_from(&root.join("pending"), &root).or_fail()?;
check!(applied.is_empty()).satisfies(is_true())
}