creator_simctl/
getenv.rs

1use std::process::Stdio;
2
3use super::{Device, Result, Validate};
4
5impl Device {
6    /// Returns a local environment variable with the given name. Do not prepend
7    /// `SIMCTL_CHILD_` to the variable name. If no variable with the given name
8    /// exists, this function will return an empty string (and no error).
9    pub fn getenv(&self, name: &str) -> Result<String> {
10        let output = self
11            .simctl()
12            .command("getenv")
13            .arg(&self.udid)
14            .arg(&name)
15            .stdout(Stdio::piped())
16            .output()?;
17
18        let output = output.validate_with_output()?;
19
20        Ok(String::from_utf8(output.stdout)?.trim().to_owned())
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use serial_test::serial;
27
28    use super::*;
29    use crate::mock;
30
31    #[test]
32    #[serial]
33    fn test_getenv() -> Result<()> {
34        mock::device()?.boot_with_env(vec![("TEST_VAR", "Hello World!")])?;
35        assert_eq!(mock::device()?.getenv("TEST_VAR")?, "Hello World!");
36        assert_eq!(mock::device()?.getenv("TEST_VAR_")?, "");
37        mock::device()?.shutdown()?;
38
39        Ok(())
40    }
41}