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:
Jakub Žádník
2023-08-26 16:41:29 +03:00
committed by GitHub
parent 3d73287ea4
commit 5ac5b90aed
37 changed files with 849 additions and 161 deletions

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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::*;

View File

@ -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);
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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");
})
}