creator_simctl/
get_app_container.rs

1//! Supporting types for the `simctl get_app_container` subcommand.
2
3use std::path::{Path, PathBuf};
4use std::process::Stdio;
5
6use super::{Device, Result, Validate};
7
8/// Identifies a container that iOS stores a particular kind of data in.
9#[derive(Clone, Debug, Eq, PartialEq)]
10pub enum Container {
11    /// This is the .app bundle itself. Apps cannot write to their app
12    /// container.
13    App,
14
15    /// This is a directory that an app can write to and read from.
16    Data,
17
18    /// This is a directory that an app can share with several other apps made
19    /// by the same developer. Each app in a group can write to and read from
20    /// this container.
21    Group(String),
22}
23
24impl Device {
25    /// Returns a path to the given container of an application with the given
26    /// bundle id.
27    pub fn get_app_container(&self, bundle_id: &str, container: &Container) -> Result<PathBuf> {
28        let container = match container {
29            Container::App => "app",
30            Container::Data => "data",
31            Container::Group(group) => group,
32        };
33
34        let output = self
35            .simctl()
36            .command("get_app_container")
37            .arg(&self.udid)
38            .arg(bundle_id)
39            .arg(container)
40            .stdout(Stdio::piped())
41            .output()?;
42
43        let output = output.validate_with_output()?;
44
45        Ok(Path::new(String::from_utf8(output.stdout)?.trim()).to_path_buf())
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use serial_test::serial;
52
53    use super::*;
54    use crate::mock;
55
56    #[test]
57    #[serial]
58    fn test_get_app_container() -> Result<()> {
59        mock::device()?.boot()?;
60
61        let path = mock::device()?.get_app_container("com.apple.mobilesafari", &Container::App)?;
62        assert!(path.ends_with(
63            "iOS.simruntime/Contents/Resources/RuntimeRoot/Applications/MobileSafari.app"
64        ));
65        let _ = mock::device()?.get_app_container("com.apple.mobilesafari", &Container::Data);
66
67        mock::device()?.shutdown()?;
68
69        Ok(())
70    }
71}