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/util: Implement enhancements of CmdResult #4259 #4261

Merged
merged 6 commits into from
Jan 25, 2023

Conversation

Joining7943
Copy link
Contributor

@Joining7943 Joining7943 commented Jan 4, 2023

This pr enhances CmdResult. The following changes are introduced in CmdResult:

  • Add methods stderr_str_apply, stderr_apply, stdout_str_apply, stdout_apply to apply a function to the stderr or stdout returning a new CmdResult on which the assertions in CmdResult can be run.
  • Add methods stderr_str_check, stderr_check, stdout_str_check, stdout_check. Enables running arbitrary functions returing a bool to verify the stdout or stderr in addition to the assertions CmdResult already provides.
  • Remove trimming of stderr in stderr_is. Add a separate method stderr_trimmed_is for asserting stderr trimmed of whitespace.
  • Remove the code and success fields and instead store the ExitStatus of the child process in the exit_status field. A
  • Add methods like signal_is, signal_name_is to assert the received signal on unix systems.
  • Improve the error messages of stderr_is_bytes and stdout_is_bytes to include (in addition to the diff of the bytes arrays) the stdout, stderr converted from bytes to a string if possible. If not possible the UTF-8 error message is shown instead of the string.

The integration tests needed to be adjusted to the new situation in stderr_is and now assert whitespace which was previously trimmed.

Some tests in test_tr fail now and needed to be disabled until fixed. tr produces sometimes two newlines at the end of the output instead of just one. I cross checked some those tests randomly with gnu's tr, to ensure that only one newline is expected.

Closes #4259

@Joining7943 Joining7943 force-pushed the tests-util-enhance-cmdresult branch from 410c896 to d269c62 Compare January 5, 2023 16:35
@github-actions
Copy link

github-actions bot commented Jan 5, 2023

GNU testsuite comparison:

GNU test failed: tests/misc/timeout. tests/misc/timeout is passing on 'main'. Maybe you have to rebase?

@github-actions
Copy link

github-actions bot commented Jan 5, 2023

GNU testsuite comparison:

GNU test failed: tests/misc/timeout. tests/misc/timeout is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/tail-2/inotify-dir-recreate. tests/tail-2/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

@Joining7943 Joining7943 force-pushed the tests-util-enhance-cmdresult branch from fc77e49 to b2e4a22 Compare January 6, 2023 09:50
@github-actions
Copy link

github-actions bot commented Jan 6, 2023

GNU testsuite comparison:

GNU test failed: tests/tail-2/inotify-dir-recreate. tests/tail-2/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

@tertsdiepraam
Copy link
Member

Instead of applying str::trim I think I prefer checking against untrimmed strings in most of these cases. So

cmdresult.stdout_is("cat: some error");
// becomes
cmdresult.stdout_is("cat: some error\n");
// instead of
cmdresult
    .stdout_trimmed_is("cat: some error");
// or
cmdresult
    .stdout_str_apply(str::trim)
    .stdout_is("cat: some error");

because that's a more precise test anyway.

@Joining7943
Copy link
Contributor Author

Instead of applying str::trim I think I prefer checking against untrimmed strings in most of these cases.

I'd actually prefer, too. I'll try to change as much of these usages.

@Joining7943
Copy link
Contributor Author

What do you think of TestScenario::scmd? I don't needed it often but if so, it's nice to have it and spares the tedious setup of a different shell command for windows and unix.

@tertsdiepraam
Copy link
Member

tertsdiepraam commented Jan 6, 2023

The functionality is good, but I'd prefer if it was a separate setting on UCommand. I find cmd, ccmd & ucmd confusing enough already 😄

@Joining7943
Copy link
Contributor Author

The functionality is good, but I'd prefer if it was a separate setting on UCommand. I find cmd, ccmd & ucmd confusing enough already

In UCommand it needs to be a new_something -> Self method and can't be a setting or else it breaks the UCommand::arg, UCommand::args methods. However, I moved some of the functionality into UCommand, so there's more than one way to construct a shell command. If you have an idea how to do it differently, I'd love to hear. I've changed the name from TestScenario::scmd to TestScenario::shell_cmd. I hope this is less confusing, just thought scmd enrows into the naming scheme in TestScenario.

@Joining7943 Joining7943 force-pushed the tests-util-enhance-cmdresult branch from d3756ea to 4d311be Compare January 6, 2023 15:23
@tertsdiepraam
Copy link
Member

It would work if we only construct the raw Command when the command gets run and keep track of the executable, arguments and env vars ourselves until that point. That makes it complicated enough for a separate PR though.

@github-actions
Copy link

github-actions bot commented Jan 6, 2023

GNU testsuite comparison:

GNU test failed: tests/tail-2/inotify-dir-recreate. tests/tail-2/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

@Joining7943
Copy link
Contributor Author

Joining7943 commented Jan 6, 2023

Ok, that would work, but is it worth it? This solution is less involved and provides pretty much the same functionality. Or is there any other gain in tracking the args, env vars etc.?

@github-actions
Copy link

github-actions bot commented Jan 6, 2023

GNU testsuite comparison:

GNU test failed: tests/tail-2/inotify-dir-recreate. tests/tail-2/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

@tertsdiepraam
Copy link
Member

We could use it to provide better error messages maybe? I'm not sure either.

@Joining7943
Copy link
Contributor Author

We could use it to provide better error messages maybe? I'm not sure either.

Me neither. But, I actually like your idea, also because this is the way how a builder like UCommand usually works, so refactoring UCommand might be a good idea.

