//! Every case runs over a SCRATCH tree the test writes itself, never the repo://! a drift check that could write into the tree it is checking is not a check.usesuper::*;use std::path::PathBuf;/// A fresh directory under the crate's own target dir — an ordinary temp dir/// would do, but this keeps the scratch beside the build it belongs to and out/// of a shared `/tmp` two lanes could collide in.fn scratch(name: &str) -> PathBuf {// Run time, not `env!` — a cached test binary carries a checkout that is gone (#403).let dir = PathBuf::from( std::env::var("CARGO_MANIFEST_DIR") .expect("cargo sets CARGO_MANIFEST_DIR for every `cargo test`"), ) .join("target/cotton-gen-tests") .join(name);let _ = std::fs::remove_dir_all(&dir); std::fs::create_dir_all(&dir).expect("a scratch tree"); dir}/// RULE: Generated design files belong to shared platform modules, so generation cannot create a second copy of shared design inside an app module.#[test]fn generated_design_stays_in_shared_platform_modules() {let files = generate().expect("rendered"); assert!(files.keys().all(|name| [IOS_DIR, IOS_PAPER_DIR, ANDROID_DIR, ANDROID_CORE_DIR].iter().any(|dir| std::path::Path::new(name).starts_with(dir))));}#[test]fn it_sees_nothing_wrong_with_what_it_just_wrote() {let dir = scratch("clean"); write(&dir).expect("written"); assert_eq!(drift(&dir).expect("checked"), Vec::<String>::new());}fn emitted_file(module: &str, accepts: implFn(&str) -> bool) -> String { generate().expect("rendered").into_keys() .find(|name| std::path::Path::new(name).starts_with(module) && accepts(name)) .expect("an emitted fixture file")}/// RULE: Drift detection reports a changed emitted file, regardless of its name or contents, so a generator rename cannot hide edits.#[test]fn it_names_a_file_somebody_edited() {let dir = scratch("edited"); write(&dir).expect("written");let file = emitted_file(IOS_DIR, |name| name.ends_with(".swift")); std::fs::write(dir.join(&file), "// hand-edited\n").expect("edited"); assert!(drift(&dir).expect("checked").contains(&file));}/// RULE: Drift detection catches even one changed byte in an emitted asset, so a typo cannot pass because the shipped palette changed.#[test]fn it_names_one_hand_edited_byte_in_a_colorset() {let dir = scratch("one-byte"); write(&dir).expect("written");let file = emitted_file(IOS_PAPER_DIR, |name| name.ends_with(".colorset/Contents.json"));letmut bytes = std::fs::read(dir.join(&file)).expect("read"); bytes[0] = if bytes[0] == b'{' { b'[' } else { b'{' }; std::fs::write(dir.join(&file), bytes).expect("edited"); assert_eq!(drift(&dir).expect("checked"), vec![file]);}#[test]fn it_names_a_file_that_is_there_and_should_not_be() {// A stale colorset for a role somebody deleted would otherwise sit in the// catalogue for ever, compiled into every build and named by nothing.let dir = scratch("ghost"); write(&dir).expect("written");let ghost = dir.join(IOS_PAPER_DIR).join("Palette.xcassets/ghost.colorset"); std::fs::create_dir_all(&ghost).expect("a ghost"); std::fs::write(ghost.join("Contents.json"), "{}\n").expect("a ghost");let differing = drift(&dir).expect("checked").join(" "); assert!( differing.contains("ghost.colorset/Contents.json"),"{differing}" );}/// RULE: A missing emitted source file is reported, so removing a generated file cannot silently pass the build's drift check.#[test]fn it_names_a_file_that_is_missing_entirely() {let dir = scratch("missing"); write(&dir).expect("written");let file = emitted_file(IOS_DIR, |name| name.ends_with(".swift")); std::fs::remove_file(dir.join(&file)).expect("removed"); assert!(drift(&dir).expect("checked").contains(&file));}#[test]fn an_empty_tree_is_every_file_as_drift_rather_than_a_throw() {let dir = scratch("empty"); assert_eq!( drift(&dir).expect("checked").len(), generate().expect("rendered").len() );}/// RULE: One drift pass reports changed, missing and extra files together, so one defect never masks the others.#[test]fn a_pass_reports_changed_missing_and_extra_files() {let dir = scratch("mixed-drift"); write(&dir).expect("written");let changed = emitted_file(ANDROID_DIR, |name| name.ends_with(".kt"));let missing = emitted_file(IOS_DIR, |name| name.ends_with(".swift"));let extra = format!("{ANDROID_DIR}/Obsolete.kt"); std::fs::write(dir.join(&changed), "// edited").unwrap(); std::fs::remove_file(dir.join(&missing)).unwrap(); std::fs::write(dir.join(&extra), "").unwrap();let differences = drift(&dir).unwrap();let changed_files = [changed, missing, extra]; assert_eq!(differences.len(), changed_files.len());for file in changed_files { assert!(differences.iter().any(|difference| difference.contains(&file)), "{file}"); }}/// RULE: Android core generation detects changed, missing and extra sources independently, so shared payload drift never depends on a particular emitted filename.#[test]fn android_core_generation_reports_changed_missing_and_extra_files() {let dir = scratch("android-core-drift"); write(&dir).expect("written");let file = emitted_file(ANDROID_CORE_DIR, |name| name.ends_with(".kt")); std::fs::write(dir.join(&file), "// edited").unwrap(); assert_eq!(drift(&dir).unwrap(), vec![file.clone()]); std::fs::remove_file(dir.join(&file)).unwrap(); assert_eq!(drift(&dir).unwrap(), vec![file]); write(&dir).unwrap();let extra = format!("{ANDROID_CORE_DIR}/Obsolete.kt"); std::fs::write(dir.join(&extra), "").unwrap();let differences = drift(&dir).unwrap(); assert_eq!(differences.len(), 1, "one synthetic extra file"); assert!(differences[0].contains(&extra));}