backdrop-issues icon indicating copy to clipboard operation
backdrop-issues copied to clipboard

Simpletest semi-silently ignores wrong file paths in .tests.info file

Open bugfolder opened this issue 3 years ago • 32 comments

As noted in https://github.com/backdrop/backdrop-issues/issues/5490,

it turned out that several tests actually never run: core/modules/system/tests/system.tests.info contains wrong file paths It's still not clear, why these tests ran in the past, but don't for several weeks. Eventually related: Issue https://github.com/backdrop/backdrop-issues/issues/5415: Gracefully handle empty or malformed .tests.info files.

In https://github.com/backdrop/backdrop-issues/issues/5415, we introduced code that checked for malformed .tests.info files and put errors into watchdog when they're detected. We can (should) add to this error-checking to verify that the file paths specified in the tests.info file are correct.

PR to follow.

bugfolder avatar Feb 04 '22 00:02 bugfolder

The PR can be tested with the existing code base (prior to merging PR https://github.com/backdrop/backdrop/pull/3942) by:

  • Enabling Simpletest
  • Visiting the testing page at admin/config/development/testing
  • Going to watchdog at admin/reports/dblog and examining the most recent entry.

bugfolder avatar Feb 04 '22 00:02 bugfolder

Here's the Tugboat dblog report:

dblog

bugfolder avatar Feb 04 '22 00:02 bugfolder

@bugfolder many thanks for the PR, the code looks good.

I do have two concerns, though:

  • The watchdog message happens only once (simpletest caches the result), so this warning may be overlooked, unless caches get flushed often.
  • Besides that log message, there's now absolutely nothing that indicates a problem. Via UI tests simply don't appear, in command line runs tests aren't listed, either.

Your PR hides problems even more. For instance, in our GHA nobody would ever realize that something's broken, because what previously caused errors is now in complete stealth mode. That seems a bit dangerous to me.

indigoxela avatar Feb 04 '22 08:02 indigoxela

Indeed, thank you @bugfolder ...but I'm also with @indigoxela on this: we need to be more "verbose" when there are wrong paths in test files. I'd suggest at least the following:

  • when the simpletest module is enabled if test files with erroneous file paths are detected, there's an error shown in the test selection page at admin/config/development/testing (and perhaps also on the site status page?).
  • we make it so that tests fail hard when test file paths are wrong (test files missing)

klonos avatar Feb 04 '22 13:02 klonos

@klonos yes, that would make sense IMO :+1:

A quick idea regarding command line: we have function backdrop_is_cli(), which we can use to let tests fail for cli, but only show a descriptive error message on the selection page when using UI, still suppressing fatal errors. Not sure how easy/complex that would be.

indigoxela avatar Feb 04 '22 13:02 indigoxela

  • Agreed that stronger notification is needed. Seems like any of the error states detected in simpletest_get_all() would warrant strong notification, not just the bad file path error.
  • An easy way to highlight problems in the UI would be to post the same error message using backdrop_set_message() at the same time as the watchdog() entry. Reasonable?
  • We could ensure that the check is always run when viewing the UI if we cleared the static variable in simpletest_test_form(), but that would slow down form loading. Is that an acceptable price to pay (considering that the vast majority of the time there won't be any errors)?
  • using backdrop_is_cli() we could throw a failed test assertion instead of posting messages and watchdog, but there's other reasons to run backdrop from the CLI (e.g., cron calls) that wouldn't warrant a hard fail. Perhaps testing setUp() routine could post a global variable $_running tests (maybe a static variable in BackdropWebTestCase), or something like that, that could be probed to know whether tests are running.

bugfolder avatar Feb 04 '22 15:02 bugfolder

An easy way to highlight problems in the UI would be to post the same error message using backdrop_set_message() at the same time as the watchdog() entry. Reasonable?

Yes, makes sense. Hm... regarding the static cache - I'm not sure if I understand what you're after.

using backdrop_is_cli() we could throw a failed test assertion instead

This might be a bit tricky (the assertion part), because the error already happens when collecting data and the test class itself isn't loaded. But we could do something easier like throwing an Exception. Then it would (should?) be visible in the GHA and also when running it locally via cli. And people could get the location and name of the failing test.info file.

throw new Exception('Some helpful hint with path and name here.');

My main concern is that currently it might appear as if "everything's green", although there's a severe problem. An exception could be enough to indicate "a problem needs to get addressed". There may be other opinions, though.

...but there's other reasons to run backdrop from the CLI (e.g., cron calls) that wouldn't warrant a hard fail

I don't actually think that a cron run triggers function simpletest_test_get_all().

indigoxela avatar Feb 04 '22 15:02 indigoxela

I don't actually think that a cron run triggers function simpletest_test_get_all().

Doi!

bugfolder avatar Feb 04 '22 16:02 bugfolder

regarding the static cache - I'm not sure if I understand what you're after.

Sorry, not the static cache, the db cache (the cache()->set('simpletest', $groups) line in simpletetest_get_all()). If you refresh the testing listing page without clearing the cache, the test code doesn't run because the page is using the cached values.

bugfolder avatar Feb 04 '22 16:02 bugfolder

Doi!

Huh? :upside_down_face:

not the static cache, the db cache (the cache()->set('simpletest', $groups)

Ah, OK, now I get it. :smile: Hm... I'd prefer if we find a solution without removing that cache. Not sure what would be smart here.

indigoxela avatar Feb 04 '22 16:02 indigoxela

Doi! Huh?

Sorry, an Americanism. It means, roughly, "I should have realized that if I had two neurons to rub together." 😉

bugfolder avatar Feb 04 '22 19:02 bugfolder

I'd prefer if we find a solution without removing that cache. Not sure what would be smart here.

Maybe we start by trying it without explicitly clearing the cache. If people are doing development, there's probably some cache-clearing going on, and that's when they'd most need to catch typos in the file path (or other malformations).

bugfolder avatar Feb 04 '22 19:02 bugfolder

Well, that was suprising. If I just throw an exception in simpletest_get_all(), the attempted test run fails, but shows up green in the PR. Not what we want.

test

bugfolder avatar Feb 04 '22 23:02 bugfolder

So, how to turn that unhandled exception into a GHA failure exit code?

bugfolder avatar Feb 05 '22 00:02 bugfolder

Huh? Seems like although there's an exception, the exit code of run-tests.sh is still 0? :confused: And GHA interprets this 0 as success...

Can we try something like that instead?

print "$error\n";
exit(1);

Just to see how that shows up in the action.

indigoxela avatar Feb 05 '22 10:02 indigoxela

Much better. (I also eliminated the HTML formatting (@uri versus %uri) from the error messages to make them usable both in the UI and as GHA error messages.)

test results

bugfolder avatar Feb 05 '22 15:02 bugfolder

I've now removed the intentional error (in user.tests.info) from the PR.

bugfolder avatar Feb 05 '22 15:02 bugfolder

@bugfolder many thanks for updating. And removing the formatting makes messages much easier to read, when running on command line (both, local/manual and in GHA).

No more hidden-away errors - wrong file paths in info files are now loud enough. Works for me. :+1:

I had another idea re errors - doing it like function assert and put the individual test failures in the database, but didn't dig deeper. IMO that would be a waste of resources, as at that point we already know, the tests have to fail. Running the remaining (still correct) tests would be a waste of time and CPU.

indigoxela avatar Feb 06 '22 09:02 indigoxela

@indigoxela, @klonos, either of you up for a code review to get this to RTBC?

bugfolder avatar Feb 10 '22 02:02 bugfolder

I don't think, I should do the code review here - got too much involved in the current solution. Another pairs of eyes would be appreciated.

indigoxela avatar Feb 10 '22 15:02 indigoxela

I'm not sure about the PR... In some places $error is a plain string, in others it uses the t() function, and in yet others it uses format_string(). Finally, at the end, it seems that regardless of how it was set above, it's then passed through format_string()...

Am I reading it wrong?

ghost avatar Jul 02 '22 08:07 ghost

... plain string, ... the t() function, ... format_string()

Not to forget format_plural(). :wink:

TBH I never completely understood, when to translate a string in simpletest and when not.

Re plain string vs. format_string - the latter is used when it's necessary to insert variables in a string. It won't get translated. format_plural does translate, though.

indigoxela avatar Jul 02 '22 09:07 indigoxela

I find this posting very helpful. In a nutshell, only use t when testing translations, or when you want to match UI text that may be translated.

https://www.drupal.org/docs/7/testing/simpletest-testing-tutorial-drupal-7#t

That post advises using format_string instead when formatting and sanitizing.

argiepiano avatar Jul 02 '22 14:07 argiepiano

So, although Simpletest module is all about testing, the error messages in this PR are not "messages that result from running a test," but rather are messages that the user sees in the UI (via backdrop_set_message() &/or watchdog()) when they're deciding what tests to run. So the messages in this PR should be translated.

And it's a little confusing because format_string() explicitly doesn't run its string arguments through t(), but format_plural() does. So in this PR, a string destined for format_string() should have been run through t(), but we can (and should) pass "raw" strings to format_plural(), which will do its own t()-ifying.

With all that being said, @BWPanda did indeed catch an inconsistency: I missed the very first case (it needed t()), now fixed in the PR.

bugfolder avatar Jul 02 '22 18:07 bugfolder

@indigoxela, @klonos, @argiepiano, any of you up for a code review of this?

bugfolder avatar Oct 30 '22 21:10 bugfolder

@bugfolder I've left some questions. It's not about newly introduced things, but pre-existing.

indigoxela avatar Oct 31 '22 09:10 indigoxela

@indigoxela, thanks for the review! I added some explanation to your first comment; more details are in my comment above.

I closed and reopened the PR, and now PHPCS is complaining about entirely unrelated files (entityreference module), so I'm going to ignore that for now.

bugfolder avatar Oct 31 '22 19:10 bugfolder

@bugfolder I opened a separate issue for the phpcs behavior.

indigoxela avatar Nov 01 '22 08:11 indigoxela

Rebased the PR. I also added some comments and code changes to make clearer the delayed substitution of variables (still using '@' rather than '%' for clarity on the command line and in GHA messages).

bugfolder avatar Nov 01 '22 14:11 bugfolder

@indigoxela, I think the PHPCS issues are being dealt with in your new issue (thanks!); would you (&/or @argiepiano) take a look at the new PR on this issue and see if the changes address your concerns about splitting up the variable substitutions (they're now applied all in one place)?

bugfolder avatar Nov 04 '22 14:11 bugfolder