Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove file I/O from tests that don't need it #11182

Merged
merged 16 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/nu-command/src/filters/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,9 @@ fn highlight_terms_in_record_with_search_columns(
let val_str = val.into_string("", config);
let Some(term_str) = term_strs
.iter()
.find(|term_str| contains_ignore_case(&val_str, term_str)) else {
continue;
.find(|term_str| contains_ignore_case(&val_str, term_str))
else {
continue;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's wrongly formatted, would you please run cargo fmt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, that's a bit weird, but I did run cargo fmt:
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let ... = ... else {} formatting was only stabilized in very recent versions of rustfmt and may possibly still be treated preserved depending on the active toolchain version?

};

let highlighted_str =
Expand Down
36 changes: 13 additions & 23 deletions crates/nu-command/tests/commands/compact.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};

#[test]
fn discards_rows_where_given_column_is_empty() {
Playground::setup("compact_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_amigos.json",
r#"
let sample_json = r#"
{
"amigos": [
{"name": "Yehuda", "rusty_luck": 1},
Expand All @@ -16,35 +11,30 @@ fn discards_rows_where_given_column_is_empty() {
{"name":"GorbyPuff"}
]
}
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(), pipeline(
"
open los_tres_amigos.json
let actual = nu!(pipeline(&format!(
"
'{}' | from json
| get amigos
cosineblast marked this conversation as resolved.
Show resolved Hide resolved
| compact rusty_luck
| length
"
));
",
sample_json
)));

assert_eq!(actual.out, "3");
});
assert_eq!(actual.out, "3");
}
#[test]
fn discards_empty_rows_by_default() {
Playground::setup("compact_test_2", |dirs, _| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
let actual = nu!(pipeline(
r#"
echo "[1,2,3,14,null]"
| from json
| compact
| length
"#
));
));

assert_eq!(actual.out, "4");
});
assert_eq!(actual.out, "4");
}
25 changes: 9 additions & 16 deletions crates/nu-command/tests/commands/default.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};

#[test]
fn adds_row_data_if_column_missing() {
Playground::setup("default_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_amigos.json",
r#"
let sample = r#"
{
"amigos": [
{"name": "Yehuda"},
Expand All @@ -16,22 +11,20 @@ fn adds_row_data_if_column_missing() {
{"name":"GorbyPuff"}
]
}
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(), pipeline(
"
open los_tres_amigos.json
let actual = nu!(pipeline(&format!(
"
{}
| get amigos
| default 1 rusty_luck
| where rusty_luck == 1
| length
"
));
",
sample
)));

assert_eq!(actual.out, "2");
});
assert_eq!(actual.out, "2");
}

#[test]
Expand Down
77 changes: 26 additions & 51 deletions crates/nu-command/tests/commands/flatten.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};

#[test]
Expand Down Expand Up @@ -43,10 +41,7 @@ fn flatten_nested_tables() {

#[test]
fn flatten_row_column_explicitly() {
Playground::setup("flatten_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"katz.json",
r#"
let sample = r#"
[
{
"people": {
Expand All @@ -61,24 +56,19 @@ fn flatten_row_column_explicitly() {
}
}
]
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(),
"open katz.json | flatten people --all | where name == Andres | length"
);
let actual = nu!(pipeline(&format!(
"{} | flatten people --all | where name == Andres | length",
sample
)));

assert_eq!(actual.out, "1");
})
assert_eq!(actual.out, "1");
}

#[test]
fn flatten_row_columns_having_same_column_names_flats_separately() {
Playground::setup("flatten_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"katz.json",
r#"
let sample = r#"
[
{
"people": {
Expand All @@ -95,24 +85,19 @@ fn flatten_row_columns_having_same_column_names_flats_separately() {
"city": [{"name": "Oregon"}, {"name": "Brooklin"}]
}
]
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(),
"open katz.json | flatten --all | flatten people city | get city_name | length"
);
let actual = nu!(pipeline(&format!(
"{} | flatten --all | flatten people city | get city_name | length",
sample
)));

assert_eq!(actual.out, "4");
})
assert_eq!(actual.out, "4");
}

