From 8d63099082ab5098f239897b6e51716f1e1a8513 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 16 Dec 2024 17:01:12 -0800 Subject: [PATCH] remove handling in `path()` and ensure `systemRoot()` has trailing separator --- dsc/src/subcommand.rs | 7 ++++++- dsc_lib/src/configure/context.rs | 4 ++-- dsc_lib/src/functions/path.rs | 15 ++++----------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/dsc/src/subcommand.rs b/dsc/src/subcommand.rs index 25046a8c..e9851424 100644 --- a/dsc/src/subcommand.rs +++ b/dsc/src/subcommand.rs @@ -341,7 +341,12 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, mounte exit(EXIT_INVALID_ARGS); } - configurator.set_system_root(path); + // make sure path has a trailing separator if it's a drive letter + if path.len() == 2 && path.chars().nth(1).unwrap_or(' ') == ':' { + configurator.set_system_root(&format!("{path}\\")); + } else { + configurator.set_system_root(path); + } } if let Err(err) = configurator.set_context(parameters.as_ref()) { diff --git a/dsc_lib/src/configure/context.rs b/dsc_lib/src/configure/context.rs index 49e90b65..a2a1e79f 100644 --- a/dsc_lib/src/configure/context.rs +++ b/dsc_lib/src/configure/context.rs @@ -45,9 +45,9 @@ impl Default for Context { #[cfg(target_os = "windows")] fn get_default_os_system_root() -> PathBuf { - // use SYSTEMDRIVE env var to get the default target path + // use SYSTEMDRIVE env var to get the default target path, append trailing separator let system_drive = std::env::var("SYSTEMDRIVE").unwrap_or_else(|_| "C:".to_string()); - PathBuf::from(system_drive) + PathBuf::from(system_drive + "\\") } #[cfg(not(target_os = "windows"))] diff --git a/dsc_lib/src/functions/path.rs b/dsc_lib/src/functions/path.rs index 40a2b872..09d2dcf2 100644 --- a/dsc_lib/src/functions/path.rs +++ b/dsc_lib/src/functions/path.rs @@ -31,15 +31,8 @@ impl Function for Path { debug!("Executing path function with args: {:?}", args); let mut path = PathBuf::new(); - let mut first = true; for arg in args { if let Value::String(s) = arg { - // if first argument is a drive letter, add it with a separator suffix as PathBuf.push() doesn't add it - if first && s.len() == 2 && s.chars().nth(1).unwrap() == ':' { - path.push(s.to_owned() + std::path::MAIN_SEPARATOR.to_string().as_str()); - first = false; - continue; - } path.push(s); } else { return Err(DscError::Parser("Arguments must all be strings".to_string())); @@ -60,14 +53,14 @@ mod tests { #[test] fn start_with_drive_letter() { let mut parser = Statement::new().unwrap(); - let result = parser.parse_and_execute("[path('C:','test')]", &Context::new()).unwrap(); + let result = parser.parse_and_execute("[path('C:\\','test')]", &Context::new()).unwrap(); assert_eq!(result, format!("C:{SEPARATOR}test")); } #[test] fn drive_letter_in_middle() { let mut parser = Statement::new().unwrap(); - let result = parser.parse_and_execute("[path('a','C:','test')]", &Context::new()).unwrap(); + let result = parser.parse_and_execute("[path('a','C:\\','test')]", &Context::new()).unwrap(); // if any part of the path is absolute, it replaces it instead of appending on Windows #[cfg(target_os = "windows")] @@ -81,11 +74,11 @@ mod tests { #[test] fn multiple_drive_letters() { let mut parser = Statement::new().unwrap(); - let result = parser.parse_and_execute("[path('C:','D:','test')]", &Context::new()).unwrap(); + let result = parser.parse_and_execute("[path('C:\\','D:\\','test')]", &Context::new()).unwrap(); // if any part of the path is absolute, it replaces it instead of appending on Windows #[cfg(target_os = "windows")] - assert_eq!(result, format!("D:test")); + assert_eq!(result, format!("D:\\test")); // non-Windows, the colon is a valid character in a path #[cfg(not(target_os = "windows"))]