2019-12-15 17:15:06 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! nu {
|
|
|
|
(cwd: $cwd:expr, $path:expr, $($part:expr),*) => {{
|
|
|
|
use $crate::fs::DisplayPath;
|
|
|
|
|
|
|
|
let path = format!($path, $(
|
|
|
|
$part.display_path()
|
|
|
|
),*);
|
|
|
|
|
|
|
|
nu!($cwd, &path)
|
|
|
|
}};
|
|
|
|
|
|
|
|
(cwd: $cwd:expr, $path:expr) => {{
|
|
|
|
nu!($cwd, $path)
|
|
|
|
}};
|
|
|
|
|
2020-09-14 16:07:02 +02:00
|
|
|
($cwd:expr, $path:expr) => {{
|
2022-02-02 21:59:01 +01:00
|
|
|
pub use itertools::Itertools;
|
2020-09-14 16:07:02 +02:00
|
|
|
pub use std::error::Error;
|
|
|
|
pub use std::io::prelude::*;
|
|
|
|
pub use std::process::{Command, Stdio};
|
2021-05-13 05:03:49 +02:00
|
|
|
pub use $crate::NATIVE_PATH_ENV_VAR;
|
2020-09-14 16:07:02 +02:00
|
|
|
|
2022-05-01 23:49:31 +02:00
|
|
|
pub fn escape_quote_string(input: String) -> String {
|
|
|
|
let mut output = String::with_capacity(input.len() + 2);
|
|
|
|
output.push('"');
|
|
|
|
|
|
|
|
for c in input.chars() {
|
|
|
|
if c == '"' || c == '\\' {
|
|
|
|
output.push('\\');
|
|
|
|
}
|
|
|
|
output.push(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
output.push('"');
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
// let commands = &*format!(
|
|
|
|
// "
|
|
|
|
// {}
|
|
|
|
// exit",
|
|
|
|
// $crate::fs::DisplayPath::display_path(&$path)
|
|
|
|
// );
|
|
|
|
|
|
|
|
let test_bins = $crate::fs::binaries();
|
|
|
|
|
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory.");
|
|
|
|
let test_bins = nu_path::canonicalize_with(&test_bins, cwd).unwrap_or_else(|e| {
|
2020-09-14 16:07:02 +02:00
|
|
|
panic!(
|
|
|
|
"Couldn't canonicalize dummy binaries path {}: {:?}",
|
|
|
|
test_bins.display(),
|
|
|
|
e
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut paths = $crate::shell_os_paths();
|
|
|
|
paths.insert(0, test_bins);
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
let path = $path.lines().collect::<Vec<_>>().join("; ");
|
|
|
|
|
2021-09-10 00:44:22 +02:00
|
|
|
let paths_joined = match std::env::join_paths(paths) {
|
2020-09-14 16:07:02 +02:00
|
|
|
Ok(all) => all,
|
|
|
|
Err(_) => panic!("Couldn't join paths for PATH var."),
|
|
|
|
};
|
|
|
|
|
2022-02-17 23:23:04 +01:00
|
|
|
let target_cwd = $crate::fs::in_directory(&$cwd);
|
|
|
|
|
2020-09-14 16:07:02 +02:00
|
|
|
let mut process = match Command::new($crate::fs::executable_path())
|
2022-02-17 23:23:04 +01:00
|
|
|
.env("PWD", &target_cwd) // setting PWD is enough to set cwd
|
2021-05-13 05:03:49 +02:00
|
|
|
.env(NATIVE_PATH_ENV_VAR, paths_joined)
|
2022-02-02 21:59:01 +01:00
|
|
|
// .arg("--skip-plugins")
|
|
|
|
// .arg("--no-history")
|
|
|
|
// .arg("--config-file")
|
|
|
|
// .arg($crate::fs::DisplayPath::display_path(&$crate::fs::fixtures().join("playground/config/default.toml")))
|
2022-05-01 23:49:31 +02:00
|
|
|
.arg(format!("-c {}", escape_quote_string($crate::fs::DisplayPath::display_path(&path))))
|
2022-02-02 21:59:01 +01:00
|
|
|
.stdout(Stdio::piped())
|
|
|
|
// .stdin(Stdio::piped())
|
2020-09-14 16:07:02 +02:00
|
|
|
.stderr(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(child) => child,
|
2022-02-02 21:59:01 +01:00
|
|
|
Err(why) => panic!("Can't run test {:?} {}", $crate::fs::executable_path(), why.to_string()),
|
|
|
|
};
|
|
|
|
|
|
|
|
// let stdin = process.stdin.as_mut().expect("couldn't open stdin");
|
|
|
|
// stdin
|
|
|
|
// .write_all(b"exit\n")
|
|
|
|
// .expect("couldn't write to stdin");
|
2020-09-14 16:07:02 +02:00
|
|
|
|
|
|
|
let output = process
|
|
|
|
.wait_with_output()
|
|
|
|
.expect("couldn't read from stdout/stderr");
|
|
|
|
|
|
|
|
let out = $crate::macros::read_std(&output.stdout);
|
|
|
|
let err = String::from_utf8_lossy(&output.stderr);
|
|
|
|
|
|
|
|
println!("=== stderr\n{}", err);
|
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
$crate::Outcome::new(out,err.into_owned())
|
2020-09-14 16:07:02 +02:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! nu_with_plugins {
|
|
|
|
(cwd: $cwd:expr, $path:expr, $($part:expr),*) => {{
|
|
|
|
use $crate::fs::DisplayPath;
|
|
|
|
|
|
|
|
let path = format!($path, $(
|
|
|
|
$part.display_path()
|
|
|
|
),*);
|
|
|
|
|
|
|
|
nu_with_plugins!($cwd, &path)
|
|
|
|
}};
|
|
|
|
|
|
|
|
(cwd: $cwd:expr, $path:expr) => {{
|
|
|
|
nu_with_plugins!($cwd, $path)
|
|
|
|
}};
|
|
|
|
|
2019-12-15 17:15:06 +01:00
|
|
|
($cwd:expr, $path:expr) => {{
|
|
|
|
pub use std::error::Error;
|
|
|
|
pub use std::io::prelude::*;
|
|
|
|
pub use std::process::{Command, Stdio};
|
2021-05-13 05:03:49 +02:00
|
|
|
pub use crate::NATIVE_PATH_ENV_VAR;
|
2019-12-15 17:15:06 +01:00
|
|
|
|
|
|
|
let commands = &*format!(
|
|
|
|
"
|
|
|
|
{}
|
|
|
|
exit",
|
|
|
|
$crate::fs::DisplayPath::display_path(&$path)
|
|
|
|
);
|
|
|
|
|
2020-01-23 17:21:05 +01:00
|
|
|
let test_bins = $crate::fs::binaries();
|
2021-08-28 14:59:09 +02:00
|
|
|
let test_bins = nu_path::canonicalize(&test_bins).unwrap_or_else(|e| {
|
2020-01-14 05:17:20 +01:00
|
|
|
panic!(
|
|
|
|
"Couldn't canonicalize dummy binaries path {}: {:?}",
|
2020-01-23 17:21:05 +01:00
|
|
|
test_bins.display(),
|
2020-01-14 05:17:20 +01:00
|
|
|
e
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2020-01-23 17:21:05 +01:00
|
|
|
let mut paths = $crate::shell_os_paths();
|
2020-05-18 21:18:46 +02:00
|
|
|
paths.insert(0, test_bins);
|
2020-01-23 17:21:05 +01:00
|
|
|
|
2021-09-10 00:44:22 +02:00
|
|
|
let paths_joined = match std::env::join_paths(paths) {
|
2020-01-23 17:21:05 +01:00
|
|
|
Ok(all) => all,
|
|
|
|
Err(_) => panic!("Couldn't join paths for PATH var."),
|
|
|
|
};
|
|
|
|
|
2022-02-17 23:23:04 +01:00
|
|
|
let target_cwd = $crate::fs::in_directory(&$cwd);
|
|
|
|
|
2019-12-15 17:15:06 +01:00
|
|
|
let mut process = match Command::new($crate::fs::executable_path())
|
2022-02-17 23:23:04 +01:00
|
|
|
.env("PWD", &target_cwd) // setting PWD is enough to set cwd
|
2021-05-13 05:03:49 +02:00
|
|
|
.env(NATIVE_PATH_ENV_VAR, paths_joined)
|
2019-12-15 17:15:06 +01:00
|
|
|
.stdout(Stdio::piped())
|
2020-05-07 13:03:43 +02:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped())
|
2019-12-15 17:15:06 +01:00
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(child) => child,
|
2019-12-31 08:36:08 +01:00
|
|
|
Err(why) => panic!("Can't run test {}", why.to_string()),
|
2019-12-15 17:15:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let stdin = process.stdin.as_mut().expect("couldn't open stdin");
|
|
|
|
stdin
|
|
|
|
.write_all(commands.as_bytes())
|
|
|
|
.expect("couldn't write to stdin");
|
|
|
|
|
|
|
|
let output = process
|
|
|
|
.wait_with_output()
|
2020-05-07 13:03:43 +02:00
|
|
|
.expect("couldn't read from stdout/stderr");
|
2019-12-15 17:15:06 +01:00
|
|
|
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
let out = $crate::macros::read_std(&output.stdout);
|
2020-05-07 13:03:43 +02:00
|
|
|
let err = String::from_utf8_lossy(&output.stderr);
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
println!("=== stderr\n{}", err);
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
$crate::Outcome::new(out,err.into_owned())
|
2019-12-15 17:15:06 +01:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
pub fn read_std(std: &[u8]) -> String {
|
|
|
|
let out = String::from_utf8_lossy(std);
|
2021-03-27 06:08:03 +01:00
|
|
|
let out = out.lines().collect::<Vec<_>>().join("\n");
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
let out = out.replace("\r\n", "");
|
2022-02-24 20:02:28 +01:00
|
|
|
out.replace('\n', "")
|
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
|
|
|
}
|