Skip to content

Commit 6a241ab

Browse files
committed
Fix path() function where first arg is a drive letter to add separator
1 parent 2c0b681 commit 6a241ab

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

dsc_lib/src/functions/path.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@ impl Function for Path {
3131
debug!("Executing path function with args: {:?}", args);
3232

3333
let mut path = PathBuf::new();
34+
let mut first = true;
3435
for arg in args {
3536
if let Value::String(s) = arg {
36-
path.push(s);
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+
} else {
43+
path.push(s);
44+
}
3745
} else {
3846
return Err(DscError::Parser("Arguments must all be strings".to_string()));
3947
}
@@ -48,6 +56,22 @@ mod tests {
4856
use crate::configure::context::Context;
4957
use crate::parser::Statement;
5058

59+
#[test]
60+
fn start_with_drive_letter() {
61+
let mut parser = Statement::new().unwrap();
62+
let separator = std::path::MAIN_SEPARATOR;
63+
let result = parser.parse_and_execute("[path('C:','test')]", &Context::new()).unwrap();
64+
assert_eq!(result, format!("C:{separator}test"));
65+
}
66+
67+
#[test]
68+
fn drive_letter_in_middle() {
69+
let mut parser = Statement::new().unwrap();
70+
let separator = std::path::MAIN_SEPARATOR;
71+
let result = parser.parse_and_execute("[path('a','C:','test')]", &Context::new()).unwrap();
72+
assert_eq!(result, format!("a{separator}C:{separator}test"));
73+
}
74+
5175
#[test]
5276
fn two_args() {
5377
let mut parser = Statement::new().unwrap();

0 commit comments

Comments
 (0)