Skip to content

Commit 0558cd8

Browse files
committed
ios: during testing, crash out early if build fails
1 parent 1a21e23 commit 0558cd8

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

tests/test_ios.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod tests {
88
use super::common::check_request_received_using;
99
use std::fs;
1010
use std::path::PathBuf;
11-
use std::process::{Command, Stdio};
11+
use std::process::{Command, ExitStatus, Stdio};
1212
use webbrowser::Browser;
1313

1414
// to run this test, run it as:
@@ -27,7 +27,7 @@ mod tests {
2727
// build test glue code
2828
let mut glue_dir = PathBuf::from(&app_dir);
2929
glue_dir.push("testglue");
30-
run_cmd(&glue_dir, "glue code build failed", &["./build"]);
30+
run_cmd(&glue_dir, &["./build"]).expect("glue code build failed");
3131

3232
// invoke server
3333
check_request_received_using(uri, &ipv4, |url, _port| {
@@ -48,42 +48,58 @@ mod tests {
4848
.collect::<Vec<String>>()
4949
.join("\n");
5050
fs::write(&swift_src, new_code).expect("failed to modify ContentView.swift");
51+
let revert_code = || fs::write(&swift_src, &old_code).expect("failed to revert code");
52+
let handle_exec_result = |result: std::io::Result<ExitStatus>, err_msg: &str| {
53+
revert_code();
54+
let success = match result {
55+
Ok(status) => status.success(),
56+
Err(_) => false,
57+
};
58+
if !success {
59+
eprintln!("{err_msg}");
60+
std::process::exit(1);
61+
}
62+
};
5163

5264
// build app
53-
run_cmd(
65+
let exec_result = run_cmd(
5466
&app_dir,
55-
"failed to build ios app",
5667
&[
5768
"xcrun",
5869
"xcodebuild",
5970
"-project",
6071
"test-ios-app.xcodeproj",
61-
"-scheme",
62-
"test-ios-app",
6372
"-configuration",
6473
"Debug",
74+
"-sdk",
75+
"iphonesimulator",
6576
"-destination",
6677
"platform=iOS Simulator,name=iphone-latest",
67-
"-derivedDataPath",
68-
"build",
78+
"-arch",
79+
if cfg!(target_arch = "aarch64") {
80+
"arm64"
81+
} else {
82+
"x86_64"
83+
},
6984
],
7085
);
86+
handle_exec_result(exec_result, "failed to build ios app");
7187

7288
// launch app on simulator
73-
run_cmd(
89+
let exec_result = run_cmd(
7490
&app_dir,
75-
"failed to install app on simulator",
7691
&[
7792
"xcrun",
7893
"simctl",
7994
"install",
8095
"booted",
81-
"build/Build/Products/Debug-iphonesimulator/test-ios-app.app",
96+
"build/Debug-iphonesimulator/test-ios-app.app",
8297
],
8398
);
84-
run_cmd(
99+
handle_exec_result(exec_result, "failed to install app on simulator");
100+
101+
let exec_result = run_cmd(
85102
&app_dir,
86-
"failed to launch app on simulator",
87103
&[
88104
"xcrun",
89105
"simctl",
@@ -92,9 +108,10 @@ mod tests {
92108
"in.rootnet.webbrowser.test-ios-app",
93109
],
94110
);
111+
handle_exec_result(exec_result, "failed to launch app on simulator");
95112

96113
// revert to the old code
97-
fs::write(&swift_src, &old_code).expect("failed to modify ContentView.swift");
114+
revert_code();
98115
})
99116
.await;
100117
}
@@ -118,13 +135,12 @@ mod tests {
118135
assert!(Browser::is_available(), "should have found a browser");
119136
}
120137

121-
fn run_cmd(app_dir: &PathBuf, failure_msg: &str, args: &[&str]) {
122-
let _ = Command::new(args[0])
138+
fn run_cmd(app_dir: &PathBuf, args: &[&str]) -> std::io::Result<ExitStatus> {
139+
Command::new(args[0])
123140
.args(&args[1..])
124141
.stdout(Stdio::inherit())
125142
.stderr(Stdio::inherit())
126143
.current_dir(app_dir)
127144
.status()
128-
.expect(failure_msg);
129145
}
130146
}

0 commit comments

Comments
 (0)