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

Fix #949 #957

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use anyhow::{Context, Result};
use std::{
env::current_dir,
path::{Path, PathBuf},
process::Command,
};
use subprocess::Exec;

pub struct RemoveFile(pub PathBuf);

Expand All @@ -16,6 +18,22 @@ impl Drop for RemoveFile {
}
}

/// Constructs a [`subprocess::Exec`] from a [`std::process::Command`].
pub fn exec_from_command(command: &Command) -> Exec {
let mut exec = Exec::cmd(command.get_program()).args(&command.get_args().collect::<Vec<_>>());
for (key, val) in command.get_envs() {
if let Some(val) = val {
exec = exec.env(key, val);
} else {
exec = exec.env_remove(key);
}
}
if let Some(path) = command.get_current_dir() {
exec = exec.cwd(path);
}
exec
}

/// Strips the current directory from the given path.
///
/// If the given path is not a child of the current directory, the path is
Expand Down
8 changes: 5 additions & 3 deletions frameworks/src/running.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::{ts, utils, OutputAccessors, OutputStrippedOfAnsiScapes, RunHigh};
use super::{ts, OutputAccessors, OutputStrippedOfAnsiScapes, RunHigh};
use anyhow::{anyhow, Error, Result};
use bstr::{io::BufReadExt, BStr};
use log::debug;
use necessist_core::{framework::Postprocess, source_warn, LightContext, Span, WarnFlags, Warning};
use necessist_core::{
framework::Postprocess, source_warn, util, LightContext, Span, WarnFlags, Warning,
};
use std::{cell::RefCell, path::Path, process::Command, rc::Rc};
use subprocess::{Exec, NullFile, Redirection};

Expand Down Expand Up @@ -80,7 +82,7 @@ impl<T: RunLow> RunHigh for RunAdapter<T> {
command.args(&context.opts.args);
command.args(final_args);

let mut exec = utils::exec_from_command(&command);
let mut exec = util::exec_from_command(&command);
if init_f_test.is_some() {
exec = exec.stdout(Redirection::Pipe);
} else {
Expand Down
10 changes: 5 additions & 5 deletions frameworks/src/ts/mocha/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{
utils, AbstractTypes, GenericVisitor, MaybeNamed, Named, OutputAccessors,
OutputStrippedOfAnsiScapes, ParseLow, Spanned, WalkDirResult,
AbstractTypes, GenericVisitor, MaybeNamed, Named, OutputAccessors, OutputStrippedOfAnsiScapes,
ParseLow, Spanned, WalkDirResult,
};
use anyhow::{anyhow, Result};
use if_chain::if_chain;
use log::debug;
use necessist_core::{
framework::Postprocess, source_warn, LightContext, LineColumn, SourceFile, Span, WarnFlags,
Warning,
framework::Postprocess, source_warn, util, LightContext, LineColumn, SourceFile, Span,
WarnFlags, Warning,
};
use once_cell::sync::Lazy;
use regex::Regex;
Expand Down Expand Up @@ -156,7 +156,7 @@ impl Mocha {
return Ok(None);
}

let mut exec = utils::exec_from_command(command);
let mut exec = util::exec_from_command(command);
exec = exec.stdout(NullFile);
exec = exec.stderr(NullFile);

Expand Down
16 changes: 0 additions & 16 deletions frameworks/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
use anyhow::{Context, Result};
use assert_cmd::output::OutputError;
use std::process::{Command, ExitStatus, Output};
use subprocess::Exec;

pub fn exec_from_command(command: &Command) -> Exec {
let mut exec = Exec::cmd(command.get_program()).args(&command.get_args().collect::<Vec<_>>());
for (key, val) in command.get_envs() {
if let Some(val) = val {
exec = exec.env(key, val);
} else {
exec = exec.env_remove(key);
}
}
if let Some(path) = command.get_current_dir() {
exec = exec.cwd(path);
}
exec
}

pub trait OutputStrippedOfAnsiScapes {
fn output_stripped_of_ansi_escapes(&mut self) -> Result<OutputError>;
Expand Down
38 changes: 27 additions & 11 deletions necessist/tests/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ examples/failure/tests/a.rs: Warning: dry run failed: code=101
.stdout(predicate::eq("2 candidates in 2 test files\n"));
}

// smoelius: Apperently, sending a ctrl-c on Windows is non-trivial:
// smoelius: Apparently, sending a ctrl-c on Windows is non-trivial:
// https://stackoverflow.com/questions/813086/can-i-send-a-ctrl-c-sigint-to-an-application-on-windows
// smoelius: Sending a ctrl-c allows the process to clean up after itself, e.g., to undo file
// rewrites.
#[cfg(not(windows))]
#[test]
fn resume_following_ctrl_c() {
use similar_asserts::SimpleDiff;
use std::{process::Stdio, thread::sleep, time::Duration};
use std::io::{BufRead, BufReader, Read};
use subprocess::Redirection;

const ROOT: &str = "examples/basic";

Expand All @@ -105,17 +108,30 @@ fn resume_following_ctrl_c() {

let _lock = BASIC_MUTEX.lock().unwrap();

let child = command().stderr(Stdio::piped()).spawn().unwrap();

sleep(Duration::from_secs(1));

kill().arg(child.id().to_string()).assert().success();

let output = child.wait_with_output().unwrap();

let stderr = String::from_utf8(output.stderr).unwrap();
let exec = util::exec_from_command(&command())
.stdout(Redirection::Pipe)
.stderr(Redirection::Pipe);
let mut popen = exec.popen().unwrap();

let stdout = popen.stdout.as_ref().unwrap();
let reader = BufReader::new(stdout);
let _: String = reader
.lines()
.map(Result::unwrap)
.find(|line| line == "examples/basic/src/lib.rs:4:5-4:12: `n += 1;` passed")
.unwrap();

let pid = popen.pid().unwrap();
kill().arg(pid.to_string()).assert().success();

let mut stderr = popen.stderr.as_ref().unwrap();
let mut buf = Vec::new();
let _ = stderr.read_to_end(&mut buf).unwrap();
let stderr = String::from_utf8(buf).unwrap();
assert!(stderr.ends_with("Ctrl-C detected\n"), "{stderr:?}");

let _ = popen.wait().unwrap();

let necessist_db = PathBuf::from("..").join(ROOT).join("necessist.db");

let _remove_file = util::RemoveFile(necessist_db);
Expand Down