use std::{ fs, path::Path, process::{Command, Output}, }; fn run(root: &Path, args: &[&str]) -> Output { Command::new(env!("CARGO_BIN_EXE_rumdl")) .current_dir(root) .args(args) .output() .unwrap() } fn has_format_cache(root: &Path) -> bool { let mut directories = vec![root.join("cache")]; while let Some(directory) = directories.pop() { for entry in fs::read_dir(directory).unwrap() { let entry = entry.unwrap(); if entry.file_type().unwrap().is_dir() { directories.push(entry.path()); } else if entry.file_name().to_string_lossy().starts_with("format-") { return true; } } } false } fn assert_no_format_cache(root: &Path) { assert!(!has_format_cache(root)); } #[test] fn silent_format_keeps_native_urls_in_reference_continuations_and_empty_labels() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("a.md"); let options = [ "--no-config", "--no-cache", "--respect-gitignore=false", "--flavor=standard", "--enable=MD034", "a.md", ]; for (atom, replacement) in [ ("https://example.com", ""), ("name@example.com", ""), ("www.example.com", ""), ] { // A definition whose destination or title is on the line after the // label is still a definition (CommonMark example 193), so nothing in it // is a bare URL and every mode leaves it as written. for source in [ format!("[x]:\n {atom}\n"), format!("[x]: https://example.org\n \"{atom}\"\n"), ] { fs::write(&path, &source).unwrap(); let checked = run(dir.path(), &[&["fmt", "--check", "--silent"][..], &options].concat()); assert_eq!(checked.status.code(), Some(0), "{source:?}: {checked:?}"); for mode in [ &["fmt", "--silent"][..], &["fmt"][..], &["check", "--fix", "--silent"][..], ] { let output = run(dir.path(), &[mode, &options].concat()); assert_eq!(output.status.code(), Some(0), "{source:?}, {mode:?}: {output:?}"); assert_eq!(fs::read_to_string(&path).unwrap(), source, "{source:?}, {mode:?}"); } } for source in [format!("![{atom}][r]\n"), format!("[{atom}][r]\n")] { let expected = source.replace(atom, replacement); fs::write(&path, &source).unwrap(); let checked = run(dir.path(), &[&["fmt", "--check", "--silent"][..], &options].concat()); assert_eq!(checked.status.code(), Some(1), "{source:?}: {checked:?}"); assert_eq!(fs::read_to_string(&path).unwrap(), source); for mode in [ &["fmt", "--silent"][..], &["fmt"][..], &["check", "--fix", "--silent"][..], ] { fs::write(&path, &source).unwrap(); let output = run(dir.path(), &[mode, &options].concat()); assert_eq!(output.status.code(), Some(0), "{source:?}, {mode:?}: {output:?}"); assert_eq!(fs::read_to_string(&path).unwrap(), expected, "{source:?}, {mode:?}"); } let stable = run(dir.path(), &[&["fmt", "--check", "--silent"][..], &options].concat()); assert_eq!(stable.status.code(), Some(0), "{source:?}: {stable:?}"); assert_eq!(fs::read_to_string(&path).unwrap(), expected); } } } #[test] fn silent_format_keeps_native_list_ownership_around_display_math() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("a.md"); let source = "1. a\n\n$$x\n- root\n see `$$`\n more\n"; let expected = "1. a\n\n$$x\n- root\n see `$$`\n more\n"; let options = [ "--no-config", "--no-cache", "--flavor=standard", "--enable=MD077", "a.md", ]; fs::write(&path, source).unwrap(); let checked = run(dir.path(), &[&["fmt", "--check", "--silent"][..], &options].concat()); assert_eq!(checked.status.code(), Some(1), "{checked:?}"); assert_eq!(fs::read_to_string(&path).unwrap(), source); for mode in [ &["fmt", "--silent"][..], &["fmt"][..], &["check", "--fix", "--silent"][..], ] { fs::write(&path, source).unwrap(); let output = run(dir.path(), &[mode, &options].concat()); assert_eq!(output.status.code(), Some(0), "{mode:?}: {output:?}"); assert_eq!(fs::read_to_string(&path).unwrap(), expected, "{mode:?}"); } let stable = run(dir.path(), &[&["fmt", "--check", "--silent"][..], &options].concat()); assert_eq!(stable.status.code(), Some(0), "{stable:?}"); assert_eq!(fs::read_to_string(&path).unwrap(), expected); } #[test] fn silent_format_reuses_a_complete_clean_check_without_running_fixes() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("a.md"); fs::write(&path, "# Title\n\nPlain text.\n").unwrap(); let checked = run( dir.path(), &["check", "--no-config", "--silent", "--cache-dir", "cache", "a.md"], ); assert_eq!(checked.status.code(), Some(0), "{checked:?}"); let formatted = Command::new(env!("CARGO_BIN_EXE_rumdl")) .current_dir(dir.path()) .args([ "fmt", "--check", "--no-config", "--silent", "--cache-dir", "cache", "a.md", ]) .output() .unwrap(); assert_eq!(formatted.status.code(), Some(0), "{formatted:?}"); assert_no_format_cache(dir.path()); assert_eq!(fs::read_to_string(&path).unwrap(), "# Title\n\nPlain text.\n"); let dirty = "#Title\n\nPlain text.\n"; fs::write(&path, dirty).unwrap(); let formatted = Command::new(env!("CARGO_BIN_EXE_rumdl")) .current_dir(dir.path()) .args([ "fmt", "--check", "--no-config", "--silent", "--cache-dir", "cache", "a.md", ]) .output() .unwrap(); assert_eq!(formatted.status.code(), Some(1), "{formatted:?}"); assert_eq!(fs::read_to_string(&path).unwrap(), dirty); } #[test] fn silent_format_reuses_identical_clean_files_with_the_same_settings() { let dir = tempfile::tempdir().unwrap(); for name in ["a.md", "b.md", "c.md", "d.md"] { fs::write(dir.path().join(name), "# Title\n\nPlain text.\n").unwrap(); } let checked = run( dir.path(), &["check", "--no-config", "--silent", "--cache-dir", "cache", "."], ); assert_eq!(checked.status.code(), Some(0), "{checked:?}"); let formatted = Command::new(env!("CARGO_BIN_EXE_rumdl")) .current_dir(dir.path()) .args(["fmt", "--check", "--no-config", "--silent", "--cache-dir", "cache", "."]) .output() .unwrap(); assert_eq!(formatted.status.code(), Some(0), "{formatted:?}"); assert_no_format_cache(dir.path()); } #[test] fn silent_format_does_not_import_another_files_flavor() { let dir = tempfile::tempdir().unwrap(); fs::write( dir.path().join(".rumdl.toml"), "[per-file-flavor]\n\"ignored.md\" = \"mkdocs\"\n\"target.md\" = \"standard\"\n\n[MD046]\nstyle = \"fenced\"\n", ) .unwrap(); for name in ["ignored.md", "target.md"] { fs::write(dir.path().join(name), "# T\n\n!!! note\n\n indented content\n").unwrap(); } let options = ["--silent", "--cache-dir=cache", "--enable=MD046"]; let checked = run(dir.path(), &[&["check"][..], &options, &["ignored.md"]].concat()); assert_eq!(checked.status.code(), Some(0), "{checked:?}"); let formatted = run(dir.path(), &[&["fmt"][..], &options, &["target.md"]].concat()); assert_eq!(formatted.status.code(), Some(0), "{formatted:?}"); assert_eq!( fs::read_to_string(dir.path().join("target.md")).unwrap(), "# T\n\n!!! note\n\n```\nindented content\n```\n" ); } #[test] fn silent_format_does_not_import_another_files_detected_flavor() { let dir = tempfile::tempdir().unwrap(); let source = "# T\n\n{/*\n\n indented content\n\n*/}\n"; for name in ["ignored.mdx", "target.md"] { fs::write(dir.path().join(name), source).unwrap(); } let options = [ "--no-config", "--silent", "--cache-dir=cache", "--enable=MD046", "--config=MD046.style=\"fenced\"", ]; let checked = run(dir.path(), &[&["check"][..], &options, &["ignored.mdx"]].concat()); assert_eq!(checked.status.code(), Some(0), "{checked:?}"); let formatted = run( dir.path(), &[&["fmt", "--check"][..], &options, &["target.md"]].concat(), ); assert_eq!(formatted.status.code(), Some(1), "{formatted:?}"); assert_eq!(fs::read_to_string(dir.path().join("target.md")).unwrap(), source); } #[test] fn silent_format_does_not_import_another_files_ignored_check() { let dir = tempfile::tempdir().unwrap(); fs::write(dir.path().join("ignored.md"), "*word*\n").unwrap(); let target = dir.path().join("target.md"); fs::write(&target, "*word*\n").unwrap(); let options = [ "--no-config", "--silent", "--cache-dir=cache", "--enable=MD049", "--config=MD049.style=\"underscore\"", "--config=per-file-ignores={\"ignored.md\"=[\"MD049\"]}", ]; let checked = run(dir.path(), &[&["check"][..], &options, &["ignored.md"]].concat()); assert_eq!(checked.status.code(), Some(0), "{checked:?}"); let formatted = run(dir.path(), &[&["fmt"][..], &options, &["target.md"]].concat()); assert_eq!(formatted.status.code(), Some(0), "{formatted:?}"); assert_eq!(fs::read_to_string(target).unwrap(), "_word_\n"); } #[test] fn silent_format_matches_diagnostic_format_bytes_and_check_status() { let dir = tempfile::tempdir().unwrap(); let original = "\r\n#Title\r\n\r\n*word* é🦀\r\n\r\n\r\n* spaced *\r\n\r\n[missing](missing.md)\r\n"; for flavor in [ "standard", "mkdocs", "mdx", "kramdown", "pandoc", "quarto", "obsidian", "azure_devops", "myst", "hugo", "mdg", "gh-aw", ] { let mut expected = None; for silent in [false, true] { fs::write(dir.path().join("a.md"), original).unwrap(); let mut args = vec!["fmt", "--no-config", "--no-cache", "--flavor", flavor, "a.md"]; if silent { args.push("--silent"); } let output = run(dir.path(), &args); assert_eq!(output.status.code(), Some(0), "{flavor}: {output:?}"); let bytes = fs::read(dir.path().join("a.md")).unwrap(); if let Some(expected) = &expected { assert_eq!(&bytes, expected, "{flavor}"); } else { expected = Some(bytes); } if silent { assert!(output.stdout.is_empty()); assert!(output.stderr.is_empty()); } } fs::write(dir.path().join("a.md"), original).unwrap(); let output = run( dir.path(), &[ "fmt", "--no-config", "--no-cache", "--silent", "--check", "--flavor", flavor, "a.md", ], ); assert_eq!(output.status.code(), Some(1), "{flavor}: {output:?}"); assert_eq!(fs::read_to_string(dir.path().join("a.md")).unwrap(), original); assert!(output.stdout.is_empty()); assert!(output.stderr.is_empty()); fs::write(dir.path().join("a.md"), expected.unwrap()).unwrap(); let output = run( dir.path(), &[ "fmt", "--no-config", "--no-cache", "--silent", "--check", "--flavor", flavor, "a.md", ], ); assert_eq!(output.status.code(), Some(0), "{flavor}: {output:?}"); } } #[test] fn silent_format_retains_configuration_and_read_errors() { let dir = tempfile::tempdir().unwrap(); fs::write(dir.path().join("a.md"), "\n#Title\n").unwrap(); for args in [ vec![ "fmt", "--no-config", "--no-cache", "--silent", "--check", "--deny-config-warnings", "a.md", ], vec!["fmt", "--no-config", "--no-cache", "--silent", "missing.md"], ] { let output = run(dir.path(), &args); assert_eq!(output.status.code(), Some(2), "{output:?}"); assert!(output.stdout.is_empty()); assert!(output.stderr.is_empty()); } } #[test] fn silent_format_keeps_encoding_and_conflict_guards() { let dir = tempfile::tempdir().unwrap(); for original in [ b"#Title\n\nbad\xfftext\n".as_slice(), b"#Title\n\n<<<<<<< HEAD\nours\n=======\ntheirs\n>>>>>>> branch\n", b"\x00\xff\x00", ] { fs::write(dir.path().join("a.md"), original).unwrap(); let output = run(dir.path(), &["fmt", "--no-config", "--no-cache", "--silent", "a.md"]); assert_eq!(output.status.code(), Some(0), "{output:?}"); assert_eq!(fs::read(dir.path().join("a.md")).unwrap(), original); } } #[test] fn silent_format_does_not_cache_an_empty_diagnostic_report() { let dir = tempfile::tempdir().unwrap(); fs::write(dir.path().join("a.md"), "#Title\n\n[missing](missing.md)\n").unwrap(); let output = run( dir.path(), &["fmt", "--no-config", "--silent", "--cache-dir", "cache", "a.md"], ); assert_eq!(output.status.code(), Some(0), "{output:?}"); for _ in 0..2 { let output = run( dir.path(), &[ "check", "--no-config", "--output-format=json", "--cache-dir", "cache", "a.md", ], ); assert_eq!(output.status.code(), Some(1), "{output:?}"); let warnings: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap(); assert!( warnings.as_array().unwrap().iter().any(|w| w["rule"] == "MD057"), "{warnings}" ); } } #[test] fn silent_format_preserves_exclusion_order_and_kramdown_report_gate() { let dir = tempfile::tempdir().unwrap(); for (original, options) in [ ( "{::nomarkdown}\r\n[ref]\r\n- item\r\n continuation\r\nexport const x = \"_a_\";\r\n\r\n continuation\r\n:::", vec!["--flavor=kramdown"], ), ( "[ref]: https://example.com\n- item\nexport const x = \"_a_\";\n \n:::\n