From c2448b9be32bcb532c31948272c1eecf6fc794e8 Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Tue, 17 Sep 2024 13:03:02 +0200 Subject: [PATCH] Move corpus test to integration test --- src/lib.rs | 75 +------------------------------------------- tests/cli.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 74 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a9b8fdf..b173ed1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,80 +146,7 @@ mod test { use super::format; use miette::{miette, Result}; use rayon::prelude::*; - use std::{ - collections::HashMap, - path::{Path, PathBuf}, - }; - - #[test] - fn corpus() -> Result<()> { - use pretty_assertions::assert_eq; - - let corpus = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("corpus"); - - let update_baseline = std::env::var("UPDATE_BASELINE").is_ok(); - - let files = walkdir::WalkDir::new(&corpus) - .into_iter() - .filter_map(|e| { - let e = e.ok()?; - - if e.file_type().is_file() - && e.path().extension().and_then(|ext| ext.to_str()) == Some("spicy") - { - Some(e) - } else { - None - } - }) - .collect::>(); - - files - .par_iter() - .filter_map(|t| { - let input = std::fs::read_to_string(t.path()).ok()?; - - let output = { - let path = t.path().to_path_buf(); - - let file_name = format!( - "{}.expected", - path.file_name() - .unwrap_or_else(|| panic!( - "cannot get filename component of {}", - path.display() - )) - .to_string_lossy() - ); - - let mut o = path; - assert!(o.pop()); - - o.join(file_name) - }; - - let formatted = format(&input, false, false).unwrap_or_else(|_| { - panic!("cannot format source file {}", t.path().to_string_lossy()) - }); - - if !update_baseline { - let expected = std::fs::read_to_string(output).expect("cannot read baseline"); - assert_eq!( - expected, - formatted, - "while formatting {}", - t.path().display() - ); - } else { - std::fs::write(output, formatted).expect("cannot update baseline"); - } - - Some(1) - }) - .collect::>(); - - Ok(()) - } + use std::{collections::HashMap, path::Path}; #[test] fn no_trailing_newline() -> Result<()> { diff --git a/tests/cli.rs b/tests/cli.rs index f85aab1..d30f3d6 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,9 +1,11 @@ use std::{ io::{Read, Write}, + path::PathBuf, process::{Command, Stdio}, }; use assert_cmd::cargo::CommandCargoExt; +use rayon::prelude::*; use tempfile::NamedTempFile; type Result = std::result::Result>; @@ -38,3 +40,89 @@ fn trailing_newline_file() -> Result<()> { assert_eq!(stdout, "1;\n"); Ok(()) } + +/// Helper to format an input file via the CLI. +fn format

(input: P) -> Result +where + P: Into, +{ + let output = Command::cargo_bin("spicy-format")? + .arg("--reject-parse-errors") + .arg(input.into()) + .stdout(Stdio::piped()) + .output()?; + + let output = String::from_utf8(output.stdout)?; + + Ok(output) +} + +#[test] +fn corpus() -> Result<()> { + use pretty_assertions::assert_eq; + + let corpus = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("corpus"); + + let update_baseline = std::env::var("UPDATE_BASELINE").is_ok(); + + let files = walkdir::WalkDir::new(&corpus) + .into_iter() + .filter_map(|e| { + let e = e.ok()?; + + if e.file_type().is_file() + && e.path().extension().and_then(|ext| ext.to_str()) == Some("spicy") + { + Some(e) + } else { + None + } + }) + .collect::>(); + + files + .par_iter() + .filter_map(|t| { + let input = t.path(); + + let output = { + let path = input.to_path_buf(); + + let file_name = format!( + "{}.expected", + path.file_name() + .unwrap_or_else(|| panic!( + "cannot get filename component of {}", + path.display() + )) + .to_string_lossy() + ); + + let mut o = path; + assert!(o.pop()); + + o.join(file_name) + }; + + let formatted = format(&input).unwrap_or_else(|_| { + panic!("cannot format source file {}", t.path().to_string_lossy()) + }); + + if !update_baseline { + let expected = std::fs::read_to_string(output).expect("cannot read baseline"); + assert_eq!( + expected, + formatted, + "while formatting {}", + t.path().display() + ); + } else { + std::fs::write(output, formatted).expect("cannot update baseline"); + } + + Some(1) + }) + .collect::>(); + + Ok(()) +}