creator_simctl/
boot.rs

1use std::ffi::OsStr;
2use std::fmt::Display;
3
4use super::{Device, Result, Validate};
5
6impl Device {
7    /// Boots this device. If the device is already booted, this function will
8    /// return an error (as does the underlying CLI).
9    ///
10    /// NOTE: this does not automatically open the visual simulator interface.
11    /// Use [`crate::Simctl::open()`] to open the visual interface.
12    pub fn boot(&self) -> Result<()> {
13        self.boot_with_env(Vec::<(String, &OsStr)>::new())
14    }
15
16    /// Boots this device with the given environment variables. Do not prepend
17    /// `SIMCTL_CHILD_` to the variable names: this is done automatically. If
18    /// the device is already booted, this function will return an error (as
19    /// does the underlying CLI).
20    ///
21    /// NOTE: this does not automatically open the visual simulator interface.
22    /// Use [`crate::Simctl::open()`] to open the visual interface.
23    pub fn boot_with_env<I, K, V>(&self, envs: I) -> Result<()>
24    where
25        I: IntoIterator<Item = (K, V)>,
26        K: Display,
27        V: AsRef<OsStr>,
28    {
29        self.simctl()
30            .command("boot")
31            .arg(&self.info().udid)
32            .envs(
33                envs.into_iter()
34                    .map(|(key, value)| (format!("SIMCTL_CHILD_{}", key), value)),
35            )
36            .output()?
37            .validate()
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use serial_test::serial;
44
45    use super::*;
46    use crate::list::DeviceState;
47    use crate::mock;
48
49    #[test]
50    #[serial]
51    fn test_boot() -> Result<()> {
52        mock::device()?.boot()?;
53        assert_eq!(mock::device()?.state, DeviceState::Booted);
54
55        mock::device()?.shutdown()?;
56        assert_eq!(mock::device()?.state, DeviceState::Shutdown);
57
58        Ok(())
59    }
60}