Skip to content

Commit

Permalink
Move exec_from_command from necessist_frameworks::utils to `neces…
Browse files Browse the repository at this point in the history
…sist_core::util`
  • Loading branch information
smoelius committed Jan 15, 2024
1 parent f6bf667 commit 5b07582
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
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

0 comments on commit 5b07582

Please sign in to comment.