nushell/tests/command_ls_tests.rs
Yehuda Katz 34292b282a Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.

The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.

As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.

For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.

This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).

The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.

This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.

By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.

In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:03:24 -07:00

70 lines
1.9 KiB
Rust

mod helpers;
use h::{in_directory as cwd, Playground, Stub::*};
use helpers as h;
#[test]
fn ls_lists_regular_files() {
let sandbox = Playground::setup_for("ls_lists_files_test")
.with_files(vec![
EmptyFile("yehuda.10.txt"),
EmptyFile("jonathan.10.txt"),
EmptyFile("andres.10.txt"),
])
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
nu!(
output,
cwd(&full_path),
r#"ls | get name | lines | split-column "." | get Column2 | str Column2 --to-int | sum | echo $it"#
);
assert_eq!(output, "30");
}
#[test]
fn ls_lists_regular_files_using_asterisk_wildcard() {
let sandbox = Playground::setup_for("ls_asterisk_wildcard_test")
.with_files(vec![
EmptyFile("los.1.txt"),
EmptyFile("tres.1.txt"),
EmptyFile("amigos.1.txt"),
EmptyFile("arepas.1.clu"),
])
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
nu!(
output,
cwd(&full_path),
"ls *.txt | get name | lines| split-column \".\" | get Column2 | str Column2 --to-int | sum | echo $it"
);
assert_eq!(output, "3");
}
#[test]
fn ls_lists_regular_files_using_question_mark_wildcard() {
let sandbox = Playground::setup_for("ls_question_mark_wildcard_test")
.with_files(vec![
EmptyFile("yehuda.10.txt"),
EmptyFile("jonathan.10.txt"),
EmptyFile("andres.10.txt"),
EmptyFile("chicken_not_to_be_picked_up.100.txt"),
])
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
nu!(
output,
cwd(&full_path),
"ls *.??.txt | get name | lines| split-column \".\" | get Column2 | str Column2 --to-int | sum | echo $it"
);
assert_eq!(output, "30");
}