Skip to content

Commit

Permalink
Move corpus test to integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Sep 17, 2024
1 parent 4f0ff3a commit c2448b9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 74 deletions.
75 changes: 1 addition & 74 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();

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::<Vec<_>>();

Ok(())
}
use std::{collections::HashMap, path::Path};

#[test]
fn no_trailing_newline() -> Result<()> {
Expand Down
88 changes: 88 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::result::Result<T, Box<dyn std::error::Error>>;
Expand Down Expand Up @@ -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<P>(input: P) -> Result<String>
where
P: Into<PathBuf>,
{
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::<Vec<_>>();

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::<Vec<_>>();

Ok(())
}

0 comments on commit c2448b9

Please sign in to comment.