-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::env; | ||
use std::process::Command; | ||
|
||
use crate::path::project_path; | ||
|
||
#[cfg(test)] | ||
#[path = "command_test.rs"] | ||
mod command_test; | ||
|
||
/// Returns a shell command originating from the project root, with cargo environment variables | ||
/// filtered out. | ||
/// | ||
/// # Arguments | ||
/// * `command_name` - The shell command name. | ||
/// | ||
/// # Returns | ||
/// * A [`std::process::Command`] object with the current directory set to the project root, and | ||
/// cleared out cargo related environment variables. | ||
pub fn create_shell_command(command_name: &str) -> Command { | ||
let project_path = project_path().expect("Failed to get project path"); | ||
let mut command = Command::new(command_name); | ||
command.current_dir(&project_path); | ||
// Filter out all CARGO_ environment variables. | ||
env::vars().filter(|(key, _)| key.starts_with("CARGO_")).for_each(|(key, _)| { | ||
command.env_remove(key); | ||
}); | ||
command | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use crate::command::create_shell_command; | ||
|
||
#[test] | ||
fn create_shell_command_example() { | ||
let mut ls_command = create_shell_command("ls"); | ||
let output = ls_command.output().expect("Failed to execute command"); | ||
let stdout = String::from_utf8_lossy(&output.stdout); | ||
|
||
assert!(output.status.success()); | ||
// Project root should contain the `crates` directory. | ||
assert!(stdout.contains("crates")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod command; | ||
pub mod path; |