Make test suite runnable in miri

This commit is contained in:
David Tolnay 2022-07-07 18:28:43 -07:00
parent a6de143358
commit d1a5075546
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
6 changed files with 83 additions and 23 deletions

View file

@ -36,7 +36,7 @@ use unsafe_libyaml::{
YAML_LITERAL_SCALAR_STYLE, YAML_PLAIN_SCALAR_STYLE, YAML_SINGLE_QUOTED_SCALAR_STYLE,
YAML_UTF8_ENCODING,
};
unsafe fn unsafe_main(
pub unsafe fn unsafe_main(
stdin: &mut dyn Read,
mut stdout: &mut dyn Write,
) -> Result<(), Box<dyn Error>> {

View file

@ -28,7 +28,7 @@ use unsafe_libyaml::{
YAML_SEQUENCE_END_EVENT, YAML_SEQUENCE_START_EVENT, YAML_STREAM_END_EVENT,
YAML_STREAM_START_EVENT,
};
unsafe fn unsafe_main(
pub unsafe fn unsafe_main(
mut stdin: &mut dyn Read,
stdout: &mut dyn Write,
) -> Result<(), Box<dyn Error>> {

48
tests/bin/mod.rs Normal file
View file

@ -0,0 +1,48 @@
use std::error::Error;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::process::{Command, Stdio};
pub struct Output {
pub success: bool,
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
}
pub fn run(
compiled: &str,
unsafe_main: unsafe fn(
stdin: &mut dyn Read,
stdout: &mut dyn Write,
) -> Result<(), Box<dyn Error>>,
input: &Path,
) -> Output {
if cfg!(miri) {
let mut input = File::open(input).unwrap();
let mut stdout = Vec::new();
let result = unsafe { unsafe_main(&mut input, &mut stdout) };
Output {
success: result.is_ok(),
stdout,
stderr: result
.err()
.as_ref()
.map_or_else(String::new, ToString::to_string)
.into(),
}
} else {
let output = Command::new(compiled)
.arg(input)
.stdin(Stdio::null())
.output()
.unwrap();
Output {
success: output.status.success(),
stdout: output.stdout,
stderr: output.stderr,
}
}
}

View file

@ -1,6 +1,10 @@
mod bin;
#[path = "../src/bin/run-emitter-test-suite.rs"]
#[allow(dead_code)]
mod run_emitter_test_suite;
use std::fs;
use std::path::Path;
use std::process::{Command, Stdio};
fn test(id: &str) {
let dir = Path::new("tests")
@ -8,11 +12,11 @@ fn test(id: &str) {
.join("yaml-test-suite")
.join(id);
let output = Command::new(env!("CARGO_BIN_EXE_run-emitter-test-suite"))
.arg(dir.join("test.event"))
.stdin(Stdio::null())
.output()
.unwrap();
let output = bin::run(
env!("CARGO_BIN_EXE_run-emitter-test-suite"),
run_emitter_test_suite::unsafe_main,
&dir.join("test.event"),
);
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
@ -25,7 +29,7 @@ fn test(id: &str) {
};
let expected = fs::read_to_string(out).unwrap();
pretty_assertions::assert_str_eq!(expected, stdout);
assert!(output.status.success());
assert!(output.success);
}
unsafe_libyaml_test_suite::test_emitter!();

View file

@ -1,6 +1,10 @@
mod bin;
#[path = "../src/bin/run-parser-test-suite.rs"]
#[allow(dead_code)]
mod run_parser_test_suite;
use std::fs;
use std::path::Path;
use std::process::{Command, Stdio};
fn test(id: &str) {
let dir = Path::new("tests")
@ -8,11 +12,11 @@ fn test(id: &str) {
.join("yaml-test-suite")
.join(id);
let output = Command::new(env!("CARGO_BIN_EXE_run-parser-test-suite"))
.arg(dir.join("in.yaml"))
.stdin(Stdio::null())
.output()
.unwrap();
let output = bin::run(
env!("CARGO_BIN_EXE_run-parser-test-suite"),
run_parser_test_suite::unsafe_main,
&dir.join("in.yaml"),
);
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
@ -20,7 +24,7 @@ fn test(id: &str) {
let expected = fs::read_to_string(dir.join("test.event")).unwrap();
pretty_assertions::assert_str_eq!(expected, stdout);
assert!(output.status.success());
assert!(output.success);
}
unsafe_libyaml_test_suite::test_parser!();

View file

@ -1,5 +1,9 @@
mod bin;
#[path = "../src/bin/run-parser-test-suite.rs"]
#[allow(dead_code)]
mod run_parser_test_suite;
use std::path::Path;
use std::process::{Command, Stdio};
fn test(id: &str) {
let dir = Path::new("tests")
@ -7,13 +11,13 @@ fn test(id: &str) {
.join("yaml-test-suite")
.join(id);
let output = Command::new(env!("CARGO_BIN_EXE_run-parser-test-suite"))
.arg(dir.join("in.yaml"))
.stdin(Stdio::null())
.output()
.unwrap();
let output = bin::run(
env!("CARGO_BIN_EXE_run-parser-test-suite"),
run_parser_test_suite::unsafe_main,
&dir.join("in.yaml"),
);
if output.status.success() {
if output.success {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
eprint!("{}", stdout);