2019-07-17 21:51:18 +02:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2019-08-01 23:00:08 +02:00
|
|
|
use glob::glob;
|
|
|
|
pub use std::path::Path;
|
2019-07-17 21:51:18 +02:00
|
|
|
pub use std::path::PathBuf;
|
2019-07-16 12:28:55 +02:00
|
|
|
|
2019-09-10 12:08:01 +02:00
|
|
|
use app_dirs::{get_app_root, AppDataType};
|
2019-08-28 19:01:16 +02:00
|
|
|
use getset::Getters;
|
2019-07-21 09:08:05 +02:00
|
|
|
use std::io::Read;
|
2019-08-28 19:01:16 +02:00
|
|
|
use tempfile::{tempdir, TempDir};
|
|
|
|
|
|
|
|
pub trait DisplayPath {
|
|
|
|
fn display_path(&self) -> String;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DisplayPath for PathBuf {
|
|
|
|
fn display_path(&self) -> String {
|
|
|
|
self.display().to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DisplayPath for str {
|
|
|
|
fn display_path(&self) -> String {
|
|
|
|
self.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DisplayPath for &str {
|
|
|
|
fn display_path(&self) -> String {
|
|
|
|
self.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DisplayPath for String {
|
|
|
|
fn display_path(&self) -> String {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DisplayPath for &String {
|
|
|
|
fn display_path(&self) -> String {
|
|
|
|
self.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DisplayPath for nu::AbsolutePath {
|
|
|
|
fn display_path(&self) -> String {
|
|
|
|
self.to_string()
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 09:08:05 +02:00
|
|
|
|
2019-07-17 20:56:15 +02:00
|
|
|
#[macro_export]
|
2019-07-16 12:28:55 +02:00
|
|
|
macro_rules! nu {
|
2019-08-29 08:31:56 +02:00
|
|
|
(cwd: $cwd:expr, $path:expr, $($part:expr),*) => {{
|
2019-08-28 19:01:16 +02:00
|
|
|
use $crate::helpers::DisplayPath;
|
|
|
|
|
|
|
|
let path = format!($path, $(
|
|
|
|
$part.display_path()
|
|
|
|
),*);
|
|
|
|
|
|
|
|
nu!($cwd, &path)
|
|
|
|
}};
|
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
(cwd: $cwd:expr, $path:expr) => {{
|
|
|
|
nu!($cwd, $path)
|
|
|
|
}};
|
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
($cwd:expr, $path:expr) => {{
|
2019-07-24 00:22:11 +02:00
|
|
|
pub use std::error::Error;
|
2019-07-17 21:51:18 +02:00
|
|
|
pub use std::io::prelude::*;
|
|
|
|
pub use std::process::{Command, Stdio};
|
2019-07-16 12:28:55 +02:00
|
|
|
|
2019-07-17 20:56:15 +02:00
|
|
|
let commands = &*format!(
|
|
|
|
"
|
2019-07-16 12:28:55 +02:00
|
|
|
cd {}
|
|
|
|
{}
|
2019-07-17 20:56:15 +02:00
|
|
|
exit",
|
2019-08-28 19:01:16 +02:00
|
|
|
$crate::helpers::in_directory($cwd),
|
|
|
|
$crate::helpers::DisplayPath::display_path(&$path)
|
2019-07-17 20:56:15 +02:00
|
|
|
);
|
2019-07-16 12:28:55 +02:00
|
|
|
|
2019-08-06 07:34:06 +02:00
|
|
|
let mut process = match Command::new(helpers::executable_path())
|
2019-07-16 12:28:55 +02:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
2019-07-17 20:56:15 +02:00
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(child) => child,
|
|
|
|
Err(why) => panic!("Can't run test {}", why.description()),
|
2019-07-16 12:28:55 +02:00
|
|
|
};
|
|
|
|
|
2019-08-06 07:34:06 +02:00
|
|
|
let stdin = process.stdin.as_mut().expect("couldn't open stdin");
|
|
|
|
stdin
|
|
|
|
.write_all(commands.as_bytes())
|
|
|
|
.expect("couldn't write to stdin");
|
2019-07-16 12:28:55 +02:00
|
|
|
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-18 00:26:27 +02:00
|
|
|
|
2019-08-06 07:34:06 +02:00
|
|
|
let output = process
|
|
|
|
.wait_with_output()
|
|
|
|
.expect("couldn't read from stdout");
|
2019-07-16 12:28:55 +02:00
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let out = String::from_utf8_lossy(&output.stdout);
|
|
|
|
let out = out.replace("\r\n", "");
|
|
|
|
let out = out.replace("\n", "");
|
|
|
|
out
|
|
|
|
}};
|
2019-07-16 12:28:55 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 20:56:15 +02:00
|
|
|
#[macro_export]
|
2019-07-16 12:28:55 +02:00
|
|
|
macro_rules! nu_error {
|
2019-08-29 08:31:56 +02:00
|
|
|
(cwd: $cwd:expr, $path:expr, $($part:expr),*) => {{
|
2019-08-28 19:01:16 +02:00
|
|
|
use $crate::helpers::DisplayPath;
|
|
|
|
|
|
|
|
let path = format!($path, $(
|
|
|
|
$part.display_path()
|
|
|
|
),*);
|
|
|
|
|
|
|
|
nu_error!($cwd, &path)
|
|
|
|
}};
|
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
(cwd: $cwd:expr, $path:expr) => {{
|
|
|
|
nu_error!($cwd, $path)
|
|
|
|
}};
|
2019-08-28 19:01:16 +02:00
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
($cwd:expr, $path:expr) => {{
|
|
|
|
pub use std::error::Error;
|
|
|
|
pub use std::io::prelude::*;
|
|
|
|
pub use std::process::{Command, Stdio};
|
2019-07-16 12:28:55 +02:00
|
|
|
|
2019-07-17 20:56:15 +02:00
|
|
|
let commands = &*format!(
|
|
|
|
"
|
2019-07-16 12:28:55 +02:00
|
|
|
cd {}
|
|
|
|
{}
|
2019-07-17 20:56:15 +02:00
|
|
|
exit",
|
2019-08-29 08:31:56 +02:00
|
|
|
$crate::helpers::in_directory($cwd),
|
|
|
|
$crate::helpers::DisplayPath::display_path(&$path)
|
2019-07-17 20:56:15 +02:00
|
|
|
);
|
2019-07-16 12:28:55 +02:00
|
|
|
|
|
|
|
let mut process = Command::new(helpers::executable_path())
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("couldn't run test");
|
|
|
|
|
|
|
|
let stdin = process.stdin.as_mut().expect("couldn't open stdin");
|
2019-07-17 20:56:15 +02:00
|
|
|
stdin
|
|
|
|
.write_all(commands.as_bytes())
|
|
|
|
.expect("couldn't write to stdin");
|
2019-07-16 12:28:55 +02:00
|
|
|
|
2019-07-17 20:56:15 +02:00
|
|
|
let output = process
|
|
|
|
.wait_with_output()
|
|
|
|
.expect("couldn't read from stderr");
|
2019-08-28 19:01:16 +02:00
|
|
|
|
|
|
|
let out = String::from_utf8_lossy(&output.stderr);
|
|
|
|
out.into_owned()
|
|
|
|
}};
|
2019-07-16 12:28:55 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 23:00:08 +02:00
|
|
|
pub enum Stub<'a> {
|
|
|
|
FileWithContent(&'a str, &'a str),
|
2019-08-25 14:59:46 +02:00
|
|
|
FileWithContentToBeTrimmed(&'a str, &'a str),
|
2019-08-01 23:00:08 +02:00
|
|
|
EmptyFile(&'a str),
|
|
|
|
}
|
2019-07-24 06:10:48 +02:00
|
|
|
|
2019-08-01 23:00:08 +02:00
|
|
|
pub struct Playground {
|
2019-08-28 19:01:16 +02:00
|
|
|
root: TempDir,
|
2019-08-01 23:00:08 +02:00
|
|
|
tests: String,
|
|
|
|
cwd: PathBuf,
|
2019-07-21 09:08:05 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
#[derive(Getters)]
|
|
|
|
#[get = "pub"]
|
|
|
|
pub struct Dirs {
|
|
|
|
pub root: PathBuf,
|
|
|
|
pub test: PathBuf,
|
|
|
|
pub fixtures: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Dirs {
|
|
|
|
pub fn formats(&self) -> PathBuf {
|
|
|
|
PathBuf::from(self.fixtures.join("formats"))
|
|
|
|
}
|
2019-09-10 12:08:01 +02:00
|
|
|
|
|
|
|
pub fn config_path(&self) -> PathBuf {
|
|
|
|
get_app_root(AppDataType::UserConfig, &nu::APP_INFO).unwrap()
|
|
|
|
}
|
2019-08-28 19:01:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 23:00:08 +02:00
|
|
|
impl Playground {
|
2019-08-28 19:01:16 +02:00
|
|
|
pub fn root(&self) -> &Path {
|
|
|
|
self.root.path()
|
2019-08-01 23:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn back_to_playground(&mut self) -> &mut Self {
|
2019-08-28 19:01:16 +02:00
|
|
|
self.cwd = PathBuf::from(self.root()).join(self.tests.clone());
|
2019-08-01 23:00:08 +02:00
|
|
|
self
|
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
pub fn setup(topic: &str, block: impl FnOnce(Dirs, &mut Playground)) {
|
2019-08-29 02:32:42 +02:00
|
|
|
let root = tempdir().expect("Couldn't create a tempdir");
|
|
|
|
let nuplay_dir = root.path().join(topic);
|
|
|
|
|
|
|
|
if PathBuf::from(&nuplay_dir).exists() {
|
|
|
|
std::fs::remove_dir_all(PathBuf::from(&nuplay_dir)).expect("can not remove directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fs::create_dir(PathBuf::from(&nuplay_dir)).expect("can not create directory");
|
|
|
|
|
|
|
|
let mut playground = Playground {
|
|
|
|
root: root,
|
|
|
|
tests: topic.to_string(),
|
|
|
|
cwd: nuplay_dir,
|
|
|
|
};
|
2019-08-28 19:01:16 +02:00
|
|
|
|
|
|
|
let project_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
let playground_root = playground.root.path();
|
|
|
|
|
|
|
|
let fixtures = project_root.join(file!());
|
|
|
|
let fixtures = fixtures
|
|
|
|
.parent()
|
|
|
|
.expect("Couldn't find the fixtures directory")
|
|
|
|
.parent()
|
|
|
|
.expect("Couldn't find the fixtures directory")
|
|
|
|
.join("fixtures");
|
|
|
|
|
|
|
|
let fixtures = dunce::canonicalize(fixtures.clone()).expect(&format!(
|
|
|
|
"Couldn't canonicalize fixtures path {}",
|
|
|
|
fixtures.display()
|
|
|
|
));
|
|
|
|
|
|
|
|
let test =
|
|
|
|
dunce::canonicalize(PathBuf::from(playground_root.join(topic))).expect(&format!(
|
|
|
|
"Couldn't canonicalize test path {}",
|
|
|
|
playground_root.join(topic).display()
|
|
|
|
));
|
|
|
|
|
2019-09-10 12:08:01 +02:00
|
|
|
let root = dunce::canonicalize(playground_root).expect(&format!(
|
|
|
|
"Couldn't canonicalize tests root path {}",
|
|
|
|
playground_root.display()
|
|
|
|
));
|
2019-09-08 10:09:05 +02:00
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let dirs = Dirs {
|
2019-09-08 10:09:05 +02:00
|
|
|
root,
|
2019-08-28 19:01:16 +02:00
|
|
|
test,
|
|
|
|
fixtures,
|
|
|
|
};
|
|
|
|
|
|
|
|
block(dirs, &mut playground);
|
|
|
|
}
|
|
|
|
|
2019-08-14 22:07:08 +02:00
|
|
|
pub fn mkdir(&mut self, directory: &str) -> &mut Self {
|
|
|
|
self.cwd.push(directory);
|
|
|
|
std::fs::create_dir_all(&self.cwd).expect("can not create directory");
|
|
|
|
self.back_to_playground();
|
2019-08-01 23:00:08 +02:00
|
|
|
self
|
2019-07-21 09:08:05 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 23:00:08 +02:00
|
|
|
pub fn with_files(&mut self, files: Vec<Stub>) -> &mut Self {
|
2019-08-25 14:59:46 +02:00
|
|
|
let endl = line_ending();
|
|
|
|
|
2019-08-01 23:00:08 +02:00
|
|
|
files
|
|
|
|
.iter()
|
|
|
|
.map(|f| {
|
|
|
|
let mut path = PathBuf::from(&self.cwd);
|
|
|
|
|
|
|
|
let (file_name, contents) = match *f {
|
2019-08-25 14:59:46 +02:00
|
|
|
Stub::EmptyFile(name) => (name, "fake data".to_string()),
|
|
|
|
Stub::FileWithContent(name, content) => (name, content.to_string()),
|
|
|
|
Stub::FileWithContentToBeTrimmed(name, content) => (
|
|
|
|
name,
|
|
|
|
content
|
|
|
|
.lines()
|
|
|
|
.skip(1)
|
|
|
|
.map(|line| line.trim())
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.join(&endl),
|
|
|
|
),
|
2019-08-01 23:00:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
path.push(file_name);
|
|
|
|
|
|
|
|
std::fs::write(PathBuf::from(path), contents.as_bytes())
|
|
|
|
.expect("can not create file");
|
|
|
|
})
|
|
|
|
.for_each(drop);
|
|
|
|
self.back_to_playground();
|
|
|
|
self
|
|
|
|
}
|
2019-07-21 09:08:05 +02:00
|
|
|
|
2019-08-01 23:00:08 +02:00
|
|
|
pub fn within(&mut self, directory: &str) -> &mut Self {
|
|
|
|
self.cwd.push(directory);
|
|
|
|
std::fs::create_dir(&self.cwd).expect("can not create directory");
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn glob_vec(pattern: &str) -> Vec<PathBuf> {
|
2019-08-19 02:12:28 +02:00
|
|
|
let glob = glob(pattern);
|
|
|
|
|
|
|
|
match glob {
|
|
|
|
Ok(paths) => paths
|
|
|
|
.map(|path| {
|
|
|
|
if let Ok(path) = path {
|
|
|
|
path
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
Err(_) => panic!("Invalid pattern."),
|
|
|
|
}
|
2019-08-01 23:00:08 +02:00
|
|
|
}
|
2019-07-21 09:08:05 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 19:58:00 +02:00
|
|
|
pub fn file_contents(full_path: impl AsRef<Path>) -> String {
|
|
|
|
let mut file = std::fs::File::open(full_path.as_ref()).expect("can not open file");
|
2019-07-24 00:22:11 +02:00
|
|
|
let mut contents = String::new();
|
|
|
|
file.read_to_string(&mut contents)
|
|
|
|
.expect("can not read file");
|
|
|
|
contents
|
2019-07-21 09:08:05 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 04:37:26 +02:00
|
|
|
pub fn file_contents_binary(full_path: impl AsRef<Path>) -> Vec<u8> {
|
|
|
|
let mut file = std::fs::File::open(full_path.as_ref()).expect("can not open file");
|
|
|
|
let mut contents = Vec::new();
|
2019-09-08 04:00:04 +02:00
|
|
|
file.read_to_end(&mut contents).expect("can not read file");
|
2019-09-04 04:37:26 +02:00
|
|
|
contents
|
|
|
|
}
|
|
|
|
|
2019-08-25 14:59:46 +02:00
|
|
|
pub fn line_ending() -> String {
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
String::from("\r\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
{
|
|
|
|
String::from("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 12:08:01 +02:00
|
|
|
pub fn delete_file_at(full_path: impl AsRef<Path>) {
|
|
|
|
let full_path = full_path.as_ref();
|
|
|
|
|
|
|
|
if full_path.exists() {
|
|
|
|
std::fs::remove_file(full_path).expect("can not delete file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 06:10:48 +02:00
|
|
|
pub fn create_file_at(full_path: impl AsRef<Path>) -> Result<(), std::io::Error> {
|
|
|
|
let full_path = full_path.as_ref();
|
|
|
|
|
2019-08-19 02:12:28 +02:00
|
|
|
if let Some(parent) = full_path.parent() {
|
|
|
|
panic!(format!("{:?} exists", parent.display()));
|
|
|
|
}
|
|
|
|
|
2019-07-24 06:10:48 +02:00
|
|
|
std::fs::write(full_path, "fake data".as_bytes())
|
2019-07-17 21:51:18 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 10:31:41 +02:00
|
|
|
pub fn copy_file_to(source: &str, destination: &str) {
|
|
|
|
std::fs::copy(source, destination).expect("can not copy file");
|
2019-07-17 21:51:18 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 19:28:58 +02:00
|
|
|
pub fn files_exist_at(files: Vec<impl AsRef<Path>>, path: impl AsRef<Path>) -> bool {
|
2019-08-07 04:45:38 +02:00
|
|
|
files.iter().all(|f| {
|
2019-08-28 19:01:16 +02:00
|
|
|
let mut loc = PathBuf::from(path.as_ref());
|
2019-08-07 04:45:38 +02:00
|
|
|
loc.push(f);
|
|
|
|
loc.exists()
|
|
|
|
})
|
2019-07-17 21:51:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_directory_at(full_path: &str) {
|
|
|
|
std::fs::remove_dir_all(PathBuf::from(full_path)).expect("can not remove directory");
|
|
|
|
}
|
|
|
|
|
2019-07-16 12:28:55 +02:00
|
|
|
pub fn executable_path() -> PathBuf {
|
|
|
|
let mut buf = PathBuf::new();
|
2019-07-17 20:56:15 +02:00
|
|
|
buf.push("target");
|
|
|
|
buf.push("debug");
|
|
|
|
buf.push("nu");
|
|
|
|
buf
|
2019-07-16 12:28:55 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
pub fn in_directory(str: impl AsRef<Path>) -> String {
|
|
|
|
str.as_ref().display().to_string()
|
2019-07-16 12:53:19 +02:00
|
|
|
}
|
2019-08-29 02:32:42 +02:00
|
|
|
|
|
|
|
pub fn pipeline(commands: &str) -> String {
|
2019-09-08 04:00:04 +02:00
|
|
|
commands
|
|
|
|
.lines()
|
|
|
|
.skip(1)
|
|
|
|
.map(|line| line.trim())
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.join(" ")
|
|
|
|
.trim_end()
|
|
|
|
.to_string()
|
2019-08-29 02:32:42 +02:00
|
|
|
}
|