|
| 1 | +use std::sync::{Arc, Mutex}; |
| 2 | + |
| 3 | +#[derive(Clone, Debug)] |
| 4 | +pub(crate) struct MockCommandState { |
| 5 | + inner: Arc<Mutex<MockCommandInner>>, |
| 6 | +} |
| 7 | + |
| 8 | +#[derive(Debug, Default)] |
| 9 | +struct MockCommandInner { |
| 10 | + mocks: Vec<MockCommand>, |
| 11 | + calls: Vec<MockCommandCall>, |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Clone, Debug)] |
| 15 | +struct MockCommand { |
| 16 | + pattern: String, |
| 17 | + result: MockCommandResult, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Clone, Debug)] |
| 21 | +pub(crate) struct MockCommandResult { |
| 22 | + pub(crate) stdout: String, |
| 23 | + pub(crate) stderr: String, |
| 24 | + pub(crate) exit_code: i32, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Clone, Debug)] |
| 28 | +pub(crate) struct MockCommandCall { |
| 29 | + pub(crate) rendered: String, |
| 30 | + pub(crate) program: String, |
| 31 | + pub(crate) args: Vec<String>, |
| 32 | + pub(crate) stdin: String, |
| 33 | + pub(crate) stdout: String, |
| 34 | + pub(crate) stderr: String, |
| 35 | + pub(crate) exit_code: i32, |
| 36 | +} |
| 37 | + |
| 38 | +impl MockCommandState { |
| 39 | + pub(crate) fn new() -> Self { |
| 40 | + Self { |
| 41 | + inner: Arc::new(Mutex::new(MockCommandInner::default())), |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + pub(crate) fn reset(&self) -> mlua::Result<()> { |
| 46 | + let mut inner = self.lock()?; |
| 47 | + inner.mocks.clear(); |
| 48 | + inner.calls.clear(); |
| 49 | + Ok(()) |
| 50 | + } |
| 51 | + |
| 52 | + pub(crate) fn push_mock(&self, pattern: String, result: MockCommandResult) -> mlua::Result<()> { |
| 53 | + let mut inner = self.lock()?; |
| 54 | + inner.mocks.push(MockCommand { pattern, result }); |
| 55 | + Ok(()) |
| 56 | + } |
| 57 | + |
| 58 | + pub(crate) fn calls(&self) -> mlua::Result<Vec<MockCommandCall>> { |
| 59 | + let inner = self.lock()?; |
| 60 | + Ok(inner.calls.clone()) |
| 61 | + } |
| 62 | + |
| 63 | + pub(crate) fn execute( |
| 64 | + &self, |
| 65 | + rendered: String, |
| 66 | + program: String, |
| 67 | + args: Vec<String>, |
| 68 | + stdin: String, |
| 69 | + ) -> mlua::Result<MockCommandResult> { |
| 70 | + let mut inner = self.lock()?; |
| 71 | + let index = inner |
| 72 | + .mocks |
| 73 | + .iter() |
| 74 | + .position(|mock| { |
| 75 | + rendered.starts_with(&mock.pattern) || rendered.contains(&mock.pattern) |
| 76 | + }) |
| 77 | + .ok_or_else(|| { |
| 78 | + mlua::Error::external(format!("unmocked external command: {rendered}")) |
| 79 | + })?; |
| 80 | + let mock = inner.mocks.remove(index); |
| 81 | + inner.calls.push(MockCommandCall { |
| 82 | + rendered, |
| 83 | + program, |
| 84 | + args, |
| 85 | + stdin, |
| 86 | + stdout: mock.result.stdout.clone(), |
| 87 | + stderr: mock.result.stderr.clone(), |
| 88 | + exit_code: mock.result.exit_code, |
| 89 | + }); |
| 90 | + Ok(mock.result) |
| 91 | + } |
| 92 | + |
| 93 | + fn lock(&self) -> mlua::Result<std::sync::MutexGuard<'_, MockCommandInner>> { |
| 94 | + self.inner |
| 95 | + .lock() |
| 96 | + .map_err(|_| mlua::Error::external("mock command state lock is poisoned")) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +pub(crate) fn format_command(program: &str, args: &[String]) -> String { |
| 101 | + std::iter::once(program.to_string()) |
| 102 | + .chain(args.iter().map(|arg| shell_quote(arg))) |
| 103 | + .collect::<Vec<_>>() |
| 104 | + .join(" ") |
| 105 | +} |
| 106 | + |
| 107 | +fn shell_quote(value: &str) -> String { |
| 108 | + if value |
| 109 | + .bytes() |
| 110 | + .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.' | b'/' | b':' | b'=')) |
| 111 | + { |
| 112 | + value.to_string() |
| 113 | + } else { |
| 114 | + format!("'{}'", value.replace('\'', "'\"'\"'")) |
| 115 | + } |
| 116 | +} |
0 commit comments