From 27838365a6841b0d3fa645ba2528221d23d4aeb2 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Thu, 27 Jun 2024 12:53:26 +0300 Subject: [PATCH] fix(cli): parse `--profile=` syntax (#10135) * fix(cli): parse `--profile=` syntax ref: https://github.com/tauri-apps/tauri/issues/6255#issuecomment-2192466839 * Update tooling/cli/src/interface/rust.rs * safe check next arg * add test --------- Co-authored-by: Lucas Nogueira --- .changes/cli-profile-parse-syntax.md | 6 +++ tooling/cli/src/interface/rust.rs | 77 ++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 .changes/cli-profile-parse-syntax.md diff --git a/.changes/cli-profile-parse-syntax.md b/.changes/cli-profile-parse-syntax.md new file mode 100644 index 000000000..592527877 --- /dev/null +++ b/.changes/cli-profile-parse-syntax.md @@ -0,0 +1,6 @@ +--- +"tauri-cli": "patch:bug" +"@tauri-apps/cli": "patch:bug" +--- + +Fix parsing of cargo profile when using `--profile=` syntax. diff --git a/tooling/cli/src/interface/rust.rs b/tooling/cli/src/interface/rust.rs index 3b6b94e10..c491f619a 100644 --- a/tooling/cli/src/interface/rust.rs +++ b/tooling/cli/src/interface/rust.rs @@ -1130,9 +1130,14 @@ pub fn get_profile(options: &Options) -> &str { options .args .iter() - .position(|a| a == "--profile") - .map(|i| options.args[i + 1].as_str()) - .unwrap_or_else(|| if options.debug { "debug" } else { "release" }) + .position(|a| a.starts_with("--profile")) + .and_then(|i| { + options.args[i] + .split_once('=') + .map(|(_, p)| Some(p)) + .unwrap_or_else(|| options.args.get(i + 1).map(|s| s.as_str())) + }) + .unwrap_or(if options.debug { "dev" } else { "release" }) } pub fn get_profile_dir(options: &Options) -> &str { @@ -1457,3 +1462,69 @@ mod pkgconfig_utils { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_profile_from_opts() { + let options = Options { + args: vec![ + "build".into(), + "--".into(), + "--profile".into(), + "testing".into(), + "--features".into(), + "feat1".into(), + ], + ..Default::default() + }; + assert_eq!(get_profile(&options), "testing"); + + let options = Options { + args: vec![ + "build".into(), + "--".into(), + "--profile=customprofile".into(), + "testing".into(), + "--features".into(), + "feat1".into(), + ], + ..Default::default() + }; + assert_eq!(get_profile(&options), "customprofile"); + + let options = Options { + debug: true, + args: vec![ + "build".into(), + "--".into(), + "testing".into(), + "--features".into(), + "feat1".into(), + ], + ..Default::default() + }; + assert_eq!(get_profile(&options), "dev"); + + let options = Options { + debug: false, + args: vec![ + "build".into(), + "--".into(), + "testing".into(), + "--features".into(), + "feat1".into(), + ], + ..Default::default() + }; + assert_eq!(get_profile(&options), "release"); + + let options = Options { + args: vec!["build".into(), "--".into(), "--profile".into()], + ..Default::default() + }; + assert_eq!(get_profile(&options), "release"); + } +}