Skip to content

Commit f5fa1cd

Browse files
committed
tests/inst: Remove all instances of sh_inline
- Update test helper functions to work with xshell Cmd struct types - Remove remaining sh_inline commands
1 parent d4808da commit f5fa1cd

3 files changed

Lines changed: 46 additions & 50 deletions

File tree

tests/inst/src/repobin.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::path::Path;
55

66
use crate::test::*;
77
use anyhow::{Context, Result};
8-
use sh_inline::bash_command;
98
use with_procspawn_tempdir::with_procspawn_tempdir;
109
use xshell::cmd;
1110

@@ -23,10 +22,10 @@ pub(crate) fn itest_nofifo() -> Result<()> {
2322
cmd!(sh, "mkdir tmproot").run()?;
2423
cmd!(sh, "mkfifo tmproot/afile").run()?;
2524
cmd_fails_with(
26-
bash_command!(
27-
r#"ostree --repo=repo commit -b fifotest -s "commit fifo" --tree=dir=./tmproot"#
28-
)
29-
.unwrap(),
25+
cmd!(
26+
sh,
27+
"ostree --repo=repo commit -b fifotest -s 'commit fifo' --tree=dir=./tmproot"
28+
),
3029
"Not a regular file or symlink",
3130
)?;
3231
Ok(())
@@ -109,18 +108,17 @@ pub(crate) fn itest_pull_basicauth() -> Result<()> {
109108
)
110109
.run()?;
111110

111+
let _p = sh.push_dir(basedir);
112112
for rem in &["unauth", "badauth"] {
113113
cmd_fails_with(
114-
bash_command!(
115-
r#"ostree --repo=client/repo pull origin-${rem} os >/dev/null"#,
116-
rem = *rem
117-
)
118-
.unwrap(),
114+
cmd!(
115+
sh,
116+
"ostree --repo=client/repo pull origin-{rem} os"
117+
).quiet().ignore_stdout(),
119118
"HTTP 403",
120119
)
121120
.context(rem)?;
122121
}
123-
let _p = sh.push_dir(basedir);
124122
cmd!(sh, "ostree --repo=client/repo pull origin-goodauth os")
125123
.ignore_stdout()
126124
.run()?;

tests/inst/src/sysroot.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ pub(crate) fn itest_immutable_bit() -> Result<()> {
5050
return Ok(());
5151
}
5252
// https://bugzilla.redhat.com/show_bug.cgi?id=1867601
53-
cmd_has_output(sh_inline::bash_command!("lsattr -d /").unwrap(), "-i-")?;
53+
cmd_has_output(cmd!(sh, "lsattr -d /"), "-i-")?;
54+
5455
Ok(())
5556
}
5657

tests/inst/src/test.rs

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use std::borrow::BorrowMut;
21
use std::fs::File;
32
use std::io::prelude::*;
43
use std::path::Path;
5-
use std::process::Command;
64
use std::time;
75

86
use anyhow::{bail, Context, Result};
97
use rand::Rng;
8+
use xshell::Cmd;
109

1110
// HTTP Server deps
1211
use futures_util::future;
@@ -16,30 +15,34 @@ use hyper_staticfile::Static;
1615
use tokio::runtime::Runtime;
1716

