nushell/crates/nu-command/tests/commands/alias.rs
nicolb2305 acd7c98c39
Removes unnecessary cwd and pipeline from various tests (#9202)
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Cleans up various tests that unnecessarily use the `cwd` argument of
`nu!`, and the `pipeline` function for single line commands. Also
replaces some unnecessary raw strings with normal strings. Part of
#8670.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
None

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
All checks pass

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-05-17 18:55:26 -05:00

152 lines
4.0 KiB
Rust

use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
#[test]
fn alias_simple() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
alias bar = use sample_def.nu greet;
bar;
greet
"#
));
assert_eq!(actual.out, "hello");
}
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
#[test]
fn alias_hiding_1() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
overlay use ./activate-foo.nu;
$nu.scope.aliases | find deactivate-foo | length
"#
));
assert_eq!(actual.out, "1");
}
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
#[test]
fn alias_hiding_2() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
overlay use ./activate-foo.nu;
deactivate-foo;
$nu.scope.aliases | find deactivate-foo | length
"#
));
assert_eq!(actual.out, "0");
}
#[test]
fn alias_fails_with_invalid_name() {
let err_msg = "name can't be a number, a filesize, or contain a hash # or caret ^";
let actual = nu!(r#" alias 1234 = echo "test" "#);
assert!(actual.err.contains(err_msg));
let actual = nu!(r#" alias 5gib = echo "test" "#);
assert!(actual.err.contains(err_msg));
let actual = nu!(r#" alias "te#t" = echo "test" "#);
assert!(actual.err.contains(err_msg));
let actual = nu!(r#" alias ^foo = echo "bar" "#);
assert!(actual.err.contains(err_msg));
}
#[test]
fn cant_alias_keyword() {
let actual = nu!(r#" alias ou = let "#);
assert!(actual.err.contains("cant_alias_keyword"));
}
#[test]
fn alias_wont_recurse() {
let actual = nu!(pipeline(
"
module myspamsymbol {
export def myfoosymbol [prefix: string, msg: string] {
$prefix + $msg
}
};
use myspamsymbol myfoosymbol;
alias myfoosymbol = myfoosymbol 'hello';
myfoosymbol ' world'
"
));
assert_eq!(actual.out, "hello world");
assert!(actual.err.is_empty());
}
// Issue https://github.com/nushell/nushell/issues/8246
#[test]
fn alias_wont_recurse2() {
Playground::setup("alias_wont_recurse2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
def eggs [] { spam 'eggs' }
alias spam = spam 'spam'
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
def spam [what: string] { 'spam ' + $what };
source spam.nu;
spam
"#
));
assert_eq!(actual.out, "spam spam");
assert!(actual.err.is_empty());
})
}
#[test]
fn alias_invalid_expression() {
let actual = nu!(r#" alias spam = 'foo' "#);
assert!(actual.err.contains("cant_alias_expression"));
let actual = nu!(r#" alias spam = ([1 2 3] | length) "#);
assert!(actual.err.contains("cant_alias_expression"));
let actual = nu!(r#" alias spam = 0..12 "#);
assert!(actual.err.contains("cant_alias_expression"));
}
#[test]
fn alias_if() {
let actual = nu!(r#" alias spam = if true { 'spam' } else { 'eggs' }; spam "#);
assert_eq!(actual.out, "spam");
}
#[test]
fn alias_match() {
let actual = nu!(r#" alias spam = match 3 { 1..10 => 'yes!' }; spam "#);
assert_eq!(actual.out, "yes!");
}
// Issue https://github.com/nushell/nushell/issues/8103
#[test]
fn alias_multiword_name() {
let actual = nu!(r#"alias `foo bar` = echo 'test'; foo bar"#);
assert_eq!(actual.out, "test");
let actual = nu!(r#"alias 'foo bar' = echo 'test'; foo bar"#);
assert_eq!(actual.out, "test");
let actual = nu!(r#"alias "foo bar" = echo 'test'; foo bar"#);
assert_eq!(actual.out, "test");
}