Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 33c0a8b

Browse files
codexByron
andcommitted
feat: add invoke_bash test helper()
Co-authored-by: Sebastian Thiel <[email protected]>
1 parent 06a1248 commit 33c0a8b

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

tests/tools/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,27 @@ pub fn run_git(working_dir: &Path, args: &[&str]) -> std::io::Result<std::proces
260260
.status()
261261
}
262262

263+
/// Run `script` with [`bash_program()`] in `cwd`.
264+
///
265+
/// Standard input is disconnected while standard output and error stay attached to the inherited
266+
/// handles.
267+
///
268+
/// # Panics
269+
///
270+
/// This function expects the script to succeed and will panic otherwise.
271+
pub fn invoke_bash(cwd: impl AsRef<Path>, script: &str) {
272+
let status = std::process::Command::new(bash_program())
273+
.current_dir(cwd)
274+
.arg("-c")
275+
.arg(script)
276+
.stdin(std::process::Stdio::null())
277+
.stdout(std::process::Stdio::inherit())
278+
.stderr(std::process::Stdio::inherit())
279+
.status()
280+
.expect("can run bash script");
281+
assert!(status.success(), "bash script failed with {status}");
282+
}
283+
263284
/// Spawn a git daemon process to host all repository at or below `working_dir`.
264285
pub fn spawn_git_daemon(working_dir: impl AsRef<Path>) -> std::io::Result<GitDaemon> {
265286
let mut ports: Vec<_> = (9419u16..9419 + 100).collect();

tests/tools/src/tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,13 @@ fn bash_program_absolute_or_unrooted() {
130130
let bash = bash_program();
131131
assert!(!is_rooted_relative(bash), "{bash:?}");
132132
}
133+
134+
#[test]
135+
fn invoke_bash_runs_in_given_working_directory() {
136+
let dir = tempfile::TempDir::new().expect("can create temp dir");
137+
invoke_bash(dir.path(), "printf '%s' hello > out");
138+
assert_eq!(
139+
std::fs::read(dir.path().join("out")).expect("script wrote output"),
140+
b"hello"
141+
);
142+
}

0 commit comments

Comments
 (0)