diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-09-22 11:49:27 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-22 11:49:27 -0400 |
commit | 0eda3fcd12244431bb7a3e7e4c85e63d93c3cddb (patch) | |
tree | 14bcebf26c1a7ba5cf32aeea62c59506845606ec /cli/tools/standalone.rs | |
parent | 716005a0d4afd1042fa75d8bdc32fd13e9ebe95f (diff) |
fix(compile): keep non-exe extension in output name on Windows (#15994)
Diffstat (limited to 'cli/tools/standalone.rs')
-rw-r--r-- | cli/tools/standalone.rs | 64 |
1 files changed, 48 insertions, 16 deletions
diff --git a/cli/tools/standalone.rs b/cli/tools/standalone.rs index 35170b6b9..03118f4b6 100644 --- a/cli/tools/standalone.rs +++ b/cli/tools/standalone.rs @@ -308,25 +308,30 @@ pub fn resolve_compile_executable_output_path( }).ok_or_else(|| generic_error( "An executable name was not provided. One could not be inferred from the URL. Aborting.", )).map(|output| { - match &compile_flags.target { - Some(target) => { - if target.contains("windows") { - output.with_extension("exe") - } else { - output - } - } - None => { - if cfg!(windows) && output.extension().unwrap_or_default() != "exe" { - output.with_extension("exe") - } else { - output - } - } - } + get_os_specific_filepath(output, &compile_flags.target) }) } +fn get_os_specific_filepath( + output: PathBuf, + target: &Option<String>, +) -> PathBuf { + let is_windows = match target { + Some(target) => target.contains("windows"), + None => cfg!(windows), + }; + if is_windows && output.extension().unwrap_or_default() != "exe" { + if let Some(ext) = output.extension() { + // keep version in my-exe-0.1.0 -> my-exe-0.1.0.exe + output.with_extension(format!("{}.exe", ext.to_string_lossy())) + } else { + output.with_extension("exe") + } + } else { + output + } +} + #[cfg(test)] mod test { pub use super::*; @@ -358,4 +363,31 @@ mod test { .unwrap(); assert_eq!(path.file_name().unwrap(), "file.exe"); } + + #[test] + fn test_os_specific_file_path() { + fn run_test(path: &str, target: Option<&str>, expected: &str) { + assert_eq!( + get_os_specific_filepath( + PathBuf::from(path), + &target.map(|s| s.to_string()) + ), + PathBuf::from(expected) + ); + } + + if cfg!(windows) { + run_test("C:\\my-exe", None, "C:\\my-exe.exe"); + run_test("C:\\my-exe.exe", None, "C:\\my-exe.exe"); + run_test("C:\\my-exe-0.1.2", None, "C:\\my-exe-0.1.2.exe"); + } else { + run_test("my-exe", Some("linux"), "my-exe"); + run_test("my-exe-0.1.2", Some("linux"), "my-exe-0.1.2"); + } + + run_test("C:\\my-exe", Some("windows"), "C:\\my-exe.exe"); + run_test("C:\\my-exe.exe", Some("windows"), "C:\\my-exe.exe"); + run_test("C:\\my-exe.0.1.2", Some("windows"), "C:\\my-exe.0.1.2.exe"); + run_test("my-exe-0.1.2", Some("linux"), "my-exe-0.1.2"); + } } |