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

test utilities: easy way to simulate terminal context #5869

Merged
merged 2 commits into from
Mar 1, 2024

Conversation

cre4ture
Copy link
Contributor

@cre4ture cre4ture commented Jan 22, 2024

addresses #5785
and addresses #1857
replaces if accepted PR #5858

let me know what you think. It's very easy to use. Just do a .simulate_terminal(true) and thats it.
I implemented in such a way that also stdin piping still works and also the capturing of stdout and stderr works as usual.
EDIT: For cases where the terminal size matters, there is no an additional function available:

.terminal_size(libc::winsize {
                ws_col: 40,
                ws_row: 10,
                ws_xpixel: 40 * 8,
                ws_ypixel: 10 * 10,
            })

Example:

#[test]
fn test_simulation_of_terminal_true() {
    let scene = TestScenario::new(util_name!());

    let out = scene
        .ucmd()
        .arg("sh")
        .arg("is_atty.sh")
        .terminal_simulation(true)
        .succeeds();
    assert_eq!(
        String::from_utf8_lossy(out.stdout()),
        "stdin is atty\r\nstdout is atty\r\nstderr is atty\r\n"
    );
    assert_eq!(
        String::from_utf8_lossy(out.stderr()),
        "This is an error message.\r\n"
    );
}

where is_atty.sh looks like this:

#!/bin/bash

if [ -t 0 ] ; then
    echo "stdin is atty"
else
    echo "stdin is not atty"
fi

if [ -t 1 ] ; then
    echo "stdout is atty"
else
    echo "stdout is not atty"
fi

if [ -t 2 ] ; then
    echo "stderr is atty"
else
    echo "stderr is not atty"
fi

echo "This is an error message." > /dev/stderr

true

tests/common/util.rs Outdated Show resolved Hide resolved
@cre4ture cre4ture force-pushed the feature/simulate_terminal_utility branch from 2ac77c5 to 7bfed9e Compare January 23, 2024 21:07
@cre4ture cre4ture force-pushed the feature/simulate_terminal_utility branch from 7bfed9e to 0e11cdc Compare February 4, 2024 22:32
@RenjiSann
Copy link
Contributor

Is it ready to merge already, or are we waiting for further improvements ?

@cre4ture cre4ture force-pushed the feature/simulate_terminal_utility branch from 0e11cdc to a803ce1 Compare February 7, 2024 21:02
@cre4ture
Copy link
Contributor Author

cre4ture commented Feb 7, 2024

What is missing is a review and approval from one of the maintainers.
@sylvestre - I also did the squash commit now. Let me know what you think.

Copy link
Member

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

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

I like it a lot! Excellent work! I only have some small questions.

I also think that we might want to be able to configure the terminal with this interface, but for now, the API is flexible enough I think. It's also about time we extract the test framework to a crate, so we can generate documentation for it. It's getting quite complex. That's something for another PR though.

I would like to wait for approval of one of the other maintainers before merging though.

One final question: have you seen any intermittently failing tests with this? Can we assume that this implementation is robust enough not to make our test suite fail unexpectedly (even more so then it already does 😅)

.gitignore Outdated Show resolved Hide resolved
fn test_simulation_of_terminal_false() {
let scene = TestScenario::new(util_name!());

let out = scene.ucmd().arg("sh").arg("is_atty.sh").succeeds();
Copy link
Member

Choose a reason for hiding this comment

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

Are these meaningful tests of env? If not, it could still be useful to have them (we're going very meta with tests for our test framework 😄), but maybe somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, yes. This is a valid point. I used env here as a tool rather than to test itself. This is something I should cleanup. Do you have a proposal for a better location of these tests?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe in the file where the test framework is defined? It's an interesting question, we don't really have tests for the tests yet 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@cre4ture cre4ture force-pushed the feature/simulate_terminal_utility branch 3 times, most recently from 0d14526 to 66ded04 Compare February 8, 2024 20:47
@cre4ture
Copy link
Contributor Author

cre4ture commented Feb 8, 2024

One final question: have you seen any intermittently failing tests with this? Can we assume that this implementation is robust enough not to make our test suite fail unexpectedly (even more so then it already does 😅)

I think its stable.

@cre4ture cre4ture closed this Feb 8, 2024
@cre4ture cre4ture reopened this Feb 8, 2024
@cre4ture cre4ture force-pushed the feature/simulate_terminal_utility branch from 66ded04 to d21d2f1 Compare February 10, 2024 13:36
@cre4ture cre4ture force-pushed the feature/simulate_terminal_utility branch 2 times, most recently from 943c22b to b291cf3 Compare February 24, 2024 16:11
@cre4ture cre4ture force-pushed the feature/simulate_terminal_utility branch from b291cf3 to a4d5def Compare February 25, 2024 17:15
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/inotify-dir-recreate (passes in this run but fails in the 'main' branch)

1 similar comment
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/inotify-dir-recreate (passes in this run but fails in the 'main' branch)

@cre4ture cre4ture force-pushed the feature/simulate_terminal_utility branch 2 times, most recently from 5d57805 to ef02e43 Compare February 25, 2024 20:40
@cre4ture cre4ture force-pushed the feature/simulate_terminal_utility branch from ef02e43 to d8b3b41 Compare February 25, 2024 20:47
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/inotify-dir-recreate (passes in this run but fails in the 'main' branch)

@sylvestre sylvestre merged commit 9003e3f into uutils:main Mar 1, 2024
62 checks passed
@sylvestre
Copy link
Sponsor Contributor

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants