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: fix some test failures for Windows OS #7364

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Sep 17, 2024

WP_UnitTestCase_Base: introduce a new assertion and a new helper method

Introduces WP_UnitTestCase_Base::assertSamePathIgnoringDirectorySeparators() and an associated helper method WP_UnitTestCase_Base::normalizeDirectorySeparatorsInPath() to allow for comparing two file path strings independently of OS-specific differences.

The normalization is done in a separate method to also allow this method to be used for path normalization within test methods themselves, like for normalizing a group of paths in an array.

The pretty specific method name for the helper (normalizeDirectorySeparatorsInPath()) is an attempt to prevent naming conflicts with methods which may exist in plugin test suites build on top of the WP Core test suite.

Tests: start using the new assertion and helper method

Trac ticket: https://core.trac.wordpress.org/ticket/61530


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

github-actions bot commented Sep 17, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jrf, hellofromtonya.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

*
* @return string
*/
public function normalizeDirectorySeparatorsInPath( $path ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

As this method is not an expect or assert, shouldn't it use the WordPress name convention?

Suggested change
public function normalizeDirectorySeparatorsInPath( $path ) {
public function normalize_directory_separators_in_path( $path ) {

Copy link
Member Author

Choose a reason for hiding this comment

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

The use of camelCase is deliberate for consistency with other expectation/assertion methods in the TestCase file.

Copy link
Contributor

Choose a reason for hiding this comment

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

But in this case, it's a public helper function and not an expectation or assertion. It is used by a new assertion, yes. But it could also be used within a test class / method as a helper for setting up conditions before doing an assertion or expectation, such as being done in this PR in test_get_block_templates_paths_dir_exists().

As it's a helper, I'm wondering if it should follow snake_case, similar to other helper methods.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, as I hinted at before, quite a lot of those helpers in the TestCase use camelCase, so I still think this is most consistent. Some examples:

  • skipOnAutomatedBranches()
  • expectDeprecated()
  • setExpectedIncorrectUsage()
  • knownWPBug()
  • prepareTemplate()

Note: AFAICS none of these are required to be camelCase by PHPUnit, still they are.

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right. There are mixed naming conventions for the helpers, i.e. some are camelCase (as you note) and others are snake_case.

I don't have strong opinions. Given precedence, seems okay to use camelCase.

Note: Though out of scope for this PR, might be worth establishing a code standard for test case helper methods and updating the handbook.

Copy link
Member Author

Choose a reason for hiding this comment

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

Note: Though out of scope for this PR, might be worth establishing a code standard for test case helper methods and updating the handbook.

I vaguely remember we opened a ticket about this quite a while back (including about naming conventions for data providers and such). Would need to search for it, but I think it should still be open.

@jrfnl jrfnl force-pushed the trac-61530/tests-assertsamepathignoringdirectoryseparators branch from 74c6a86 to e9c5b25 Compare September 18, 2024 16:55
Introduces `WP_UnitTestCase_Base::assertSamePathIgnoringDirectorySeparators()` and an associated helper method `WP_UnitTestCase_Base::normalizeDirectorySeparatorsInPath()` to allow for comparing two file path strings independently of OS-specific differences.

The normalization is done in a separate method to also allow this method to be used for path normalization within test methods themselves, like for normalizing a group of paths in an array.

The pretty specific method name for the helper (`normalizeDirectorySeparatorsInPath()`) is an attempt to prevent naming conflicts with methods which may exist in plugin test suites build on top of the WP Core test suite.
@jrfnl jrfnl force-pushed the trac-61530/tests-assertsamepathignoringdirectoryseparators branch from e9c5b25 to 309ce81 Compare September 18, 2024 17:40
Copy link
Contributor

@hellofromtonya hellofromtonya 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 this idea / approach to encapsulate the logic for the OS-specifics and providing a reusable assertion.

Ready for commit 👍

@hellofromtonya
Copy link
Contributor

@hellofromtonya hellofromtonya deleted the trac-61530/tests-assertsamepathignoringdirectoryseparators branch September 18, 2024 21:06
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.

2 participants