Skip to content

Commit

Permalink
Fix path() function where first arg is a drive letter to add separator
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveL-MSFT committed Dec 15, 2024
1 parent 2c0b681 commit 6a241ab
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion dsc_lib/src/functions/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ 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 {
path.push(s);
// 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;
} else {
path.push(s);
}
} else {
return Err(DscError::Parser("Arguments must all be strings".to_string()));
}
Expand All @@ -48,6 +56,22 @@ mod tests {
use crate::configure::context::Context;
use crate::parser::Statement;

#[test]
fn start_with_drive_letter() {
let mut parser = Statement::new().unwrap();
let separator = std::path::MAIN_SEPARATOR;
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 separator = std::path::MAIN_SEPARATOR;
let result = parser.parse_and_execute("[path('a','C:','test')]", &Context::new()).unwrap();
assert_eq!(result, format!("a{separator}C:{separator}test"));
}

#[test]
fn two_args() {
let mut parser = Statement::new().unwrap();
Expand Down

0 comments on commit 6a241ab

Please sign in to comment.