1817
/// Run command and assert that its stderr contains pat
19-
pub(crate) fn cmd_fails_with<C: BorrowMut<Command>>(mut c: C, pat: &str) -> Result<()> {
20-
let c = c.borrow_mut();
21-
let o = c.output()?;
22-
if o.status.success() {
23-
bail!("Command {:?} unexpectedly succeeded", c);
18+
pub fn cmd_fails_with(cmd: Cmd, pat: &str) -> Result<()> {
19+
let cmd_str = cmd.to_string();
20+
let output = cmd.ignore_status().output()?;
21+
22+
if output.status.success() {
23+
bail!("Command {:?} unexpectedly succeeded", cmd_str);
2424
}
25-
if twoway::find_bytes(&o.stderr, pat.as_bytes()).is_none() {
26-
dbg!(String::from_utf8_lossy(&o.stdout));
27-
dbg!(String::from_utf8_lossy(&o.stderr));
28-
bail!("Command {:?} stderr did not match: {}", c, pat);
25+
26+
if twoway::find_bytes(&output.stderr, pat.as_bytes()).is_none() {
27+
dbg!(String::from_utf8_lossy(&output.stdout));
28+
dbg!(String::from_utf8_lossy(&output.stderr));
29+
bail!("Command {:?} stderr did not match: {}", cmd_str, pat);
2930
}
31+
3032
Ok(())
3133
}
3234

33-
/// Run command and assert that its stdout contains pat
34-
pub(crate) fn cmd_has_output<C: BorrowMut<Command>>(mut c: C, pat: &str) -> Result<()> {
35-
let c = c.borrow_mut();
36-
let o = c.output()?;
37-
if !o.status.success() {
38-
bail!("Command {:?} failed", c);
35+
/// Run command and assert that its stdout contains `pat`
36+
pub(crate) fn cmd_has_output(cmd: Cmd, pat: &str) -> Result<()> {
37+
let cmd_str = cmd.to_string();
38+
let output = cmd.output()?; // run, error if command fails
39+
40+
if !output.status.success() {
41+
bail!("Command {} failed", cmd_str);
3942
}
40-
if twoway::find_bytes(&o.stdout, pat.as_bytes()).is_none() {
41-
dbg!(String::from_utf8_lossy(&o.stdout));
42-
bail!("Command {:?} stdout did not match: {}", c, pat);
43+
if twoway::find_bytes(&output.stdout, pat.as_bytes()).is_none() {
44+
dbg!(String::from_utf8_lossy(&output.stdout));
45+
bail!("Command {} stdout did not match: {}", cmd_str, pat);
4346
}
4447
Ok(())
4548
}
@@ -201,36 +204,30 @@ pub(crate) fn prepare_reboot<M: AsRef<str>>(mark: M) -> Result<()> {
201204
mod tests {
202205
use super::*;
203206

204-
fn oops() -> Command {
205-
let mut c = Command::new("/bin/bash");
206-
c.args(&["-c", "echo oops 1>&2; exit 1"]);
207-
c
207+
fn oops(sh: &Shell) -> Cmd {
208+
cmd!(sh, "bash -c 'echo oops 1>&2; exit 1'")
208209
}
209210

210211
#[test]
211212
fn test_fails_with_matches() -> Result<()> {
212-
cmd_fails_with(Command::new("false"), "")?;
213-
cmd_fails_with(oops(), "oops")?;
213+
cmd_fails_with(cmd!(sh, "false"), "")?;
214+
cmd_fails_with(oops(&sh), "oops")?;
214215
Ok(())
215216
}
216217

217218
#[test]
218-
fn test_fails_with_fails() {
219-
cmd_fails_with(Command::new("true"), "somepat").expect_err("true");
220-
cmd_fails_with(oops(), "nomatch").expect_err("nomatch");
219+
fn test_fails_with_fails() -> Result<()> {
220+
let sh = Shell::new()?;
221+
cmd_fails_with(cmd!(sh, "true"), "somepat").expect_err("true");
222+
cmd_fails_with(oops(&sh), "nomatch").expect_err("nomatch");
223+
Ok(())
221224
}
222225

223226
#[test]
224227
fn test_output() -> Result<()> {
225-
cmd_has_output(Command::new("true"), "")?;
226-
assert!(cmd_has_output(Command::new("true"), "foo").is_err());
227-
cmd_has_output(
228-
sh_inline::bash_command!("echo foobarbaz; echo fooblahbaz").unwrap(),
229-
"blah",
230-
)?;
231-
assert!(
232-
cmd_has_output(sh_inline::bash_command!("echo foobarbaz").unwrap(), "blah").is_err()
233-
);
228+
let sh = Shell::new()?;
229+
cmd_has_output(cmd!(sh, "echo foobarbaz; echo fooblahbaz"), "blah")?;
230+
assert!(cmd_has_output(cmd!(sh, "echo foobarbaz"), "blah").is_err());
234231
Ok(())
235232
}
236233

0 commit comments

Comments
 (0)