#[test]
fn flatten_table_columns_explicitly() {
Playground::setup("flatten_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"katz.json",
r#"
let sample = r#"
[
{
"people": {
Expand All @@ -129,24 +114,19 @@ fn flatten_table_columns_explicitly() {
"city": ["Oregon", "Brooklin"]
}
]
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(),
"open katz.json | flatten city --all | where people.name == Katz | length"
);
let actual = nu!(pipeline(&format!(
"{} | flatten city --all | where people.name == Katz | length",
sample
)));

assert_eq!(actual.out, "2");
})
assert_eq!(actual.out, "2");
}

#[test]
fn flatten_more_than_one_column_that_are_subtables_not_supported() {
Playground::setup("flatten_test_4", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"katz.json",
r#"
let sample = r#"
[
{
"people": {
Expand All @@ -165,15 +145,10 @@ fn flatten_more_than_one_column_that_are_subtables_not_supported() {
"city": ["Oregon", "Brooklin"]
}
]
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(),
"open katz.json | flatten tags city --all"
);
let actual = nu!(pipeline(&format!("{} | flatten tags city --all", sample)));

assert!(actual.err.contains("tried flattening"));
assert!(actual.err.contains("but is flattened already"));
})
assert!(actual.err.contains("tried flattening"));
assert!(actual.err.contains("but is flattened already"));
}
90 changes: 34 additions & 56 deletions crates/nu-command/tests/commands/group_by.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};

#[test]
fn groups() {
Playground::setup("group_by_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_caballeros.csv",
r#"
first_name,last_name,rusty_at,type
Andrés,Robalino,10/11/2013,A
JT,Turner,10/12/2013,B
Yehuda,Katz,10/11/2013,A
"#,
)]);
let sample = r#"
[[first_name, last_name, rusty_at, type];
[Andrés, Robalino, "10/11/2013", A],
[JT, Turner, "10/12/2013", B],
[Yehuda, Katz, "10/11/2013", A]]
"#;

let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open los_tres_caballeros.csv
let actual = nu!(pipeline(&format!(
r#"
{}
| group-by rusty_at
| get "10/11/2013"
| length
"#
));
"#,
sample
)));

assert_eq!(actual.out, "2");
})
assert_eq!(actual.out, "2");
}

#[test]
fn errors_if_given_unknown_column_name() {
Playground::setup("group_by_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_caballeros.json",
r#"
let sample = r#"
{
"nu": {
"committers": [
Expand All @@ -54,46 +44,34 @@ fn errors_if_given_unknown_column_name() {
]
}
}
"#,
)]);
"#;

let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open los_tres_caballeros.json
| group-by {|| get nu.releases.version }
"#
));
let actual = nu!(pipeline(&format!(
r#"
'{}'
| from json
| group-by {{|| get nu.releases.version }}
"#,
sample
)));

assert!(actual
.err
.contains("requires a table with one value for grouping"));
})
assert!(actual
.err
.contains("requires a table with one value for grouping"));
}

#[test]
fn errors_if_column_not_found() {
Playground::setup("group_by_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_caballeros.csv",
r#"
first_name,last_name,rusty_at,type
Andrés,Robalino,10/11/2013,A
JT,Turner,10/12/2013,B
Yehuda,Katz,10/11/2013,A
"#,
)]);
let sample = r#"
[[first_name, last_name, rusty_at, type];
[Andrés, Robalino, "10/11/2013", A],
[JT, Turner, "10/12/2013", B],
[Yehuda, Katz, "10/11/2013", A]]
"#;

let actual = nu!(
cwd: dirs.test(), pipeline(
"
open los_tres_caballeros.csv
| group-by ttype
"
));
let actual = nu!(pipeline(&format!("{} | group-by ttype", sample)));

assert!(actual.err.contains("did you mean 'type'"),);
})
assert!(actual.err.contains("did you mean 'type'"),);
}

#[test]
Expand Down