Skip to content

Commit

Permalink
remove handling in path() and ensure systemRoot() has trailing se…
Browse files Browse the repository at this point in the history
…parator
  • Loading branch information
SteveL-MSFT committed Dec 17, 2024
1 parent c49ef04 commit 8d63099
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
7 changes: 6 additions & 1 deletion dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,12 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, 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()) {
Expand Down
4 changes: 2 additions & 2 deletions dsc_lib/src/configure/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
15 changes: 4 additions & 11 deletions dsc_lib/src/functions/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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")]
Expand All @@ -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"))]
Expand Down

0 comments on commit 8d63099

Please sign in to comment.