However, would you be fine with keeping TestScenario::shell_cmd and UCommand::new_shell_cmd the way it is for now? I can create a UCommand refactoring issue and then we can track these two methods, so we don't forget about it.

@Joining7943 Joining7943 force-pushed the tests-util-enhance-cmdresult branch from 036c75f to 6a54f3c Compare January 6, 2023 18:23
@Joining7943 Joining7943 marked this pull request as ready for review January 6, 2023 18:50
@Joining7943
Copy link
Contributor Author

This pr would be ready so far. Please tell me if you're ok with keeping the TestScenario::shell_cmd and UCommand::new_shell_cmd methods.

Please note that I updated the pr description. Some tests in test_tr needed to be disabled because of 2 newlines instead of an expected amount of 1, so there's an issue in tr somewhere.

@github-actions
Copy link

github-actions bot commented Jan 6, 2023

GNU testsuite comparison:

GNU test failed: tests/tail-2/inotify-dir-recreate. tests/tail-2/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

@tertsdiepraam
Copy link
Member

Please tell me if you're ok with keeping the TestScenario::shell_cmd and UCommand::new_shell_cmd methods.

Do you think they're useful on their own? If were gonna do the same thing a different way soon and remove these I don't think we need to include them here.

/// Return the exit status of the child process, if any.
///
/// Returns None if the child process is still running or hasn't been started.
pub fn maybe_exit_status(&self) -> Option<ExitStatus> {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think try_exit_status is idiomatic Rust

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dunno, just tried something here. I searched the stdlib once and found no clear answer on how a naming scheme should look like for methods that return an Option. Try is definitely a good choice for methods that return a Result. Most of the time methods that return an Option are not given a special prefix, seldomly they are prefixed with try or even maybe. Sometimes they prefix variables that contain an option with maybe. If you'd agree, in our case it's probably best to remove the maybe prefix, since this is just a getter and we don't really try to get something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I overlooked, that I named another method exit_status already. It's try_exit_status, then, if that's preferred over maybe_exit_status.

tests/common/util.rs Show resolved Hide resolved
Comment on lines 544 to 547
pub fn stderr_trimmed_is<T: AsRef<str>>(&self, msg: T) -> &Self {
assert_eq!(self.stderr_str().trim(), msg.as_ref());
self
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need this now that we check the newlines explicitly? I think the problem you found with tr show that checking the newlines explicitly is the way to go.

Copy link
Contributor Author

@Joining7943 Joining7943 Jan 7, 2023

Choose a reason for hiding this comment

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

Do we still need this now that we check the newlines explicitly?

Nope. I'll remove the method.

@Joining7943
Copy link
Contributor Author

Do you think they're useful on their own? If were gonna do the same thing a different way soon and remove these I don't think we need to include them here.

ok, let's remove them and address this in another pr.

@Joining7943 Joining7943 force-pushed the tests-util-enhance-cmdresult branch 2 times, most recently from 461003f to db6e429 Compare January 7, 2023 16:54
@github-actions
Copy link

github-actions bot commented Jan 7, 2023

GNU testsuite comparison:

GNU test failed: tests/tail-2/inotify-dir-recreate. tests/tail-2/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

@Joining7943
Copy link
Contributor Author

Everything alright with this pr? I addressed all your comments. If it's ok I don't squash the commits any further.

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.

Yeah looks good! Just one more nit. As a final squash maybe you could add the signals feature to the commit that introduced the need for them.

/// zero-exit from running the Command?
/// see [`success`]
success: bool,
// /// exit status for command (if there is one)
Copy link
Member

Choose a reason for hiding this comment

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

There's a double comment here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@Joining7943 Joining7943 force-pushed the tests-util-enhance-cmdresult branch from 548df96 to eee6cdc Compare January 11, 2023 12:32
@Joining7943
Copy link
Contributor Author

As a final squash maybe you could add the signals feature to the commit that introduced the need for them.

Sure, no problem.

@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/rm/rm2. tests/rm/rm2 is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/tail-2/inotify-dir-recreate. tests/tail-2/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

@Joining7943 Joining7943 force-pushed the tests-util-enhance-cmdresult branch from eee6cdc to 338e951 Compare January 19, 2023 14:39
@Joining7943
Copy link
Contributor Author

@tertsdiepraam my account was suspended since last week. I'm not sure about the reason for the suspend, but github unsuspended my account today after I wrote to the github support several times. Seems, that all prs, issues etc. which I created, disappeared for that period. However, it looks like noting's lost and I rebased onto main and resolved all conflicts with the main branch. Besides that, I made no changes and we're back to the state of last week.

… unix systems

Add tests for these signal methods. Include `signals` feature in Cargo.toml uucore dev-dependencies.
…derr_trimmed_is.

Fix tests assert whitespace instead of trimming it. Disable some tests in `test_tr` because `tr`
produces too many newlines.
@Joining7943 Joining7943 force-pushed the tests-util-enhance-cmdresult branch from 338e951 to 78a3810 Compare January 22, 2023 13:56
@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/misc/timeout. tests/misc/timeout is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/tail-2/inotify-dir-recreate. tests/tail-2/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

@tertsdiepraam
Copy link
Member

my account was suspended since last week

Glad to see you're back!

@tertsdiepraam tertsdiepraam merged commit fc7e51a into uutils:main Jan 25, 2023
@Joining7943
Copy link
Contributor Author

Glad to see you're back!

yeah me too! Honestly missed that working on uutils.

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.

tests/util: Enhance CmdResult
2 participants