nushell/crates/nu-test-support/src/commands.rs
Mathspy c66b97126f
Restore nu_with_plugins test macro (#6065)
* Updated nu_with_plugins to handle new nushell

- Now it requires the plugin format and name to be passed in, because
  we can't really guess the format
- It calls `register` with format and plugin path
- It creates a temporary folder and in it an empty temporary plugin.nu
  so that the tests don't conflict with each other or with local copy of
  plugin.nu
- Instead of passing the commands via stdin it passes them via the new
  --commands command line argument

* Rename path to command for clarity

* Enable core_inc tests

Remove deprecated inc feature and replace with new plugin feature

* Update core_inc tests for new nu_with_plugins syntax

* Rework core_inc::can_only_apply_one

The new inc plugin doesn't error if passed more than one but instead
chooses the highest increment

* Gate all plugin tests behind feature = "plugin" instead of one by one

* Remove format!-like behavior from nu_with_plugins

nu_with_plugins had format!-like behavior where it would allow calls
such as this:
```rs
nu_with_plugins!(
  cwd: "dir/",
  "open {} | get {}",
  "Cargo.toml",
  "package.version"
)
```
And although nifty it seems to have never been used before and the same
can be achieved with a format! like so:
```rs
nu_with_plugins!(
  cwd: "dir/",
  format!("open {} | get {}", "Cargo.toml", "package.version")
)
```
So I am removing it to keep the complexity of the macro in check

* Add multi-plugin support to nu_with_plugins

Useful for testing interactions between plugins

* Alternative 1: run `cargo build` inside of tests

* Handle Windows by canonicalizing paths and add .exe

One VM install later and lots of learning about how command line
arguments work and here we are
2022-07-22 00:14:37 -04:00

98 lines
2.3 KiB
Rust

// use nu_protocol::{
// ast::{Expr, Expression},
// Span, Spanned, Type,
// };
// pub struct ExternalBuilder {
// name: String,
// args: Vec<String>,
// }
// impl ExternalBuilder {
// pub fn for_name(name: &str) -> ExternalBuilder {
// ExternalBuilder {
// name: name.to_string(),
// args: vec![],
// }
// }
// pub fn arg(&mut self, value: &str) -> &mut Self {
// self.args.push(value.to_string());
// self
// }
// pub fn build(&mut self) -> ExternalCommand {
// let mut path = crate::fs::binaries();
// path.push(&self.name);
// let name = Spanned {
// item: path.to_string_lossy().to_string(),
// span: Span::new(0, 0),
// };
// let args = self
// .args
// .iter()
// .map(|arg| Expression {
// expr: Expr::String(arg.to_string()),
// span: Span::new(0, 0),
// ty: Type::Unknown,
// custom_completion: None,
// })
// .collect::<Vec<_>>();
// ExternalCommand {
// name: name.to_string(),
// name_tag: Tag::unknown(),
// args: ExternalArgs {
// list: args,
// span: name.span,
// },
// }
// }
// }
use std::{
io::Read,
process::{Command, Stdio},
};
pub fn ensure_binary_present(package: &str) {
let cargo_path = env!("CARGO");
let mut arguments = vec!["build", "--package", package, "--quiet"];
let profile = std::env::var("NUSHELL_CARGO_TARGET");
if let Ok(profile) = &profile {
arguments.push("--profile");
arguments.push(profile);
}
let mut command = Command::new(cargo_path)
.args(arguments)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("Failed to spawn cargo build command");
let stderr = command.stderr.take();
let success = command
.wait()
.expect("failed to wait cargo build command")
.success();
if let Some(mut stderr) = stderr {
let mut buffer = String::new();
stderr
.read_to_string(&mut buffer)
.expect("failed to read cargo build stderr");
if !buffer.is_empty() {
println!("=== cargo build stderr\n{}", buffer);
}
}
if !success {
panic!("cargo build failed");
}
}