Skip to content

Commit

Permalink
chore: add shell command builder
Browse files Browse the repository at this point in the history
commit-id:96859c27
  • Loading branch information
Itay-Tsabary-Starkware committed Nov 25, 2024
1 parent 6c5ac10 commit 2f3ac1a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/infra_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ workspace = true

[dependencies]
thiserror.workspace = true
tokio = { workspace = true, features = ["process"] }

[dev-dependencies]
rstest.workspace = true
29 changes: 29 additions & 0 deletions crates/infra_utils/src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::env;

use tokio::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
}
15 changes: 15 additions & 0 deletions crates/infra_utils/src/command_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use rstest::rstest;

use crate::command::create_shell_command;

#[rstest]
#[tokio::test]
async fn create_shell_command_example() {
let mut ls_command = create_shell_command("ls");
let output = ls_command.output().await.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"));
}
1 change: 1 addition & 0 deletions crates/infra_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod command;
pub mod path;

0 comments on commit 2f3ac1a

Please sign in to comment.