-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #589 from SteveL-MSFT/mounted-path
Add `--system-root` parameter, `systemRoot()` and `path()` functions to dsc
- Loading branch information
Showing
10 changed files
with
224 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use crate::DscError; | ||
use crate::configure::context::Context; | ||
use crate::functions::{AcceptedArgKind, Function}; | ||
use serde_json::Value; | ||
use std::path::PathBuf; | ||
use tracing::debug; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Path {} | ||
|
||
/// Implements the `path` function. | ||
/// Accepts a variable number of arguments, each of which is a string. | ||
/// Returns a string that is the concatenation of the arguments, separated by the platform's path separator. | ||
impl Function for Path { | ||
fn min_args(&self) -> usize { | ||
2 | ||
} | ||
|
||
fn max_args(&self) -> usize { | ||
usize::MAX | ||
} | ||
|
||
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { | ||
vec![AcceptedArgKind::String] | ||
} | ||
|
||
fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { | ||
debug!("Executing path function with args: {:?}", args); | ||
|
||
let mut path = PathBuf::new(); | ||
for arg in args { | ||
if let Value::String(s) = arg { | ||
path.push(s); | ||
} else { | ||
return Err(DscError::Parser("Arguments must all be strings".to_string())); | ||
} | ||
} | ||
|
||
Ok(Value::String(path.to_string_lossy().to_string())) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::configure::context::Context; | ||
use crate::parser::Statement; | ||
|
||
#[test] | ||
fn two_args() { | ||
let mut parser = Statement::new().unwrap(); | ||
let separator = std::path::MAIN_SEPARATOR; | ||
let result = parser.parse_and_execute("[path('a','b')]", &Context::new()).unwrap(); | ||
assert_eq!(result, format!("a{separator}b")); | ||
} | ||
|
||
#[test] | ||
fn three_args() { | ||
let mut parser = Statement::new().unwrap(); | ||
let separator = std::path::MAIN_SEPARATOR; | ||
let result = parser.parse_and_execute("[path('a','b','c')]", &Context::new()).unwrap(); | ||
assert_eq!(result, format!("a{separator}b{separator}c")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use crate::DscError; | ||
use crate::configure::context::Context; | ||
use crate::functions::{AcceptedArgKind, Function}; | ||
use serde_json::Value; | ||
use tracing::debug; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct SystemRoot {} | ||
|
||
/// Implements the `systemRoot` function. | ||
/// This function returns the value of the specified system root path. | ||
impl Function for SystemRoot { | ||
fn min_args(&self) -> usize { | ||
0 | ||
} | ||
|
||
fn max_args(&self) -> usize { | ||
0 | ||
} | ||
|
||
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { | ||
vec![AcceptedArgKind::String] | ||
} | ||
|
||
fn invoke(&self, _args: &[Value], context: &Context) -> Result<Value, DscError> { | ||
debug!("Executing targetPath function"); | ||
|
||
Ok(Value::String(context.system_root.to_string_lossy().to_string())) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::configure::context::Context; | ||
use crate::parser::Statement; | ||
use std::path::PathBuf; | ||
|
||
#[test] | ||
fn init() { | ||
let mut parser = Statement::new().unwrap(); | ||
let result = parser.parse_and_execute("[systemRoot()]", &Context::new()).unwrap(); | ||
// on windows, the default is SYSTEMDRIVE env var | ||
#[cfg(target_os = "windows")] | ||
assert_eq!(result, std::env::var("SYSTEMDRIVE").unwrap()); | ||
// on linux/macOS, the default is / | ||
#[cfg(not(target_os = "windows"))] | ||
assert_eq!(result, "/"); | ||
} | ||
|
||
#[test] | ||
fn simple() { | ||
let mut parser = Statement::new().unwrap(); | ||
let mut context = Context::new(); | ||
let separator = std::path::MAIN_SEPARATOR; | ||
context.system_root = PathBuf::from(format!("{separator}mnt")); | ||
let result = parser.parse_and_execute("[systemRoot()]", &context).unwrap(); | ||
assert_eq!(result, format!("{separator}mnt")); | ||
} | ||
|
||
} |