Skip to content

Commit 8d63099

Browse files
committed
remove handling in path() and ensure systemRoot() has trailing separator
1 parent c49ef04 commit 8d63099

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

dsc/src/subcommand.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,12 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
341341
exit(EXIT_INVALID_ARGS);
342342
}
343343

344-
configurator.set_system_root(path);
344+
// make sure path has a trailing separator if it's a drive letter
345+
if path.len() == 2 && path.chars().nth(1).unwrap_or(' ') == ':' {
346+
configurator.set_system_root(&format!("{path}\\"));
347+
} else {
348+
configurator.set_system_root(path);
349+
}
345350
}
346351

347352
if let Err(err) = configurator.set_context(parameters.as_ref()) {

dsc_lib/src/configure/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ impl Default for Context {
4545

4646
#[cfg(target_os = "windows")]
4747
fn get_default_os_system_root() -> PathBuf {
48-
// use SYSTEMDRIVE env var to get the default target path
48+
// use SYSTEMDRIVE env var to get the default target path, append trailing separator
4949
let system_drive = std::env::var("SYSTEMDRIVE").unwrap_or_else(|_| "C:".to_string());
50-
PathBuf::from(system_drive)
50+
PathBuf::from(system_drive + "\\")
5151
}
5252

5353
#[cfg(not(target_os = "windows"))]

dsc_lib/src/functions/path.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,8 @@ impl Function for Path {
3131
debug!("Executing path function with args: {:?}", args);
3232

3333
let mut path = PathBuf::new();
34-
let mut first = true;
3534
for arg in args {
3635
if let Value::String(s) = arg {
37-
// if first argument is a drive letter, add it with a separator suffix as PathBuf.push() doesn't add it
38-
if first && s.len() == 2 && s.chars().nth(1).unwrap() == ':' {
39-
path.push(s.to_owned() + std::path::MAIN_SEPARATOR.to_string().as_str());
40-
first = false;
41-
continue;
42-
}
4336
path.push(s);
4437
} else {
4538
return Err(DscError::Parser("Arguments must all be strings".to_string()));
@@ -60,14 +53,14 @@ mod tests {
6053
#[test]
6154
fn start_with_drive_letter() {
6255
let mut parser = Statement::new().unwrap();
63-
let result = parser.parse_and_execute("[path('C:','test')]", &Context::new()).unwrap();
56+
let result = parser.parse_and_execute("[path('C:\\','test')]", &Context::new()).unwrap();
6457
assert_eq!(result, format!("C:{SEPARATOR}test"));
6558
}
6659

6760
#[test]
6861
fn drive_letter_in_middle() {
6962
let mut parser = Statement::new().unwrap();
70-
let result = parser.parse_and_execute("[path('a','C:','test')]", &Context::new()).unwrap();
63+
let result = parser.parse_and_execute("[path('a','C:\\','test')]", &Context::new()).unwrap();
7164

7265
// if any part of the path is absolute, it replaces it instead of appending on Windows
7366
#[cfg(target_os = "windows")]
@@ -81,11 +74,11 @@ mod tests {
8174
#[test]
8275
fn multiple_drive_letters() {
8376
let mut parser = Statement::new().unwrap();
84-
let result = parser.parse_and_execute("[path('C:','D:','test')]", &Context::new()).unwrap();
77+
let result = parser.parse_and_execute("[path('C:\\','D:\\','test')]", &Context::new()).unwrap();
8578

8679
// if any part of the path is absolute, it replaces it instead of appending on Windows
8780
#[cfg(target_os = "windows")]
88-
assert_eq!(result, format!("D:test"));
81+
assert_eq!(result, format!("D:\\test"));
8982

9083
// non-Windows, the colon is a valid character in a path
9184
#[cfg(not(target_os = "windows"))]

0 commit comments

Comments
 (0)