inline_c/
assert.rs

1use std::{fs, path::PathBuf, process::Command};
2
3/// `Assert` is a wrapper around the [`assert_cmd::assert::Assert`]
4/// struct.
5pub struct Assert {
6    command: assert_cmd::Command,
7    files_to_remove: Option<Vec<PathBuf>>,
8}
9
10impl Assert {
11    pub(crate) fn new(command: Command, files_to_remove: Option<Vec<PathBuf>>) -> Self {
12        Self {
13            command: assert_cmd::Command::from_std(command),
14            files_to_remove,
15        }
16    }
17
18    pub fn assert(&mut self) -> assert_cmd::assert::Assert {
19        self.command.assert()
20    }
21
22    /// Shortcut to `self.assert().success()`.
23    pub fn success(&mut self) -> assert_cmd::assert::Assert {
24        self.assert().success()
25    }
26
27    /// Shortcut to `self.assert().failure()`.
28    pub fn failure(&mut self) -> assert_cmd::assert::Assert {
29        self.assert().failure()
30    }
31}
32
33impl Drop for Assert {
34    fn drop(&mut self) {
35        if let Some(files_to_remove) = &self.files_to_remove {
36            for file in files_to_remove.iter() {
37                if fs::metadata(file).is_ok() {
38                    fs::remove_file(file)
39                        .unwrap_or_else(|_| panic!("Failed to remove `{:?}`", file));
40                }
41            }
42        }
43    }
44}