mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
* Add new path parse subcommand This includes a slight refactor to all the path subcommand `action()` functions. * Remove filestem and extension; Fix example * Add additional description to path parse * Put join arg behind flag; Fix missing import (Win) * Fix error when column path is passed as arg * Add structured path joining Structured path is implicitly joined at every patch subcommand call. * Fix existing path join tests; Fix rustfmt * Remove redundant 'static lifetime (clippy) * Add initial impl of path split subcommand * Add ability to join path from parts * Fix wrong results in path split examples * Fix remaining asyncs after engine change * Do not wrap split path parts into table When the input is just a list of values, the `path split` command will split each value directly into the output stream, similar to `split-row`. Column path--specified values are still wrapped into a table so they can still be used to replace table fields. * Join list of values instead of going one-by-one When `path join` encounters a list of values, it attempts to join them, instead of going one-by-one like the rest of the path commands. You can still `each { echo $it | path join }` to join them one-by-one, if the values are, e.g., tables. Now, the behavior of `path split` and `path join` should match the `split-row` and `str collect` counterparts and should hopefully align better with user's expectations. * Make sure path join detects structured path * Fix panic on empty input stream Also, doesn't collect input into vector unnecessarily. * Fix path join not appending value * Remove argument serialization * Make better errors; Misc refactor * OsStr -> String encoding is now lossy, instead of throwing an error * The consequence is action() now always returns Value instead of Result * Removed redundant handle_value() call in `path join` * Fix possible incorrect error detection in `path split` * Applied rustfmt + clippy * Add more usage, examples & test; Fix type error The 'parent' column was required to be a path but didn't work with string. * Add more help & examples; Maybe fix Windows error * Refactor operate function Reducing code repetition * Review usages and examples * Add the option to manually specify the extension * Add more tests; Fix failures on Windows * Move path commands to engine-p * Small refactor
This commit is contained in:
@ -1,37 +0,0 @@
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn returns_extension_of_path_ending_with_dot() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "bacon." | path extension
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replaces_extension_with_dot_of_path_ending_with_dot() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "bacon." | path extension -r .egg
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "bacon..egg");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replaces_extension_of_empty_path() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "" | path extension -r egg
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "");
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
use super::join_path_sep;
|
||||
|
||||
#[test]
|
||||
fn returns_filestem_of_dot() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "menu/eggs/."
|
||||
| path filestem
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "eggs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_filestem_of_double_dot() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "menu/eggs/.."
|
||||
| path filestem
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_filestem_of_path_with_empty_prefix() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "menu/spam.txt"
|
||||
| path filestem -p ""
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "spam");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_filestem_of_path_with_empty_suffix() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "menu/spam.txt"
|
||||
| path filestem -s ""
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "spam.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_filestem_of_path_with_empty_prefix_and_suffix() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "menu/spam.txt"
|
||||
| path filestem -p "" -s ""
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "spam.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_filestem_with_wrong_prefix_and_suffix() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "menu/spam.txt"
|
||||
| path filestem -p "bacon" -s "eggs"
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "spam.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replaces_filestem_stripped_to_dot() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "menu/spam.txt"
|
||||
| path filestem -p "spam" -s "txt" -r ".eggs."
|
||||
"#
|
||||
));
|
||||
|
||||
let expected = join_path_sep(&["menu", "spam.eggs.txt"]);
|
||||
assert_eq!(actual.out, expected);
|
||||
}
|
@ -8,7 +8,7 @@ fn returns_path_joined_with_column_path() {
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo [ [name]; [eggs] ]
|
||||
| path join spam.txt name
|
||||
| path join -a spam.txt name
|
||||
| get name
|
||||
"#
|
||||
));
|
||||
@ -17,13 +17,27 @@ fn returns_path_joined_with_column_path() {
|
||||
assert_eq!(actual.out, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_path_joined_from_list() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo [ home viking spam.txt ]
|
||||
| path join
|
||||
"#
|
||||
));
|
||||
|
||||
let expected = join_path_sep(&["home", "viking", "spam.txt"]);
|
||||
assert_eq!(actual.out, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn appends_slash_when_joined_with_empty_path() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo "/some/dir"
|
||||
| path join ''
|
||||
| path join -a ''
|
||||
"#
|
||||
));
|
||||
|
||||
@ -37,7 +51,7 @@ fn returns_joined_path_when_joining_empty_path() {
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo ""
|
||||
| path join foo.txt
|
||||
| path join -a foo.txt
|
||||
"#
|
||||
));
|
||||
|
||||
|
@ -2,9 +2,9 @@ mod basename;
|
||||
mod dirname;
|
||||
mod exists;
|
||||
mod expand;
|
||||
mod extension;
|
||||
mod filestem;
|
||||
mod join;
|
||||
mod parse;
|
||||
mod split;
|
||||
mod type_;
|
||||
|
||||
use std::path::MAIN_SEPARATOR;
|
||||
|
136
crates/nu-command/tests/commands/path/parse.rs
Normal file
136
crates/nu-command/tests/commands/path/parse.rs
Normal file
@ -0,0 +1,136 @@
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn parses_single_path_prefix() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo 'C:\users\viking\spam.txt'
|
||||
| path parse
|
||||
| get prefix
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "C:");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_single_path_parent() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo 'home/viking/spam.txt'
|
||||
| path parse
|
||||
| get parent
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "home/viking");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_single_path_stem() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo 'home/viking/spam.txt'
|
||||
| path parse
|
||||
| get stem
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "spam");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_custom_extension_gets_extension() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo 'home/viking/spam.tar.gz'
|
||||
| path parse -e tar.gz
|
||||
| get extension
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "tar.gz");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_custom_extension_gets_stem() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo 'home/viking/spam.tar.gz'
|
||||
| path parse -e tar.gz
|
||||
| get stem
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "spam");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_ignoring_extension_gets_extension() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo 'home/viking/spam.tar.gz'
|
||||
| path parse -e ''
|
||||
| get extension
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_ignoring_extension_gets_stem() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo 'home/viking/spam.tar.gz'
|
||||
| path parse -e ""
|
||||
| get stem
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "spam.tar.gz");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_column_path_extension() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo [[home, barn]; ['home/viking/spam.txt', 'barn/cow/moo.png']]
|
||||
| path parse home barn
|
||||
| get barn
|
||||
| get extension
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "png");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_into_correct_number_of_columns() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo 'home/viking/spam.txt'
|
||||
| path parse
|
||||
| pivot
|
||||
| get Column0
|
||||
| length
|
||||
"#
|
||||
));
|
||||
|
||||
#[cfg(windows)]
|
||||
let expected = "4";
|
||||
#[cfg(not(windows))]
|
||||
let expected = "3";
|
||||
|
||||
assert_eq!(actual.out, expected);
|
||||
}
|
47
crates/nu-command/tests/commands/path/split.rs
Normal file
47
crates/nu-command/tests/commands/path/split.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn splits_empty_path() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo '' | path split
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn splits_correctly_single_path() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo ['home/viking/spam.txt']
|
||||
| path split
|
||||
| last
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "spam.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn splits_correctly_with_column_path() {
|
||||
let actual = nu!(
|
||||
cwd: "tests", pipeline(
|
||||
r#"
|
||||
echo [
|
||||
[home, barn];
|
||||
|
||||
['home/viking/spam.txt', 'barn/cow/moo.png']
|
||||
['home/viking/eggs.txt', 'barn/goat/cheese.png']
|
||||
]
|
||||
| path split home barn
|
||||
| get barn
|
||||
| length
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "6");
|
||||
}
|
Reference in New Issue
Block a user