From 5e8ce09c707e65ee0613a111b4dd59a2372d695d Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 6 Jan 2025 11:27:10 -0600 Subject: [PATCH] `cppwinrt` should consistently panic on failure (#3415) --- crates/libs/cppwinrt/readme.md | 5 +---- crates/libs/cppwinrt/src/lib.rs | 19 +++++++++++-------- .../json_validator_winrt_client_cpp/build.rs | 3 +-- crates/tests/winrt/composable_client/build.rs | 3 +-- .../tests/winrt/constructors_client/build.rs | 3 +-- crates/tests/winrt/noexcept/build.rs | 3 +-- crates/tests/winrt/ref_params/build.rs | 3 +-- 7 files changed, 17 insertions(+), 22 deletions(-) diff --git a/crates/libs/cppwinrt/readme.md b/crates/libs/cppwinrt/readme.md index c05bcdcdba..3ee0068d6b 100644 --- a/crates/libs/cppwinrt/readme.md +++ b/crates/libs/cppwinrt/readme.md @@ -16,10 +16,7 @@ version = "0.1" Use `cppwinrt` function as needed: ```rust -match cppwinrt::cppwinrt(["-help"]) { - Ok(output) => println!("{output}"), - Err(error) => println!("{error}"), -}; +println!("{}", cppwinrt::cppwinrt(["-help"])); ``` Source: diff --git a/crates/libs/cppwinrt/src/lib.rs b/crates/libs/cppwinrt/src/lib.rs index 2e993dd825..de0d743239 100644 --- a/crates/libs/cppwinrt/src/lib.rs +++ b/crates/libs/cppwinrt/src/lib.rs @@ -6,7 +6,7 @@ const VERSION: &str = "2.0.240405.15"; /// Calls the C++/WinRT compiler with the given arguments. /// /// Use `cppwinrt["-help"]` for available options. -pub fn cppwinrt(args: I) -> Result +pub fn cppwinrt(args: I) -> String where I: IntoIterator, S: AsRef, @@ -27,9 +27,9 @@ where _ = std::fs::remove_file(path); if output.status.success() { - Ok(String::from_utf8_lossy(&output.stdout).to_string()) + String::from_utf8_lossy(&output.stdout).to_string() } else { - Err(String::from_utf8_lossy(&output.stderr).to_string()) + panic!("{}", String::from_utf8_lossy(&output.stderr)) } } @@ -38,11 +38,14 @@ mod tests { use crate::*; #[test] - fn test() { - let ok = cppwinrt(["-help"]).unwrap(); - assert!(ok.contains(VERSION), "unexpected version"); + #[should_panic(expected = "'-invalid' is not supported")] + fn invalid_arg() { + cppwinrt(["-invalid"]); + } - let err = cppwinrt(["-invalid"]).unwrap_err(); - assert!(err.contains("'-invalid' is not supported")); + #[test] + fn unexpected_version() { + let ok = cppwinrt(["-help"]); + assert!(ok.contains(VERSION), "unexpected version"); } } diff --git a/crates/samples/components/json_validator_winrt_client_cpp/build.rs b/crates/samples/components/json_validator_winrt_client_cpp/build.rs index fc4c5998ab..0e63b374b8 100644 --- a/crates/samples/components/json_validator_winrt_client_cpp/build.rs +++ b/crates/samples/components/json_validator_winrt_client_cpp/build.rs @@ -14,8 +14,7 @@ fn main() { &format!("{}\\System32\\WinMetadata", env!("windir")), "-out", &include, - ]) - .unwrap(); + ]); cc::Build::new() .cpp(true) diff --git a/crates/tests/winrt/composable_client/build.rs b/crates/tests/winrt/composable_client/build.rs index 6a01e1b9d4..eb297b0e2b 100644 --- a/crates/tests/winrt/composable_client/build.rs +++ b/crates/tests/winrt/composable_client/build.rs @@ -26,8 +26,7 @@ fn main() { "../../../libs/bindgen/default", "-out", &include, - ]) - .unwrap(); + ]); cc::Build::new() .cpp(true) diff --git a/crates/tests/winrt/constructors_client/build.rs b/crates/tests/winrt/constructors_client/build.rs index 02ac560d53..854f0a05b2 100644 --- a/crates/tests/winrt/constructors_client/build.rs +++ b/crates/tests/winrt/constructors_client/build.rs @@ -26,8 +26,7 @@ fn main() { "../../../libs/bindgen/default", "-out", &include, - ]) - .unwrap(); + ]); cc::Build::new() .cpp(true) diff --git a/crates/tests/winrt/noexcept/build.rs b/crates/tests/winrt/noexcept/build.rs index 0a464c84d6..525568f26b 100644 --- a/crates/tests/winrt/noexcept/build.rs +++ b/crates/tests/winrt/noexcept/build.rs @@ -47,8 +47,7 @@ fn main() { &format!("{}\\System32\\WinMetadata", env!("windir")), "-out", &include, - ]) - .unwrap(); + ]); cc::Build::new() .cpp(true) diff --git a/crates/tests/winrt/ref_params/build.rs b/crates/tests/winrt/ref_params/build.rs index 0a464c84d6..525568f26b 100644 --- a/crates/tests/winrt/ref_params/build.rs +++ b/crates/tests/winrt/ref_params/build.rs @@ -47,8 +47,7 @@ fn main() { &format!("{}\\System32\\WinMetadata", env!("windir")), "-out", &include, - ]) - .unwrap(); + ]); cc::Build::new() .cpp(true)