Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework solution api #71

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,15 @@ impl App {
}
}

let run_command: Command = command_from_argument(args.get_one::<String>("command"))?
let mut run_command = command_from_argument(args.get_one::<String>("command"))?
.expect("--command is required to run solution.");

let timeout_seconds: f64 = *args.get_one::<f64>("timeout").unwrap_or(&5.0);

let timeout = std::time::Duration::from_micros(match timeout_seconds {
x if x.is_nan() => return Err(anyhow!("Timeout can't be NaN")),
x if x < 0.0 => return Err(anyhow!("Timeout can't be negative (use 0 for no timeout)")),
x if x == 0.0 => u64::MAX,
x => (1e6 * x) as u64,
});
let timeout = match *args.get_one::<f64>("timeout").unwrap_or(&5.0) {
secs if secs.is_nan() => return Err(anyhow!("Timeout can't be NaN")),
secs if secs < 0.0 => return Err(anyhow!("Timeout can't be negative (use 0 for no timeout)")),
secs if secs == 0.0 => std::time::Duration::MAX,
secs => std::time::Duration::from_secs(secs as u64),
Andriamanitra marked this conversation as resolved.
Show resolved Hide resolved
};

let all_testcases = self.read_clash(&handle)?.testcases().to_owned();

Expand All @@ -366,18 +364,18 @@ impl App {
};

let num_tests = testcases.len();
let suite_run = solution::run(testcases, run_command, timeout);
let suite_run = solution::lazy_run(testcases, &mut run_command, &timeout);

let ignore_failures = args.get_flag("ignore-failures");
let show_whitespace = *args.get_one::<bool>("show-whitespace").unwrap_or(&false);
let ostyle = OutputStyle::from_env(show_whitespace);

let mut num_passed = 0;

for testrun in suite_run {
ostyle.print_result(&testrun);
for test_run in suite_run {
ostyle.print_result(&test_run);

if testrun.is_successful() {
if test_run.is_successful() {
num_passed += 1;
} else if !ignore_failures {
break
Expand Down
69 changes: 61 additions & 8 deletions src/solution.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,71 @@
mod suite_run;
mod test_run;

use std::io::Write;
use std::process::Command;
use std::time::Duration;

use suite_run::SuiteRun;
pub use test_run::{TestResult, TestRun};
use wait_timeout::ChildExt;

use crate::clash::TestCase;

pub fn run(
testcases: Vec<&TestCase>,
run_command: Command,
timeout: Duration,
) -> impl IntoIterator<Item = TestRun> {
SuiteRun::new(testcases, run_command, timeout)
pub fn lazy_run<'a>(
testcases: impl IntoIterator<Item = &'a TestCase>,
run_command: &'a mut Command,
timeout: &'a Duration,
) -> impl IntoIterator<Item = TestRun<'a>> {
testcases.into_iter().map(|test| run_testcase(test, run_command, timeout))
}

fn run_testcase<'a>(test: &'a TestCase, run_command: &mut Command, timeout: &Duration) -> TestRun<'a> {
let mut run = match run_command
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
{
Ok(run) => run,
Err(error) => return TestRun::new(test, unable_to_run(error, run_command)),
};

run.stdin
.as_mut()
.unwrap()
.write(test.test_in.as_bytes())
.expect("Fatal error: could not write to stdin.");

TestRun::new(test, get_result(run, &test.test_out, timeout))
}

fn unable_to_run(error: std::io::Error, cmd: &mut Command) -> TestResult {
TestResult::UnableToRun {
error_msg: format!("{}: {}", cmd.get_program().to_str().unwrap_or("Unable to run command"), error),
}
}

fn get_result(mut run: std::process::Child, expected: &str, timeout: &Duration) -> TestResult {
let timed_out = run.wait_timeout(*timeout).expect("Could not wait for program execution.").is_none();

if timed_out {
run.kill().expect("Failed to kill test run process");
Andriamanitra marked this conversation as resolved.
Show resolved Hide resolved
}

let output = run.wait_with_output().expect("Could not wait for program execution.");

let stdout = String::from_utf8(output.stdout)
.unwrap_or_default()
.replace("\r\n", "\n")
.trim_end()
.to_string();
let stderr = String::from_utf8(output.stderr).unwrap_or_default();

if stdout == expected.trim_end() {
TestResult::Success
} else if timed_out {
TestResult::Timeout { stdout, stderr }
} else if output.status.success() {
TestResult::WrongOutput { stdout, stderr }
} else {
TestResult::RuntimeError { stdout, stderr }
}
}
90 changes: 0 additions & 90 deletions src/solution/suite_run.rs

This file was deleted.