Fix find -v command on tables (issue #9043) (#9159)

# Description
This PR fixes issue #9043 where find -v was returning empty tables
and/or wrong output.
It also refactors some big code chunks with repetitions into it's own
functions.

# User-Facing Changes

# Tests + Formatting
Unit tests added for asserting changes.

# After Submitting
This commit is contained in:
juanPabloMiceli
2023-05-11 15:39:21 -03:00
committed by GitHub
parent 39e51f1953
commit e735d0c561
2 changed files with 286 additions and 298 deletions

View File

@ -1,123 +1,96 @@
use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
use nu_test_support::nu;
#[test]
fn find_with_list_search_with_string() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[moe larry curly] | find moe | get 0
"#
));
let actual = nu!("[moe larry curly] | find moe | get 0");
assert_eq!(actual.out, "moe");
}
#[test]
fn find_with_list_search_with_char() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[moe larry curly] | find l | to json -r
"#
));
let actual = nu!("[moe larry curly] | find l | to json -r");
assert_eq!(actual.out, r#"["larry","curly"]"#);
}
#[test]
fn find_with_list_search_with_number() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[1 2 3 4 5] | find 3 | get 0
"#
));
let actual = nu!("[1 2 3 4 5] | find 3 | get 0");
assert_eq!(actual.out, "3");
}
#[test]
fn find_with_string_search_with_string() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo Cargo.toml | find toml
"#
));
let actual = nu!("echo Cargo.toml | find toml");
assert_eq!(actual.out, "Cargo.toml");
}
#[test]
fn find_with_string_search_with_string_not_found() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[moe larry curly] | find shemp | is-empty
"#
));
let actual = nu!("[moe larry curly] | find shemp | is-empty");
assert_eq!(actual.out, "true");
}
#[test]
fn find_with_filepath_search_with_string() {
Playground::setup("filepath_test_1", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("amigos.txt"),
EmptyFile("arepas.clu"),
EmptyFile("los.txt"),
EmptyFile("tres.txt"),
]);
let actual =
nu!(r#"["amigos.txt","arepas.clu","los.txt","tres.txt"] | find arep | to json -r"#);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls
| get name
| find arep
| to json -r
"#
));
assert_eq!(actual.out, r#"["arepas.clu"]"#);
})
assert_eq!(actual.out, r#"["arepas.clu"]"#);
}
#[test]
fn find_with_filepath_search_with_multiple_patterns() {
Playground::setup("filepath_test_2", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("amigos.txt"),
EmptyFile("arepas.clu"),
EmptyFile("los.txt"),
EmptyFile("tres.txt"),
]);
let actual =
nu!(r#"["amigos.txt","arepas.clu","los.txt","tres.txt"] | find arep ami | to json -r"#);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls
| get name
| find arep ami
| to json -r
"#
));
assert_eq!(actual.out, r#"["amigos.txt","arepas.clu"]"#);
})
assert_eq!(actual.out, r#"["amigos.txt","arepas.clu"]"#);
}
#[test]
fn find_takes_into_account_linebreaks_in_string() {
let actual = nu!(
cwd: ".", pipeline(
r#"
"atest\nanothertest\nnohit\n" | find a | length
"#
));
let actual = nu!(r#""atest\nanothertest\nnohit\n" | find a | length"#);
assert_eq!(actual.out, "2");
}
#[test]
fn find_with_regex_in_table_keeps_row_if_one_column_matches() {
let actual = nu!(
"[[name nickname]; [Maurice moe] [Laurence larry]] | find --regex ce | get name | to json -r"
);
assert_eq!(actual.out, r#"["Maurice","Laurence"]"#);
}
#[test]
fn inverted_find_with_regex_in_table_keeps_row_if_none_of_the_columns_matches() {
let actual = nu!(
"[[name nickname]; [Maurice moe] [Laurence larry]] | find --regex moe --invert | get name | to json -r"
);
assert_eq!(actual.out, r#"["Laurence"]"#);
}
#[test]
fn find_in_table_only_keep_rows_with_matches_on_selected_columns() {
let actual = nu!(
"[[name nickname]; [Maurice moe] [Laurence larry]] | find r --columns [nickname] | get name | to json -r"
);
assert!(actual.out.contains("Laurence"));
assert!(!actual.out.contains("Maurice"));
}
#[test]
fn inverted_find_in_table_keeps_row_if_none_of_the_selected_columns_matches() {
let actual = nu!(
"[[name nickname]; [Maurice moe] [Laurence larry]] | find r --columns [nickname] --invert | get name | to json -r"
);
assert_eq!(actual.out, r#"["Maurice"]"#);
}