Debian Codegraph @

third_party/rust/mach/ci/deploy_and_run_on_ios_simulator.rs

raw
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // This is a script to deploy and execute a binary on an iOS simulator.
12 // The primary use of this is to be able to run unit tests on the simulator and
13 // retrieve the results.
14 //
15 // To do this through Cargo instead, use Dinghy
16 // (https://github.com/snipsco/dinghy): cargo dinghy install, then cargo dinghy
17 // test.
18 //
19 // Source: this script is part of libc
20 // https://github.com/rust-lang/libc/blob/master/ci/ios/deploy_and_run_on_ios_simulator.rs
21 // and should be sync'ed with it when ci breaks (or periodically).
22
23 use std::env;
24 use std::fs::{self, File};
25 use std::io::Write;
26 use std::path::Path;
27 use std::process;
28 use std::process::Command;
29
30 macro_rules! t {
31 ($e:expr) => (match $e {
32 Ok(e) => e,
33 Err(e) => panic!("{} failed with: {}", stringify!($e), e),
34 })
35 }
36
37 // Step one: Wrap as an app
38 fn package_as_simulator_app(crate_name: &str, test_binary_path: &Path) {
39 println!("Packaging simulator app");
40 drop(fs::remove_dir_all("ios_simulator_app"));
41 t!(fs::create_dir("ios_simulator_app"));
42 t!(fs::copy(test_binary_path,
43 Path::new("ios_simulator_app").join(crate_name)));
44
45 let mut f = t!(File::create("ios_simulator_app/Info.plist"));
46 t!(f.write_all(format!(r#"
47 <?xml version="1.0" encoding="UTF-8"?>
48 <!DOCTYPE plist PUBLIC
49 "-//Apple//DTD PLIST 1.0//EN"
50 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
51 <plist version="1.0">
52 <dict>
53 <key>CFBundleExecutable</key>
54 <string>{}</string>
55 <key>CFBundleIdentifier</key>
56 <string>com.rust.unittests</string>
57 </dict>
58 </plist>
59 "#, crate_name).as_bytes()));
60 }
61
62 // Step two: Start the iOS simulator
63 fn start_simulator() {
64 println!("Looking for iOS simulator");
65 let output = t!(Command::new("xcrun").arg("simctl").arg("list").output());
66 assert!(output.status.success());
67 let mut simulator_exists = false;
68 let mut simulator_booted = false;
69 let mut found_rust_sim = false;
70 let stdout = t!(String::from_utf8(output.stdout));
71 for line in stdout.lines() {
72 if line.contains("rust_ios") {
73 if found_rust_sim {
74 panic!("Duplicate rust_ios simulators found. Please \
75 double-check xcrun simctl list.");
76 }
77 simulator_exists = true;
78 simulator_booted = line.contains("(Booted)");
79 found_rust_sim = true;
80 }
81 }
82
83 if simulator_exists == false {
84 println!("Creating iOS simulator");
85 Command::new("xcrun")
86 .arg("simctl")
87 .arg("create")
88 .arg("rust_ios")
89 .arg("com.apple.CoreSimulator.SimDeviceType.iPhone-SE")
90 .arg("com.apple.CoreSimulator.SimRuntime.iOS-10-2")
91 .check_status();
92 } else if simulator_booted == true {
93 println!("Shutting down already-booted simulator");
94 Command::new("xcrun")
95 .arg("simctl")
96 .arg("shutdown")
97 .arg("rust_ios")
98 .check_status();
99 }
100
101 println!("Starting iOS simulator");
102 // We can't uninstall the app (if present) as that will hang if the
103 // simulator isn't completely booted; just erase the simulator instead.
104 Command::new("xcrun").arg("simctl").arg("erase").arg("rust_ios").check_status();
105 Command::new("xcrun").arg("simctl").arg("boot").arg("rust_ios").check_status();
106 }
107
108 // Step three: Install the app
109 fn install_app_to_simulator() {
110 println!("Installing app to simulator");
111 Command::new("xcrun")
112 .arg("simctl")
113 .arg("install")
114 .arg("booted")
115 .arg("ios_simulator_app/")
116 .check_status();
117 }
118
119 // Step four: Run the app
120 fn run_app_on_simulator() {
121 println!("Running app");
122 let output = t!(Command::new("xcrun")
123 .arg("simctl")
124 .arg("launch")
125 .arg("--console")
126 .arg("booted")
127 .arg("com.rust.unittests")
128 .output());
129
130 println!("stdout --\n{}\n", String::from_utf8_lossy(&output.stdout));
131 println!("stderr --\n{}\n", String::from_utf8_lossy(&output.stderr));
132
133 let stdout = String::from_utf8_lossy(&output.stdout);
134 let passed = stdout.lines()
135 .find(|l| l.contains("PASSED"))
136 .map(|l| l.contains("tests"))
137 .unwrap_or(false);
138
139 println!("Shutting down simulator");
140 Command::new("xcrun")
141 .arg("simctl")
142 .arg("shutdown")
143 .arg("rust_ios")
144 .check_status();
145 if !passed {
146 panic!("tests didn't pass");
147 }
148 }
149
150 trait CheckStatus {
151 fn check_status(&mut self);
152 }
153
154 impl CheckStatus for Command {
155 fn check_status(&mut self) {
156 println!("\trunning: {:?}", self);
157 assert!(t!(self.status()).success());
158 }
159 }
160
161 fn main() {
162 let args: Vec<String> = env::args().collect();
163 if args.len() != 2 {
164 println!("Usage: {} <executable>", args[0]);
165 process::exit(-1);
166 }
167
168 let test_binary_path = Path::new(&args[1]);
169 let crate_name = test_binary_path.file_name().unwrap();
170
171 package_as_simulator_app(crate_name.to_str().unwrap(), test_binary_path);
172 start_simulator();
173 install_app_to_simulator();
174 run_app_on_simulator();
175 }