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

Tests: Remove file I/O from all tests that don't need it #7189

Closed
webbedspace opened this issue Nov 22, 2022 · 7 comments · Fixed by #11182
Closed

Tests: Remove file I/O from all tests that don't need it #7189

webbedspace opened this issue Nov 22, 2022 · 7 comments · Fixed by #11182
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed tests issues to add tests or fix tests
Milestone

Comments

@webbedspace
Copy link
Contributor

Related problem

An alarming amount of tests in Nu-command resemble the following:

#[test]
fn removes_duplicate_rows() {
    Playground::setup("uniq_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
                Jonathan,Turner,10/12/2013,B
                Yehuda,Katz,10/11/2013,A
                Jonathan,Turner,10/12/2013,B
                Yehuda,Katz,10/11/2013,A
            "#,
        )]);

        let actual = nu!(
            cwd: dirs.test(), pipeline(
            r#"
                open los_tres_caballeros.csv
                | uniq
                | length

            "#
        ));

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

All I have to say is: why is file I/O (and CSV parsing) even part of this test?! The test is whether the uniq command works. Just write the input data in Nuon! Adding open and an entire file playground setup just makes the test slower and more difficult to read.

Describe the solution you'd like

Command tests should incisively probe the output of commands using the minimum necessary input to demonstrate whatever behaviour is being tested. Any command that doesn't involve file I/O shouldn't have it included in the test.

Describe alternatives you've considered

No response

Additional context and details

No response

@webbedspace webbedspace added the enhancement New feature or request label Nov 22, 2022
@sholderbach
Copy link
Member

Generally yes, the majority of the unittests should focus on primarily exercising the edge cases of the command of interest.
But we also need to make sure we capture the quirks of our engine and nu_protocol. E.g. if the implementation differs for a PipelineData::Liststream and Value::List, directly pumping in the data literals would lose some of the complexity and coverage.
When simplifying the tests the ones describing weird interactions that caused regressions previously, should remain obvious.

@sholderbach sholderbach added help wanted Extra attention is needed good first issue Good for newcomers tests issues to add tests or fix tests labels Nov 22, 2022
@fdncred
Copy link
Collaborator

fdncred commented Nov 22, 2022

Also, some of these tests have been around longer than Nuon has been around.

@rgwood
Copy link
Contributor

rgwood commented Mar 30, 2023

I think it would be good to clean this up where file access is unnecessary, it would simplify our tests.

That said, I don’t think we’ll see any meaningful improvements in test execution time.

@cosineblast
Copy link
Contributor

I'd like to work on this particular issue

@fdncred
Copy link
Collaborator

fdncred commented Nov 8, 2023

@cosineblast go for it. i've assigned it to you.

@cosineblast
Copy link
Contributor

Is it ok to use | from json to convert tests that describe their input as json, or would it be preferable to use nuon directly in those as well? Example:

#[test]
fn discards_rows_where_given_column_is_empty() {
    let sample_json = r#"
                {
                    "amigos": [
                        {"name":   "Yehuda", "rusty_luck": 1},
                        {"name": "JT", "rusty_luck": 1},
                        {"name":   "Andres", "rusty_luck": 1},
                        {"name":"GorbyPuff"}
                    ]
                }
            "#;

    let actual = nu!(pipeline(&format!(
        "
                '{}' | from json
                | get amigos
                | compact rusty_luck
                | length
            ",
        sample_json
    )));

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

@sholderbach
Copy link
Member

I think generally, having simpler tests that are focussed on just the properties under test is preferrable...
with the small but nasty caveat that the way PipelineData is used in some commands could permit that behavior is unreachable if the data is e.g. PipelineData::ListStream instead of PIpelineData::Value or vice versa. So a certain diversity in how test data is passed to the command under test can be helpful.

But from json without -o currently uses PipelineData::Value like you get from a literal.

Ok(convert_string_to_value(string_input, span)?
.into_pipeline_data_with_metadata(metadata))

So if there is not a specific prior failure mentioned or it makes sense from the context I think this can be turned into a direct literal.

sholderbach pushed a commit that referenced this issue Nov 29, 2023
# Description

This PR implements modifications to command tests that write unnecessary
json and csv to disk then load it with open, by using nuon literals
instead.

- Fixes #7189



# User-Facing Changes
None

# Tests + Formatting
This only affects existing tests, which still pass.
@hustcer hustcer added this to the v0.88.0 milestone Nov 30, 2023
hardfau1t pushed a commit to hardfau1t/nushell that referenced this issue Dec 14, 2023
# Description

This PR implements modifications to command tests that write unnecessary
json and csv to disk then load it with open, by using nuon literals
instead.

- Fixes nushell#7189



# User-Facing Changes
None

# Tests + Formatting
This only affects existing tests, which still pass.
dmatos2012 pushed a commit to dmatos2012/nushell that referenced this issue Feb 20, 2024
# Description

This PR implements modifications to command tests that write unnecessary
json and csv to disk then load it with open, by using nuon literals
instead.

- Fixes nushell#7189



# User-Facing Changes
None

# Tests + Formatting
This only affects existing tests, which still pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed tests issues to add tests or fix tests
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants