forked from extern/nushell
Allow parse-time evaluation of calls, pipelines and subexpressions (#9499)
Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
This commit is contained in:
@ -3,7 +3,7 @@ use std::path::Path;
|
||||
use super::PathSubcommandArguments;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::{
|
||||
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
|
||||
SyntaxShape, Type, Value,
|
||||
@ -45,6 +45,10 @@ impl Command for SubCommand {
|
||||
"Get the final component of a path."
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
@ -67,6 +71,27 @@ impl Command for SubCommand {
|
||||
)
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let args = Arguments {
|
||||
replace: call.get_flag_const(working_set, "replace")?,
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&get_basename, &args, value, head),
|
||||
working_set.permanent().ctrlc.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
|
@ -2,7 +2,7 @@ use std::path::Path;
|
||||
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::{
|
||||
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
|
||||
SyntaxShape, Type, Value,
|
||||
@ -53,6 +53,10 @@ impl Command for SubCommand {
|
||||
"Get the parent directory of a path."
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
@ -76,6 +80,28 @@ impl Command for SubCommand {
|
||||
)
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let args = Arguments {
|
||||
replace: call.get_flag_const(working_set, "replace")?,
|
||||
num_levels: call.get_flag_const(working_set, "num-levels")?,
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&get_dirname, &args, value, head),
|
||||
working_set.permanent().ctrlc.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
|
@ -1,9 +1,9 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use nu_engine::current_dir;
|
||||
use nu_engine::{current_dir, current_dir_const};
|
||||
use nu_path::expand_path_with;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::{
|
||||
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
@ -45,6 +45,10 @@ impl Command for SubCommand {
|
||||
If you need to distinguish dirs and files, please use `path type`."#
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
@ -66,6 +70,26 @@ If you need to distinguish dirs and files, please use `path type`."#
|
||||
)
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let args = Arguments {
|
||||
pwd: current_dir_const(working_set)?,
|
||||
};
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&exists, &args, value, head),
|
||||
working_set.permanent().ctrlc.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
|
@ -1,9 +1,9 @@
|
||||
use std::path::Path;
|
||||
|
||||
use nu_engine::env::current_dir_str;
|
||||
use nu_engine::env::{current_dir_str, current_dir_str_const};
|
||||
use nu_path::{canonicalize_with, expand_path_with};
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::{
|
||||
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
@ -48,6 +48,10 @@ impl Command for SubCommand {
|
||||
"Try to expand a path to its absolute form."
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
@ -71,6 +75,28 @@ impl Command for SubCommand {
|
||||
)
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let args = Arguments {
|
||||
strict: call.has_flag("strict"),
|
||||
cwd: current_dir_str_const(working_set)?,
|
||||
not_follow_symlink: call.has_flag("no-symlink"),
|
||||
};
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&expand, &args, value, head),
|
||||
working_set.permanent().ctrlc.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
|
@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::{
|
||||
engine::Command, Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned,
|
||||
SyntaxShape, Type, Value,
|
||||
@ -46,6 +46,10 @@ impl Command for SubCommand {
|
||||
the output of 'path parse' and 'path split' subcommands."#
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
@ -53,29 +57,24 @@ the output of 'path parse' and 'path split' subcommands."#
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let args = Arguments {
|
||||
append: call.rest(engine_state, stack, 0)?,
|
||||
};
|
||||
|
||||
let metadata = input.metadata();
|
||||
run(call, &args, input)
|
||||
}
|
||||
|
||||
match input {
|
||||
PipelineData::Value(val, md) => {
|
||||
Ok(PipelineData::Value(handle_value(val, &args, head), md))
|
||||
}
|
||||
PipelineData::ListStream(..) => Ok(PipelineData::Value(
|
||||
handle_value(input.into_value(head), &args, head),
|
||||
metadata,
|
||||
)),
|
||||
PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty { dst_span: head }),
|
||||
_ => Err(ShellError::UnsupportedInput(
|
||||
"Input value cannot be joined".to_string(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
input.span().unwrap_or(call.head),
|
||||
)),
|
||||
}
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let args = Arguments {
|
||||
append: call.rest_const(working_set, 0)?,
|
||||
};
|
||||
|
||||
run(call, &args, input)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
@ -147,6 +146,27 @@ the output of 'path parse' and 'path split' subcommands."#
|
||||
}
|
||||
}
|
||||
|
||||
fn run(call: &Call, args: &Arguments, input: PipelineData) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
|
||||
let metadata = input.metadata();
|
||||
|
||||
match input {
|
||||
PipelineData::Value(val, md) => Ok(PipelineData::Value(handle_value(val, args, head), md)),
|
||||
PipelineData::ListStream(..) => Ok(PipelineData::Value(
|
||||
handle_value(input.into_value(head), args, head),
|
||||
metadata,
|
||||
)),
|
||||
PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty { dst_span: head }),
|
||||
_ => Err(ShellError::UnsupportedInput(
|
||||
"Input value cannot be joined".to_string(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
input.span().unwrap_or(call.head),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_value(v: Value, args: &Arguments, head: Span) -> Value {
|
||||
match v {
|
||||
Value::String { ref val, .. } => join_single(Path::new(val), head, args),
|
||||
|
@ -2,7 +2,7 @@ use std::path::Path;
|
||||
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::{
|
||||
engine::Command, Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned,
|
||||
SyntaxShape, Type, Value,
|
||||
@ -48,6 +48,10 @@ impl Command for SubCommand {
|
||||
On Windows, an extra 'prefix' column is added."#
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
@ -70,6 +74,27 @@ On Windows, an extra 'prefix' column is added."#
|
||||
)
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let args = Arguments {
|
||||
extension: call.get_flag_const(working_set, "extension")?,
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&parse, &args, value, head),
|
||||
working_set.permanent().ctrlc.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
|
@ -3,7 +3,7 @@ use std::path::Path;
|
||||
use nu_engine::CallExt;
|
||||
use nu_path::expand_to_real_path;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::{
|
||||
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
|
||||
SyntaxShape, Type, Value,
|
||||
@ -52,6 +52,10 @@ absolute or both relative. The argument path needs to be a parent of the input
|
||||
path."#
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
@ -74,6 +78,27 @@ path."#
|
||||
)
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let args = Arguments {
|
||||
path: call.req_const(working_set, 0)?,
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&relative_to, &args, value, head),
|
||||
working_set.permanent().ctrlc.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::path::{Component, Path};
|
||||
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::{
|
||||
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
@ -36,6 +36,10 @@ impl Command for SubCommand {
|
||||
"Split a path into a list based on the system's path separator."
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
@ -56,6 +60,25 @@ impl Command for SubCommand {
|
||||
)
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let args = Arguments;
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&split, &args, value, head),
|
||||
working_set.permanent().ctrlc.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
|
@ -2,7 +2,7 @@ use std::path::Path;
|
||||
|
||||
use nu_path::expand_tilde;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::{
|
||||
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
@ -43,6 +43,10 @@ impl Command for SubCommand {
|
||||
If nothing is found, an empty string will be returned."#
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
@ -63,6 +67,25 @@ If nothing is found, an empty string will be returned."#
|
||||
)
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let args = Arguments;
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&r#type, &args, value, head),
|
||||
working_set.permanent().ctrlc.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
|
@ -3,7 +3,7 @@ use nu_cmd_base::input_handler::{operate, CmdArgument};
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::ast::CellPath;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
|
||||
use nu_protocol::Category;
|
||||
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
@ -62,6 +62,10 @@ impl Command for SubCommand {
|
||||
vec!["size", "count"]
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
@ -70,11 +74,17 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
||||
let args = Arguments {
|
||||
cell_paths: (!cell_paths.is_empty()).then_some(cell_paths),
|
||||
graphemes: grapheme_flags(call)?,
|
||||
};
|
||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
run(cell_paths, engine_state, call, input)
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let cell_paths: Vec<CellPath> = call.rest_const(working_set, 0)?;
|
||||
run(cell_paths, working_set.permanent(), call, input)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
@ -101,6 +111,19 @@ impl Command for SubCommand {
|
||||
}
|
||||
}
|
||||
|
||||
fn run(
|
||||
cell_paths: Vec<CellPath>,
|
||||
engine_state: &EngineState,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let args = Arguments {
|
||||
cell_paths: (!cell_paths.is_empty()).then_some(cell_paths),
|
||||
graphemes: grapheme_flags(call)?,
|
||||
};
|
||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||
}
|
||||
|
||||
fn action(input: &Value, arg: &Arguments, head: Span) -> Value {
|
||||
match input {
|
||||
Value::String { val, .. } => Value::int(
|
||||
|
@ -34,3 +34,9 @@ fn echo_range_handles_exclusive_down() {
|
||||
|
||||
assert_eq!(actual.out, "[3,2]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn echo_const() {
|
||||
let actual = nu!("const x = (echo spam); $x");
|
||||
assert_eq!(actual.out, "spam");
|
||||
}
|
||||
|
@ -81,3 +81,9 @@ fn replaces_basename_of_path_ending_with_double_dot() {
|
||||
let expected = join_path_sep(&["some/file.txt/..", "eggs"]);
|
||||
assert_eq!(actual.out, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn const_path_basename() {
|
||||
let actual = nu!("const name = ('spam/eggs.txt' | path basename); $name");
|
||||
assert_eq!(actual.out, "eggs.txt");
|
||||
}
|
||||
|
@ -135,3 +135,9 @@ fn replaces_dirname_of_way_too_many_levels() {
|
||||
let expected = join_path_sep(&["eggs", "some/dir/with/spam.txt"]);
|
||||
assert_eq!(actual.out, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn const_path_dirname() {
|
||||
let actual = nu!("const name = ('spam/eggs.txt' | path dirname); $name");
|
||||
assert_eq!(actual.out, "spam");
|
||||
}
|
||||
|
@ -57,3 +57,9 @@ fn checks_tilde_relative_path_exists() {
|
||||
let actual = nu!("'~' | path exists");
|
||||
assert_eq!(actual.out, "true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn const_path_exists() {
|
||||
let actual = nu!("const exists = ('~' | path exists); $exists");
|
||||
assert_eq!(actual.out, "true");
|
||||
}
|
||||
|
@ -66,6 +66,26 @@ fn expands_path_with_double_dot() {
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn const_path_expand() {
|
||||
Playground::setup("const_path_expand", |dirs, sandbox| {
|
||||
sandbox
|
||||
.within("menu")
|
||||
.with_files(vec![EmptyFile("spam.txt")]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
const result = ("menu/./spam.txt" | path expand);
|
||||
$result
|
||||
"#
|
||||
));
|
||||
|
||||
let expected = dirs.test.join("menu").join("spam.txt");
|
||||
assert_eq!(PathBuf::from(actual.out), expected);
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
mod windows {
|
||||
use super::*;
|
||||
|
@ -54,3 +54,10 @@ fn returns_joined_path_when_joining_empty_path() {
|
||||
|
||||
assert_eq!(actual.out, "foo.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn const_path_join() {
|
||||
let actual = nu!("const name = ('spam' | path join 'eggs.txt'); $name");
|
||||
let expected = join_path_sep(&["spam", "eggs.txt"]);
|
||||
assert_eq!(actual.out, expected);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ mod parse;
|
||||
mod split;
|
||||
mod type_;
|
||||
|
||||
use nu_test_support::{nu, pipeline};
|
||||
use std::path::MAIN_SEPARATOR;
|
||||
|
||||
/// Helper function that joins string literals with '/' or '\', based on host OS
|
||||
@ -32,3 +33,9 @@ fn joins_path_on_other_than_windows() {
|
||||
|
||||
assert_eq!(&actual, "sausage/bacon/spam");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn const_path_relative_to() {
|
||||
let actual = nu!("'/home/viking' | path relative-to '/home'");
|
||||
assert_eq!(actual.out, "viking");
|
||||
}
|
||||
|
@ -119,3 +119,15 @@ fn parses_into_correct_number_of_columns() {
|
||||
|
||||
assert_eq!(actual.out, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn const_path_parse() {
|
||||
let actual = nu!("const name = ('spam/eggs.txt' | path parse); $name.parent");
|
||||
assert_eq!(actual.out, "spam");
|
||||
|
||||
let actual = nu!("const name = ('spam/eggs.txt' | path parse); $name.stem");
|
||||
assert_eq!(actual.out, "eggs");
|
||||
|
||||
let actual = nu!("const name = ('spam/eggs.txt' | path parse); $name.extension");
|
||||
assert_eq!(actual.out, "txt");
|
||||
}
|
||||
|
@ -25,3 +25,13 @@ fn splits_correctly_single_path() {
|
||||
|
||||
assert_eq!(actual.out, "spam.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn splits_correctly_single_path_const() {
|
||||
let actual = nu!(r#"
|
||||
const result = ('home/viking/spam.txt' | path split);
|
||||
$result | last
|
||||
"#);
|
||||
|
||||
assert_eq!(actual.out, "spam.txt");
|
||||
}
|
||||
|
@ -61,3 +61,22 @@ fn returns_type_of_existing_directory() {
|
||||
assert_eq!(actual.out, "dir");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_type_of_existing_file_const() {
|
||||
Playground::setup("path_type_const", |dirs, sandbox| {
|
||||
sandbox
|
||||
.within("menu")
|
||||
.with_files(vec![EmptyFile("spam.txt")]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
const ty = ("menu" | path type);
|
||||
$ty
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "dir");
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user