nushell/crates/nu-command/tests/commands/def.rs
pwygab 6fc5244439
tighter restrictions on alias and def names (#7392)
# Description

Prevent a situation where a `def` can't be run due to a poor choice of
name. Related: #6335. Hashtags, numbers and filesizes are no longer
allowed. `alias` check has been moved because previously `alias 123`
would be caught but `alias "123"` would be permitted.

# User-Facing Changes

Some definitions can no longer be made, but because they couldn't be run
previously anyway, it doesn't really matter.

# 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` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests 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.
2022-12-22 12:31:34 -08:00

66 lines
1.5 KiB
Rust

use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
use std::fs;
#[test]
fn def_with_comment() {
Playground::setup("def_with_comment", |dirs, _| {
let data = r#"
#My echo
export def e [arg] {echo $arg}
"#;
fs::write(dirs.root().join("def_test"), data).expect("Unable to write file");
let actual = nu!(
cwd: dirs.root(),
"use def_test e; help e | to json -r"
);
assert!(actual.out.contains("My echo\\n\\n"));
});
}
#[test]
fn def_errors_with_multiple_short_flags() {
let actual = nu!(
cwd: ".", pipeline(
r#"
def test-command [ --long(-l)(-o) ] {}
"#
));
assert!(actual.err.contains("expected one short flag"));
}
#[test]
fn def_fails_with_invalid_name() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
def 1234 = echo "test"
"#
));
assert!(actual
.err
.contains("command name can't be a number, a filesize, or contain a hash"));
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
def 5gib = echo "test"
"#
));
assert!(actual
.err
.contains("command name can't be a number, a filesize, or contain a hash"));
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
def "te#t" = echo "test"
"#
));
assert!(actual
.err
.contains("command name can't be a number, a filesize, or contain a hash"));
}